-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
193 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |