From c561138990b69059b1f5b6314a052d14c2d77b35 Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Thu, 30 Nov 2017 14:12:46 +0100 Subject: [PATCH 1/6] code changes + turn off log propagation --- .gitignore | 2 + MANIFEST.in | 1 + README.rst | 93 +++++++++++++++++++ pylogging/__init__.py | 6 +- .../{create_logger.py => _create_logger.py} | 24 ++--- setup.cfg | 0 setup.py | 40 +++++++- 7 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 MANIFEST.in create mode 100644 README.rst rename pylogging/{create_logger.py => _create_logger.py} (94%) create mode 100644 setup.cfg diff --git a/.gitignore b/.gitignore index 113294a..0ef119d 100644 --- a/.gitignore +++ b/.gitignore @@ -97,3 +97,5 @@ ENV/ # mypy .mypy_cache/ + +README.md diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..9561fb1 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.rst diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..126c323 --- /dev/null +++ b/README.rst @@ -0,0 +1,93 @@ +pylogging +~~~~~~~~~ + +A simple python logger which writes logs to disk with some default +configs. + +Compatible with: +~~~~~~~~~~~~~~~~ + +Python 2.7 and 3.5 + +Current stable version: +~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + 0.2.0 + +Installation: +~~~~~~~~~~~~~ + +Install using pip +^^^^^^^^^^^^^^^^^ + +:: + + pip install git+https://github.com/ansrivas/pylogging.git --upgrade + +Install by adding to requirements.txt of your project +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Add the following lines to your ``requirements.txt`` file. + ``git+https://github.com/ansrivas/pylogging.git`` + +- Install all packages in your ``requirements.txt`` file by running the + command: ``$ pip install -r requirements.txt`` + +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'],`` + +- 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'],`` + +- Install your project along with ``pylogging`` by running the command: + ``python setup.py install`` + +Usage: +~~~~~~ + +- ``setup_logger`` sets up the global logger with the provided + settings. After calling it once, simply ``import logging`` and create + a logger for that module ``logger = logging.getLogger(__name__)`` and + use it at shown below. + +:: + + from pylogging import HandlerType, setup_logger + import logging + + logger = logging.getLogger(__name__) + + if __name__ == '__main__': + setup_logger(log_directory='./logs', file_handler_type=HandlerType.ROTATING_FILE_HANDLER, allow_console_logging=True) + + logger.error("Error logs") + logger.debug("Debug logs") + logger.info("Info logs") + +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'} ) + gelf_handler :An external handler for graylog data publishing. diff --git a/pylogging/__init__.py b/pylogging/__init__.py index f2bfb09..753a4b8 100644 --- a/pylogging/__init__.py +++ b/pylogging/__init__.py @@ -1,9 +1,9 @@ # !/usr/bin/env python # -*- coding: utf-8 -*- """Initialize module utils.""" - -from .create_logger import setup_logger +__all__ = ['setup_logger', 'HandlerType', 'Formatters'] +from ._create_logger import setup_logger from .handler_types import HandlerType from .formatters import Formatters -__version__ = '0.2.0' +__version__ = '0.2.3' diff --git a/pylogging/create_logger.py b/pylogging/_create_logger.py similarity index 94% rename from pylogging/create_logger.py rename to pylogging/_create_logger.py index a8be39f..229261f 100644 --- a/pylogging/create_logger.py +++ b/pylogging/_create_logger.py @@ -2,26 +2,26 @@ # -*- coding: utf-8 -*- """Docstring for logging module.""" +import os import logging import logging.handlers -import os -from future.utils import raise_with_traceback as rwt -from future.utils import iteritems - -from pylogging.handler_types import HandlerType from pylogging.log_levels import LogLevel from pylogging.formatters import Formatters +from pylogging.handler_types import HandlerType + +from future.utils import iteritems +from future.utils import raise_with_traceback as rwt def __set_log_levels(level_dict): """Set the log levels for any log-handler for e.g. level_dict = {'requests':'error'}.""" if not isinstance(level_dict, dict): - rwt(TypeError('Expecting dict object with format: \{\'requests\':\'warning\'\} \n' + + rwt(TypeError('Expecting dict object with format: \{\'requests\':\'warning\'\} \n' 'Available levels are: {0}'.format(LogLevel.levels.keys))) - else: - for key, val in iteritems(level_dict): - logging.getLogger(key).setLevel(LogLevel.get_level(val)) + + for key, val in iteritems(level_dict): + logging.getLogger(key).setLevel(LogLevel.get_level(val)) def __setup_file_logging(g_logger=None, @@ -35,6 +35,7 @@ def __setup_file_logging(g_logger=None, generated_files = os.path.join(os.path.abspath(os.path.expanduser(log_directory))) if not os.path.exists(generated_files): os.makedirs(generated_files) + all_logs_fname = '{0}/all.log'.format(generated_files) error_logs_fname = '{0}/error.log'.format(generated_files) @@ -99,14 +100,15 @@ def setup_logger(log_directory='.', change_log_level (dict) :A dictionary of handlers with corresponding log-level ( for eg. {'requests':'warning'} ) gelf_handler :An external handler for graylog data publishing. """ - if file_handler_type not in [HandlerType.ROTATING_FILE_HANDLER, - HandlerType.TIME_ROTATING_FILE_HANDLER]: + file_handlers = [HandlerType.ROTATING_FILE_HANDLER, HandlerType.TIME_ROTATING_FILE_HANDLER] + if file_handler_type not in file_handlers: rwt(ValueError('Please pass an object of HandlerType class')) if change_log_level: __set_log_levels(change_log_level) logger = logging.getLogger() + logger.propagate = False logger.setLevel(logging.DEBUG) if gelf_handler: diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index 5f39bef..f33424a 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,46 @@ +import re +from os import path + +from codecs import open # To use a consistent encoding from setuptools import find_packages, setup +here = path.abspath(path.dirname(__file__)) + +# Get the long description from the relevant file +with open(path.join(here, 'README.rst'), encoding='utf-8') as f: + long_description = f.read() + + +def get_version(): + with open('pylogging/__init__.py') as version_file: + return re.search(r"""__version__\s+=\s+(['"])(?P.+?)\1""", + version_file.read()).group('version') + + setup(name='pylogging', - version='0.2.0', + version=get_version(), description='File logging for Python', + long_description=long_description, author='Ankur Srivastava', author_email='best.ankur@gmail.com', url='https://github.com/ansrivas/pylogging', - download_url='https://github.com/ansrivas/pylogging/tarball/0.2.0', + download_url='https://github.com/ansrivas/pylogging/tarball/{0}'.format(get_version()), + include_package_data=True, license='MIT', - install_requires=['future', 'requests', 'requests-futures', 'ujson', 'graypy'], + zip_safe=False, + install_requires=['future', 'requests-futures', 'ujson', 'graypy'], + extras_require={ + 'dev': [ + 'pytest', + 'pytest-pep8', + 'pytest-cov', + ] + }, + classifiers=[ + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", ], packages=find_packages()) From e35ce2bcbd3b7b447e6be66a90f7b9322bdd4970 Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Thu, 30 Nov 2017 14:13:48 +0100 Subject: [PATCH 2/6] removed .md --- README.md | 75 ------------------------------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 88d727b..0000000 --- a/README.md +++ /dev/null @@ -1,75 +0,0 @@ -### pylogging - -A simple python logger which writes logs to disk with some default configs. - -### Compatible with: -Python 2.7 and 3.5 - -### Current stable version: - 0.2.0 - -### Installation: - -#### Install using pip -``` -pip install git+https://github.com/ansrivas/pylogging.git --upgrade -``` - -#### Install by adding to requirements.txt of your project -* Add the following lines to your `requirements.txt` file. -`git+https://github.com/ansrivas/pylogging.git` - -* Install all packages in your `requirements.txt` file by running the command: -`$ pip install -r requirements.txt` - - -#### 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'],` - -* 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'],` - -* Install your project along with `pylogging` by running the command: -`python setup.py install` - - -### Usage: - -* `setup_logger` sets up the global logger with the provided settings. After calling it once, simply `import logging` and create a logger for that - module `logger = logging.getLogger(__name__)` and use it at shown below. - -``` -from pylogging import HandlerType, setup_logger -import logging - -logger = logging.getLogger(__name__) - -if __name__ == '__main__': - setup_logger(log_directory='./logs', file_handler_type=HandlerType.ROTATING_FILE_HANDLER, allow_console_logging=True) - - logger.error("Error logs") - logger.debug("Debug logs") - logger.info("Info logs") -``` - -### 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'} ) -gelf_handler :An external handler for graylog data publishing. -``` From 7ed2dea4f286c13a9f42b453a2a693379f20685f Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Sat, 31 Mar 2018 16:14:31 +0200 Subject: [PATCH 3/6] updated log info --- pylogging/_create_logger.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pylogging/_create_logger.py b/pylogging/_create_logger.py index 229261f..998a8b9 100644 --- a/pylogging/_create_logger.py +++ b/pylogging/_create_logger.py @@ -79,7 +79,8 @@ def setup_logger(log_directory='.', when_to_rotate='D', change_log_level=None, log_formatter=Formatters.TextFormatter, - gelf_handler=None): + gelf_handler=None, + **kwargs): """Set up the global logging settings. Args: @@ -98,6 +99,7 @@ def setup_logger(log_directory='.', '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. """ file_handlers = [HandlerType.ROTATING_FILE_HANDLER, HandlerType.TIME_ROTATING_FILE_HANDLER] @@ -117,7 +119,8 @@ def setup_logger(log_directory='.', # create console handler and set level to info if allow_console_logging: handler = logging.StreamHandler() - handler.setLevel(logging.INFO) + log_level = kwargs.get("console_log_level", logging.INFO) + handler.setLevel(log_level) handler.setFormatter(log_formatter) logger.addHandler(handler) From 52a083c90cf22c8d3e08788651ed853723ff9641 Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Sat, 31 Mar 2018 17:00:56 +0200 Subject: [PATCH 4/6] updated version --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 126c323..e16a965 100644 --- a/README.rst +++ b/README.rst @@ -14,7 +14,7 @@ Current stable version: :: - 0.2.0 + 0.2.3 Installation: ~~~~~~~~~~~~~ From 27e17ca2ce68e80b2f03b00d958efbeae88b03fb Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Sat, 31 Mar 2018 17:01:52 +0200 Subject: [PATCH 5/6] updated readme --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index e16a965..fc3e99b 100644 --- a/README.rst +++ b/README.rst @@ -90,4 +90,5 @@ Important arguments to ``setup_logger`` function: '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. From 3ce805959e8414968d50d187fdcb5a62f8a1aa72 Mon Sep 17 00:00:00 2001 From: Ankur Srivastava Date: Sat, 31 Mar 2018 21:03:42 +0200 Subject: [PATCH 6/6] updated docs --- 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 998a8b9..efe0013 100644 --- a/pylogging/_create_logger.py +++ b/pylogging/_create_logger.py @@ -99,7 +99,7 @@ def setup_logger(log_directory='.', '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 + 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. """ file_handlers = [HandlerType.ROTATING_FILE_HANDLER, HandlerType.TIME_ROTATING_FILE_HANDLER]