Skip to content

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankur Srivastava committed Aug 4, 2017
2 parents 7f5f226 + a9a90df commit 9a8cde7
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 57 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A simple python logger which writes logs to disk with some default configs.
Python 2.7 and 3.5

### Current stable version:
0.1.0
0.2.0

### Installation:

Expand Down Expand Up @@ -56,19 +56,20 @@ if __name__ == '__main__':
### 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'} )
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.
```
17 changes: 17 additions & 0 deletions examples/elastic-search/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""Initialize module utils."""

from send_to_elastic_search import Log
from pylogging import HandlerType, setup_logger

logger = Log(__name__)

if __name__ == '__main__':
setup_logger(log_directory='./logs', file_handler_type=HandlerType.TIME_ROTATING_FILE_HANDLER, allow_console_logging=True,
backup_count=100, max_file_size_bytes=10000, when_to_rotate='D', change_log_level=None, allow_file_logging=False)

logger.debug("hello debug", "this is my message")
logger.error("hello error", "this doesn't look to be mine")
logger.info("hello info", 1, 2, 3)
logger.exception("hello exception", {"efficiency": 20})
43 changes: 43 additions & 0 deletions examples/elastic-search/send_to_elastic_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""Publish logs to elastic search."""

from pylogging.elogger import ElasticLoggger
import logging
ellog = ElasticLoggger(tag="my_app", elastic_url="http://localhost:3332", auth=('myuser', 'mypassword'))


class Log(object):
"""Logger object."""

def __init__(self, module_name):
"""Pass the logger NAME i.e. __name__ object here for each module: `logger = logging.getLogger(__name__)` ."""
self.logger = logging.getLogger(name=module_name)

def stringify(self, *args):
"""Handle multiple arguments and return them as a string format."""
return(",".join(str(x) for x in args))

def debug(self, *args):
"""Debug logs."""
msg = self.stringify(args)
self.logger.debug(msg)
ellog.debug(msg)

def error(self, *args):
"""Error logs."""
msg = self.stringify(args)
self.logger.error(msg)
ellog.error(msg)

def exception(self, *args):
"""Exception logs."""
msg = self.stringify(args)
self.logger.exception(msg)
ellog.exception(msg)

def info(self, *args):
"""Info logs."""
msg = self.stringify(args)
self.logger.info(msg)
ellog.info(msg)
32 changes: 32 additions & 0 deletions examples/simple_gelf_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""Simple example to use with Gelf logging module."""


import logging

from pylogging import HandlerType, setup_logger
from graypy import GELFHandler
logger = logging.getLogger(__name__)

# If want to add extra fields.
# logger = logging.LoggerAdapter(logger, {"app_name": "test-service"})
if __name__ == '__main__':
gelf_handler = GELFHandler(host="localhost",
port=12201,
level_names=True,
debugging_fields=False)

setup_logger(log_directory='./logs',
file_handler_type=HandlerType.TIME_ROTATING_FILE_HANDLER,
allow_console_logging=True,
allow_file_logging=True,
backup_count=100,
max_file_size_bytes=100000,
when_to_rotate='D',
change_log_level=None,
gelf_handler=gelf_handler)

logger.error("Error logs")
logger.debug("Debug logs")
logger.info("Info logs")
3 changes: 2 additions & 1 deletion pylogging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

from .create_logger import setup_logger
from .handler_types import HandlerType
from .formatters import Formatters

__version__ = '0.1.0'
__version__ = '0.2.0'
63 changes: 28 additions & 35 deletions pylogging/create_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,7 @@

from pylogging.handler_types import HandlerType
from pylogging.log_levels import LogLevel


class LogFormatter(logging.Formatter):
"""Format the meta data in the log message to fix string length."""

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

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())
return s
from pylogging.formatters import Formatters


def __set_log_levels(level_dict):
Expand All @@ -43,7 +29,8 @@ def __setup_file_logging(g_logger=None,
file_handler_type=HandlerType.TIME_ROTATING_FILE_HANDLER,
backup_count=100,
max_file_size_bytes=10000,
when_to_rotate='D'):
when_to_rotate='D',
log_formatter=Formatters.TextFormatter):
"""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):
Expand All @@ -62,7 +49,7 @@ def __setup_file_logging(g_logger=None,
backupCount=backup_count)

handler.setLevel(logging.ERROR)
handler.setFormatter(LogFormatter())
handler.setFormatter(log_formatter)
g_logger.addHandler(handler)

# create debug file handler and set level to debug
Expand All @@ -76,7 +63,7 @@ def __setup_file_logging(g_logger=None,
backupCount=backup_count)

handler.setLevel(logging.DEBUG)
handler.setFormatter(LogFormatter())
handler.setFormatter(log_formatter)
g_logger.addHandler(handler)

print('Logging into directory {}\n'.format(generated_files))
Expand All @@ -89,25 +76,28 @@ def setup_logger(log_directory='.',
backup_count=100,
max_file_size_bytes=100000,
when_to_rotate='D',
change_log_level=None):
change_log_level=None,
log_formatter=Formatters.TextFormatter,
gelf_handler=None):
"""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
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'} )
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.
"""
if file_handler_type not in [HandlerType.ROTATING_FILE_HANDLER,
HandlerType.TIME_ROTATING_FILE_HANDLER]:
Expand All @@ -119,11 +109,14 @@ def setup_logger(log_directory='.',
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

if gelf_handler:
logger.addHandler(gelf_handler)

# create console handler and set level to info
if allow_console_logging:
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
handler.setFormatter(LogFormatter())
handler.setFormatter(log_formatter)
logger.addHandler(handler)

if allow_file_logging:
Expand Down
49 changes: 49 additions & 0 deletions pylogging/formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""Bunch of log formatters to be used."""

import logging

try:
import ujson as json
except Exception as ex:
import json


class TextFormatter(logging.Formatter):
"""Format the meta data in the log message to fix string length."""

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

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())
return s


class JsonFormatter(logging.Formatter):
"""Format the meta data in the json log message and fix string length."""

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

def format(self, record):
"""Default json formatter."""
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),
'log_location': location_line,
'log_level': record.levelname,
'message': record.getMessage()}
return json.dumps(output)


class Formatters(object):
"""Define a common class for Formatters."""

TextFormatter = TextFormatter()
JsonFormatter = JsonFormatter()
4 changes: 2 additions & 2 deletions pylogging/log_levels.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""Module to manage different log levels based on different strings."""
"""Module to set log level."""

import logging

from future.utils import raise_with_traceback as rwt


class LogLevel(object):
"""Manage loglevels based on different strings."""
"""Log levels definition."""

levels = {
'DEBUG': logging.DEBUG,
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from setuptools import find_packages, setup

setup(name='pylogging',
version='0.1.0',
version='0.2.0',
description='File logging for Python',
author='Ankur Srivastava',
author_email='[email protected]',
url='https://github.com/ansrivas/pylogging',
download_url='https://github.com/ansrivas/pylogging/tarball/0.1.0',
download_url='https://github.com/ansrivas/pylogging/tarball/0.2.0',
license='MIT',
install_requires=['future', 'requests', 'requests-futures'],
install_requires=['future', 'requests', 'requests-futures', 'ujson', 'graypy'],
packages=find_packages())

0 comments on commit 9a8cde7

Please sign in to comment.