From bd3e99fccc75ef3b9d3c8083ce7a2e411e12060e Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Wed, 14 Nov 2018 14:52:21 +0100 Subject: [PATCH 1/5] Added a log_context variable to add some extra context to the log messages --- CHANGELOG.md | 9 ++++++++ README.rst | 41 +++++++++++++++++++------------------ pylogging/_create_logger.py | 29 ++++++++++++++++++++++---- setup.cfg | 5 +++++ 4 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..cedf0df --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Change Log +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +# tag:0.2.5 / 2018-11-14 +- Code Refactor +- Introduced a new parameter `log_context` which will let users define a dictionary with some contextual information. diff --git a/README.rst b/README.rst index fc3e99b..9a07062 100644 --- a/README.rst +++ b/README.rst @@ -14,7 +14,7 @@ Current stable version: :: - 0.2.3 + 0.2.5 Installation: ~~~~~~~~~~~~~ @@ -39,11 +39,11 @@ Install by adding to setup.py of your project ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Add the following to the ``install_requires`` parameter of your setup - function: ``install_requires=['pylogging==0.1.0'],`` + function: ``install_requires=['pylogging==0.2.5'],`` - Add the following to the ``dependency_links`` parameter of your setup function: - ``dependency_links=['https://github.com/ansrivas/pylogging/tarball/master#egg=pylogging-0.1.0'],`` + ``dependency_links=['https://github.com/ansrivas/pylogging/tarball/master#egg=pylogging-0.2.5'],`` - Install your project along with ``pylogging`` by running the command: ``python setup.py install`` @@ -75,20 +75,21 @@ Important arguments to ``setup_logger`` function: :: - log_directory (str) :directory to write log files to. Applicable only when `allow_file_logging` = True - file_handler_type :object of logging handler from HandlerType class. Applicable only when `allow_file_logging` = True - allow_console_logging (bool) :Turn off/on the console logging. - allow_file_logging (bool) :Turn off/on if logs need to go in files as well. - backup_count (int) :Number of files to backup before rotating the logs. - max_file_size_bytes (int) :Size of file in bytes before rotating the file. Applicable only to ROTATING_FILE_HANDLER. - when_to_rotate (str) :Duration after which a file can be rotated. Applicable only to TIME_ROTATING_FILE_HANDLER - Accepts following values: - 'S' Seconds - 'M' Minutes - 'H' Hours - 'D' Days - 'W0'-'W6' Weekday (0=Monday) - 'midnight' Roll over at midnight - change_log_level (dict) :A dictionary of handlers with corresponding log-level ( for eg. {'requests':'warning'} ) - console_log_level (dict) :Change the LogLevel of console log handler, default is INFO - gelf_handler :An external handler for graylog data publishing. + log_directory (str) :directory to write log files to. Applicable only when `allow_file_logging` = True + file_handler_type :object of logging handler from HandlerType class. Applicable only when `allow_file_logging` = True + allow_console_logging (bool) :Turn off/on the console logging. + allow_file_logging (bool) :Turn off/on if logs need to go in files as well. + backup_count (int) :Number of files to backup before rotating the logs. + max_file_size_bytes (int) :Size of file in bytes before rotating the file. Applicable only to ROTATING_FILE_HANDLER. + when_to_rotate (str) :Duration after which a file can be rotated. Applicable only to TIME_ROTATING_FILE_HANDLER + Accepts following values: + 'S' Seconds + 'M' Minutes + 'H' Hours + 'D' Days + 'W0'-'W6' Weekday (0=Monday) + 'midnight' Roll over at midnight + change_log_level (dict) :A dictionary of handlers with corresponding log-level ( for eg. {'requests':'warning'} ) + console_log_level (logging) :Change the LogLevel of console log handler, default is logging.INFO (e.g. logging.DEBUG, logging.INFO) + gelf_handler :An external handler for graylog data publishing. + log_tags :Adding contextual information to a given log handler for e.g. {'app_name': 'My Perfect App'} diff --git a/pylogging/_create_logger.py b/pylogging/_create_logger.py index efe0013..fb4734d 100644 --- a/pylogging/_create_logger.py +++ b/pylogging/_create_logger.py @@ -30,7 +30,8 @@ def __setup_file_logging(g_logger=None, backup_count=100, max_file_size_bytes=10000, when_to_rotate='D', - log_formatter=Formatters.TextFormatter): + log_formatter=Formatters.TextFormatter, + log_filter=log_filter): """Attach logs to be written to disk if its required.""" generated_files = os.path.join(os.path.abspath(os.path.expanduser(log_directory))) if not os.path.exists(generated_files): @@ -48,7 +49,8 @@ def __setup_file_logging(g_logger=None, handler = logging.handlers.TimedRotatingFileHandler(error_logs_fname, when=when_to_rotate, backupCount=backup_count) - + if log_filter: + handler.addFilter(log_filter) handler.setLevel(logging.ERROR) handler.setFormatter(log_formatter) g_logger.addHandler(handler) @@ -62,7 +64,8 @@ def __setup_file_logging(g_logger=None, handler = logging.handlers.TimedRotatingFileHandler(all_logs_fname, when=when_to_rotate, backupCount=backup_count) - + if log_filter: + handler.addFilter(log_filter) handler.setLevel(logging.DEBUG) handler.setFormatter(log_formatter) g_logger.addHandler(handler) @@ -80,6 +83,7 @@ def setup_logger(log_directory='.', change_log_level=None, log_formatter=Formatters.TextFormatter, gelf_handler=None, + log_tags=None, **kwargs): """Set up the global logging settings. @@ -101,6 +105,7 @@ def setup_logger(log_directory='.', change_log_level (dict) :A dictionary of handlers with corresponding log-level ( for eg. {'requests':'warning'} ) console_log_level (logging) :Change the LogLevel of console log handler, default is logging.INFO (e.g. logging.DEBUG, logging.INFO) gelf_handler :An external handler for graylog data publishing. + log_tags :Adding contextual information to a given log handler for e.g. {'app_name': 'My Perfect App'} """ file_handlers = [HandlerType.ROTATING_FILE_HANDLER, HandlerType.TIME_ROTATING_FILE_HANDLER] if file_handler_type not in file_handlers: @@ -109,11 +114,24 @@ def setup_logger(log_directory='.', if change_log_level: __set_log_levels(change_log_level) + # Check if log_tags is not defined, initialize it to empty dict + if not log_tags: + log_tags = {} + + class AppFilter(logging.Filter): + def filter(self, record): + for key, value in log_tags.items(): + setattr(record, key, value) + return True + + log_filter = AppFilter() if log_tags else None logger = logging.getLogger() logger.propagate = False logger.setLevel(logging.DEBUG) if gelf_handler: + if log_filter: + gelf_handler.addFilter(log_filter) logger.addHandler(gelf_handler) # create console handler and set level to info @@ -122,6 +140,8 @@ def setup_logger(log_directory='.', log_level = kwargs.get("console_log_level", logging.INFO) handler.setLevel(log_level) handler.setFormatter(log_formatter) + if log_filter: + handler.addFilter(log_filter) logger.addHandler(handler) if allow_file_logging: @@ -130,4 +150,5 @@ def setup_logger(log_directory='.', file_handler_type=file_handler_type, backup_count=backup_count, max_file_size_bytes=max_file_size_bytes, - when_to_rotate=when_to_rotate) + when_to_rotate=when_to_rotate, + log_filter=log_filter) diff --git a/setup.cfg b/setup.cfg index e69de29..d7b63e9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -0,0 +1,5 @@ +[pep8] +max-line-length=120 + +[flake8] +ignore = N801,N802,N803,W503,E12 From 50f768aa01048a8c5737b1efb09d678e1707e8c3 Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Wed, 14 Nov 2018 14:53:12 +0100 Subject: [PATCH 2/5] bump version --- pylogging/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylogging/__init__.py b/pylogging/__init__.py index 753a4b8..9e5e8a4 100644 --- a/pylogging/__init__.py +++ b/pylogging/__init__.py @@ -6,4 +6,4 @@ from .handler_types import HandlerType from .formatters import Formatters -__version__ = '0.2.3' +__version__ = '0.2.5' From c63bbbd6c2c636bf43488cceb987c27707f7d368 Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Wed, 14 Nov 2018 15:01:53 +0100 Subject: [PATCH 3/5] fixed missing param --- pylogging/_create_logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylogging/_create_logger.py b/pylogging/_create_logger.py index fb4734d..cfa84eb 100644 --- a/pylogging/_create_logger.py +++ b/pylogging/_create_logger.py @@ -31,7 +31,7 @@ def __setup_file_logging(g_logger=None, max_file_size_bytes=10000, when_to_rotate='D', log_formatter=Formatters.TextFormatter, - log_filter=log_filter): + log_filter=None): """Attach logs to be written to disk if its required.""" generated_files = os.path.join(os.path.abspath(os.path.expanduser(log_directory))) if not os.path.exists(generated_files): From 51110164bbf27eca62af9c3129ffb80ea02515a2 Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Wed, 14 Nov 2018 16:37:04 +0100 Subject: [PATCH 4/5] updated max-line-length --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index d7b63e9..bd18218 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,3 +3,4 @@ max-line-length=120 [flake8] ignore = N801,N802,N803,W503,E12 +max-line-length=120 From 833edb811b802b527bd979fac44990002d2d7c4b Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Fri, 28 Dec 2018 11:02:19 +0100 Subject: [PATCH 5/5] new relese --- README.rst | 2 +- pylogging/formatters.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 9a07062..c353ad4 100644 --- a/README.rst +++ b/README.rst @@ -7,7 +7,7 @@ configs. Compatible with: ~~~~~~~~~~~~~~~~ -Python 2.7 and 3.5 +Python 2.7 and 3.5+ Current stable version: ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/pylogging/formatters.py b/pylogging/formatters.py index e63457b..1c290d2 100644 --- a/pylogging/formatters.py +++ b/pylogging/formatters.py @@ -21,7 +21,7 @@ def format(self, record): line_number = "%s" % (record.lineno) location_line = error_location[:32] + ":" + line_number s = "%.19s [%-8s] [%-36s] %s" % (self.formatTime(record, self.datefmt), - record.levelname, location_line, record.getMessage()) + record.levelname, location_line, record.getMessage()) return s