From 2c25de11d1db0621a6d21a2f3d12245304bfdc31 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 17 Jun 2016 03:24:58 +0300 Subject: make the status logger a database writer --- track_status.py | 17 +++++---- vk/utils/tracking/__init__.py | 3 +- vk/utils/tracking/db/writer/__init__.py | 2 +- vk/utils/tracking/db/writer/log.py | 56 +++++++++++++++++++++++++++++ vk/utils/tracking/logger.py | 64 --------------------------------- 5 files changed, 68 insertions(+), 74 deletions(-) create mode 100644 vk/utils/tracking/db/writer/log.py delete mode 100644 vk/utils/tracking/logger.py diff --git a/track_status.py b/track_status.py index 8f164b7..9935b64 100644 --- a/track_status.py +++ b/track_status.py @@ -3,11 +3,11 @@ # See LICENSE.txt for details. import vk.api -from vk.utils.tracking import Logger, StatusTracker +from vk.utils.tracking import StatusTracker from vk.utils.tracking.db.writer import * if __name__ == '__main__': - import argparse, sys + import argparse, logging, sys def natural_number(s): x = int(s) @@ -31,14 +31,17 @@ if __name__ == '__main__': args = parser.parse_args() - Logger.set_up(args.log) + logging.basicConfig(format='[%(asctime)s] %(message)s', + stream=args.log, + level=logging.INFO, + datefmt='%Y-%m-%d %H:%M:%S') api = vk.api.API(vk.api.Language.EN) tracker = StatusTracker(api, args.timeout) - tracker.add_initial_status_handler(Logger.on_initial_status) - tracker.add_status_update_handler(Logger.on_status_update) - tracker.add_connection_error_handler(Logger.on_exception) + tracker.add_initial_status_handler(log.Logger.on_initial_status) + tracker.add_status_update_handler(log.Logger.on_status_update) + tracker.add_connection_error_handler(log.Logger.on_exception) with csv.Writer(args.output) as csv_writer: @@ -49,5 +52,5 @@ if __name__ == '__main__': try: tracker.loop(args.uids) except Exception as e: - Logger.on_exception(e) + logging.exception(e) sys.exit(1) diff --git a/vk/utils/tracking/__init__.py b/vk/utils/tracking/__init__.py index 641d4c8..2e8b02e 100644 --- a/vk/utils/tracking/__init__.py +++ b/vk/utils/tracking/__init__.py @@ -2,7 +2,6 @@ # This file is licensed under the terms of the MIT License. # See LICENSE.txt for details. -from .logger import Logger from .status_tracker import StatusTracker -__all__ = 'logger', 'status_tracker', +__all__ = 'status_tracker', diff --git a/vk/utils/tracking/db/writer/__init__.py b/vk/utils/tracking/db/writer/__init__.py index 330d5a8..8eb62e1 100644 --- a/vk/utils/tracking/db/writer/__init__.py +++ b/vk/utils/tracking/db/writer/__init__.py @@ -2,4 +2,4 @@ # This file is licensed under the terms of the MIT License. # See LICENSE.txt for details. -__all__ = 'csv', +__all__ = 'csv', 'log' diff --git a/vk/utils/tracking/db/writer/log.py b/vk/utils/tracking/db/writer/log.py new file mode 100644 index 0000000..02697b7 --- /dev/null +++ b/vk/utils/tracking/db/writer/log.py @@ -0,0 +1,56 @@ +# Copyright 2016 Egor Tensin +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +import logging + +class Logger: + @staticmethod + def on_initial_status(user): + if user.is_online(): + logging.info(Logger._format_user_is_online(user)) + else: + logging.info(Logger._format_user_is_offline(user)) + logging.info(Logger._format_user_last_seen(user)) + + @staticmethod + def on_status_update(user): + if user.is_online(): + logging.info(Logger._format_user_went_online(user)) + else: + logging.info(Logger._format_user_went_offline(user)) + logging.info(Logger._format_user_last_seen(user)) + + @staticmethod + def on_exception(e): + logging.exception(e) + + @staticmethod + def _format_user(user): + if user.has_last_name(): + return '{} {}'.format(user.get_first_name(), user.get_last_name()) + else: + return '{}'.format(user.get_first_name()) + + @staticmethod + def _format_user_is_online(user): + return '{} is ONLINE.'.format(Logger._format_user(user)) + + @staticmethod + def _format_user_is_offline(user): + return '{} is OFFLINE.'.format(Logger._format_user(user)) + + @staticmethod + def _format_user_last_seen(user): + return '{} was last seen at {} using {}.'.format( + Logger._format_user(user), + user.get_last_seen_time_local(), + user.get_last_seen_platform().get_description_for_sentence()) + + @staticmethod + def _format_user_went_online(user): + return '{} went ONLINE.'.format(Logger._format_user(user)) + + @staticmethod + def _format_user_went_offline(user): + return '{} went OFFLINE.'.format(Logger._format_user(user)) diff --git a/vk/utils/tracking/logger.py b/vk/utils/tracking/logger.py deleted file mode 100644 index 8da418a..0000000 --- a/vk/utils/tracking/logger.py +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2016 Egor Tensin -# This file is licensed under the terms of the MIT License. -# See LICENSE.txt for details. - -import logging -import sys - -class Logger: - @staticmethod - def set_up(fd=sys.stdout): - logging.basicConfig(format='[%(asctime)s] %(message)s', - stream=fd, - level=logging.INFO, - datefmt='%Y-%m-%d %H:%M:%S') - - @staticmethod - def on_initial_status(user): - if user.is_online(): - logging.info(Logger._format_user_is_online(user)) - else: - logging.info(Logger._format_user_is_offline(user)) - logging.info(Logger._format_user_last_seen(user)) - - @staticmethod - def on_status_update(user): - if user.is_online(): - logging.info(Logger._format_user_went_online(user)) - else: - logging.info(Logger._format_user_went_offline(user)) - logging.info(Logger._format_user_last_seen(user)) - - @staticmethod - def on_exception(e): - logging.exception(e) - - @staticmethod - def _format_user(user): - if user.has_last_name(): - return '{} {}'.format(user.get_first_name(), user.get_last_name()) - else: - return '{}'.format(user.get_first_name()) - - @staticmethod - def _format_user_is_online(user): - return '{} is ONLINE.'.format(Logger._format_user(user)) - - @staticmethod - def _format_user_is_offline(user): - return '{} is OFFLINE.'.format(Logger._format_user(user)) - - @staticmethod - def _format_user_last_seen(user): - return '{} was last seen at {} using {}.'.format( - Logger._format_user(user), - user.get_last_seen_time_local(), - user.get_last_seen_platform().get_description_for_sentence()) - - @staticmethod - def _format_user_went_online(user): - return '{} went ONLINE.'.format(Logger._format_user(user)) - - @staticmethod - def _format_user_went_offline(user): - return '{} went OFFLINE.'.format(Logger._format_user(user)) -- cgit v1.2.3