Skip to content

Commit

Permalink
Merge pull request #7 from ansrivas/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ansrivas committed Dec 28, 2018
2 parents e77e653 + aca0a09 commit bf2a833
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 62 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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.

# tag:0.2.4 / 2018-10-09
- Code Refactor
- Introduced a new parameter `file_log_level` which will decide what information should go to different files.

43 changes: 22 additions & 21 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ configs.
Compatible with:
~~~~~~~~~~~~~~~~

Python 2.7 and 3.5
Python 2.7 and 3.5+

Current stable version:
~~~~~~~~~~~~~~~~~~~~~~~

::

0.2.3
0.2.5

Installation:
~~~~~~~~~~~~~
Expand All @@ -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``
Expand Down Expand Up @@ -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'}
5 changes: 3 additions & 2 deletions examples/simple_gelf_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# If want to add extra fields.
# logger = logging.LoggerAdapter(logger, {"app_name": "test-service"})
if __name__ == '__main__':
gelf_handler = GELFHandler(host="localhost",
gelf_handler = GELFHandler(host="elk.recogizer.net",
port=12201,
level_names=True,
debugging_fields=False)
Expand All @@ -25,7 +25,8 @@
max_file_size_bytes=100000,
when_to_rotate='D',
change_log_level=None,
gelf_handler=gelf_handler)
gelf_handler=gelf_handler,
log_tags={"app_name": "Test-App"},)

logger.error("Error logs")
logger.debug("Debug logs")
Expand Down
2 changes: 1 addition & 1 deletion pylogging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from .handler_types import HandlerType
from .formatters import Formatters

__version__ = '0.2.4'
__version__ = '0.2.5'
84 changes: 49 additions & 35 deletions pylogging/_create_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,45 @@ def __setup_file_logging(g_logger=None,
max_file_size_bytes=10000,
when_to_rotate='D',
log_formatter=Formatters.TextFormatter,
file_log_level=logging.DEBUG):
file_log_level=logging.DEBUG,
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):
os.makedirs(generated_files)

all_logs_fname = '{0}/all.log'.format(generated_files)
info_logs_fname = '{0}/info.log'.format(generated_files)
error_logs_fname = '{0}/error.log'.format(generated_files)

def __add_handlers_to_global_logger(file_handler_type, fname, log_level):
"""Add different file handlers to global logger.
By default three types of files are generated in logs.
- all.log will contain all the log levels i.e. including DEBUG
- info.log will contain all the log level above and including INFO
- error.log will contain all the log level which are above and including ERROR
"""
# create error file handler and set level to error
if file_handler_type == HandlerType.ROTATING_FILE_HANDLER:
handler = logging.handlers.RotatingFileHandler(fname,
maxBytes=max_file_size_bytes,
backupCount=backup_count)
else:
handler = logging.handlers.TimedRotatingFileHandler(fname,
when=when_to_rotate,
backupCount=backup_count)

handler.setLevel(log_level)
handler.setFormatter(log_formatter)
g_logger.addHandler(handler)

if file_log_level == logging.DEBUG:
__add_handlers_to_global_logger(file_handler_type, error_logs_fname, logging.ERROR)
__add_handlers_to_global_logger(file_handler_type, all_logs_fname, logging.DEBUG)
__add_handlers_to_global_logger(file_handler_type, info_logs_fname, logging.INFO)
elif file_log_level == logging.INFO:
__add_handlers_to_global_logger(file_handler_type, error_logs_fname, logging.ERROR)
__add_handlers_to_global_logger(file_handler_type, info_logs_fname, logging.INFO)
elif file_log_level == logging.WARNING:
__add_handlers_to_global_logger(file_handler_type, error_logs_fname, logging.WARNING)
# create error file handler and set level to error
if file_handler_type == HandlerType.ROTATING_FILE_HANDLER:
handler = logging.handlers.RotatingFileHandler(error_logs_fname,
maxBytes=max_file_size_bytes,
backupCount=backup_count)
else:
__add_handlers_to_global_logger(file_handler_type, error_logs_fname, logging.ERROR)
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)

# create debug file handler and set level to debug
if file_handler_type == HandlerType.ROTATING_FILE_HANDLER:
handler = logging.handlers.RotatingFileHandler(all_logs_fname,
maxBytes=max_file_size_bytes,
backupCount=backup_count)
else:
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)

print('Logging into directory {}\n'.format(generated_files))

Expand All @@ -89,6 +85,7 @@ def setup_logger(log_directory='.',
log_formatter=Formatters.TextFormatter,
gelf_handler=None,
file_log_level=logging.DEBUG,
log_tags=None,
**kwargs):
"""Set up the global logging settings.
Expand Down Expand Up @@ -118,6 +115,7 @@ def setup_logger(log_directory='.',
file_log_level (logging) :Change the LogLevel of file log handler, default is logging.DEBUG
(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:
Expand All @@ -126,11 +124,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
Expand All @@ -139,6 +150,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:
Expand All @@ -148,4 +161,5 @@ def setup_logger(log_directory='.',
backup_count=backup_count,
max_file_size_bytes=max_file_size_bytes,
when_to_rotate=when_to_rotate,
file_log_level=file_log_level)
file_log_level=file_log_level,
log_filter=log_filter)
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[pep8]
max-line-length=119
max-line-length=120

[flake8]
ignore = N801,N802,N803,W503,E12
max-line-length=119
max-line-length=120

0 comments on commit bf2a833

Please sign in to comment.