Skip to content

Commit

Permalink
Merge pull request #6 from ansrivas/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ansrivas authored Oct 9, 2018
2 parents e4ef0a8 + 57c8c74 commit e77e653
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 57 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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.4 / 2018-10-09
- Code Refactor
- Introduced a new parameter `file_log_level` which will decide what information should go to different files.

24 changes: 8 additions & 16 deletions docs/source/pylogging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,32 @@ pylogging package
Submodules
----------

pylogging\.create\_logger module
--------------------------------

.. automodule:: pylogging.create_logger
:members:
:undoc-members:
:show-inheritance:

pylogging\.elogger module
-------------------------
pylogging.elogger module
------------------------

.. automodule:: pylogging.elogger
:members:
:undoc-members:
:show-inheritance:

pylogging\.formatters module
----------------------------
pylogging.formatters module
---------------------------

.. automodule:: pylogging.formatters
:members:
:undoc-members:
:show-inheritance:

pylogging\.handler\_types module
--------------------------------
pylogging.handler\_types module
-------------------------------

.. automodule:: pylogging.handler_types
:members:
:undoc-members:
:show-inheritance:

pylogging\.log\_levels module
-----------------------------
pylogging.log\_levels module
----------------------------

.. automodule:: pylogging.log_levels
:members:
Expand Down
3 changes: 2 additions & 1 deletion examples/simple_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
logger = logging.getLogger(__name__)

if __name__ == '__main__':
setup_logger(log_directory='./logs', file_handler_type=HandlerType.ROTATING_FILE_HANDLER, allow_console_logging=True)
setup_logger(log_directory='./logs', file_handler_type=HandlerType.ROTATING_FILE_HANDLER,
allow_console_logging=True, file_log_level=logging.INFO)

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.3'
__version__ = '0.2.4'
90 changes: 54 additions & 36 deletions pylogging/_create_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
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'
'Available levels are: {0}'.format(LogLevel.levels.keys)))
rwt(TypeError("Expecting dict object with format for e.g. {'requests':'warning'} \n"
"Available levels are: {0}".format(LogLevel.levels.keys)))

for key, val in iteritems(level_dict):
logging.getLogger(key).setLevel(LogLevel.get_level(val))
Expand All @@ -30,42 +30,50 @@ 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,
file_log_level=logging.DEBUG):
"""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)

# 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:
handler = logging.handlers.TimedRotatingFileHandler(error_logs_fname,
when=when_to_rotate,
backupCount=backup_count)

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)
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(logging.DEBUG)
handler.setFormatter(log_formatter)
g_logger.addHandler(handler)
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)
else:
__add_handlers_to_global_logger(file_handler_type, error_logs_fname, logging.ERROR)

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

Expand All @@ -80,26 +88,35 @@ def setup_logger(log_directory='.',
change_log_level=None,
log_formatter=Formatters.TextFormatter,
gelf_handler=None,
file_log_level=logging.DEBUG,
**kwargs):
"""Set up the global logging settings.
Args:
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
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
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)
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)
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.
"""
file_handlers = [HandlerType.ROTATING_FILE_HANDLER, HandlerType.TIME_ROTATING_FILE_HANDLER]
Expand Down Expand Up @@ -130,4 +147,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,
file_log_level=file_log_level)
15 changes: 12 additions & 3 deletions pylogging/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ class TextFormatter(logging.Formatter):

datefmt = '%Y-%m-%d %H:%M:%S'

def __init__(self, context=None):
self.context = context
super(TextFormatter, self).__init__()

def format(self, record):
"""Default formatter."""
error_location = "%s.%s" % (record.name, record.funcName)
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())

s = "%.19s [%-8s] [%-36s] %s" % (self.formatTime(record, TextFormatter.datefmt),
record.levelname, location_line, record.getMessage())
if self.context:
s = "%.19s [%s] [%-8s] [%-36s] %s" % (self.formatTime(record, TextFormatter.datefmt), self.context,
record.levelname, location_line, record.getMessage())

return s


Expand All @@ -35,7 +44,7 @@ def format(self, record):
error_location = "%s.%s" % (record.name, record.funcName)
line_number = "%s" % (record.lineno)
location_line = error_location[:32] + ":" + line_number
output = {'log_time': self.formatTime(record, self.datefmt),
output = {'log_time': self.formatTime(record, TextFormatter.datefmt),
'log_location': location_line,
'log_level': record.levelname,
'message': record.getMessage()}
Expand Down
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[pep8]
max-line-length=119

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

0 comments on commit e77e653

Please sign in to comment.