python-json-logger-0.1.11/0000755000175000017500000000000013447466063016772 5ustar zakzajaczakzajac00000000000000python-json-logger-0.1.11/LICENSE0000644000175000017500000000241213144070056017761 0ustar zakzajaczakzajac00000000000000Copyright (c) 2011, Zakaria Zajac All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. python-json-logger-0.1.11/tests/0000755000175000017500000000000013447466063020134 5ustar zakzajaczakzajac00000000000000python-json-logger-0.1.11/tests/tests.py0000644000175000017500000001517213370564422021647 0ustar zakzajaczakzajac00000000000000# -*- coding: utf-8 -*- import unittest import logging import json import sys import traceback try: import xmlrunner except ImportError: pass try: from StringIO import StringIO except ImportError: # Python 3 Support from io import StringIO sys.path.append('src/python-json-logger') from pythonjsonlogger import jsonlogger import datetime class TestJsonLogger(unittest.TestCase): def setUp(self): self.logger = logging.getLogger('logging-test') self.logger.setLevel(logging.DEBUG) self.buffer = StringIO() self.logHandler = logging.StreamHandler(self.buffer) self.logger.addHandler(self.logHandler) def testDefaultFormat(self): fr = jsonlogger.JsonFormatter() self.logHandler.setFormatter(fr) msg = "testing logging format" self.logger.info(msg) logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson["message"], msg) def testFormatKeys(self): supported_keys = [ 'asctime', 'created', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'thread', 'threadName' ] log_format = lambda x: ['%({0:s})'.format(i) for i in x] custom_format = ' '.join(log_format(supported_keys)) fr = jsonlogger.JsonFormatter(custom_format) self.logHandler.setFormatter(fr) msg = "testing logging format" self.logger.info(msg) log_msg = self.buffer.getvalue() log_json = json.loads(log_msg) for supported_key in supported_keys: if supported_key in log_json: self.assertTrue(True) def testUnknownFormatKey(self): fr = jsonlogger.JsonFormatter('%(unknown_key)s %(message)s') self.logHandler.setFormatter(fr) msg = "testing unknown logging format" try: self.logger.info(msg) except: self.assertTrue(False, "Should succeed") def testLogADict(self): fr = jsonlogger.JsonFormatter() self.logHandler.setFormatter(fr) msg = {"text": "testing logging", "num": 1, 5: "9", "nested": {"more": "data"}} self.logger.info(msg) logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson.get("text"), msg["text"]) self.assertEqual(logJson.get("num"), msg["num"]) self.assertEqual(logJson.get("5"), msg[5]) self.assertEqual(logJson.get("nested"), msg["nested"]) self.assertEqual(logJson["message"], None) def testLogExtra(self): fr = jsonlogger.JsonFormatter() self.logHandler.setFormatter(fr) extra = {"text": "testing logging", "num": 1, 5: "9", "nested": {"more": "data"}} self.logger.info("hello", extra=extra) logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson.get("text"), extra["text"]) self.assertEqual(logJson.get("num"), extra["num"]) self.assertEqual(logJson.get("5"), extra[5]) self.assertEqual(logJson.get("nested"), extra["nested"]) self.assertEqual(logJson["message"], "hello") def testJsonDefaultEncoder(self): fr = jsonlogger.JsonFormatter() self.logHandler.setFormatter(fr) msg = {"adate": datetime.datetime(1999, 12, 31, 23, 59), "otherdate": datetime.date(1789, 7, 14), "otherdatetime": datetime.datetime(1789, 7, 14, 23, 59), "otherdatetimeagain": datetime.datetime(1900, 1, 1)} self.logger.info(msg) logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson.get("adate"), "1999-12-31T23:59:00") self.assertEqual(logJson.get("otherdate"), "1789-07-14") self.assertEqual(logJson.get("otherdatetime"), "1789-07-14T23:59:00") self.assertEqual(logJson.get("otherdatetimeagain"), "1900-01-01T00:00:00") def testJsonCustomDefault(self): def custom(o): return "very custom" fr = jsonlogger.JsonFormatter(json_default=custom) self.logHandler.setFormatter(fr) msg = {"adate": datetime.datetime(1999, 12, 31, 23, 59), "normal": "value"} self.logger.info(msg) logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson.get("adate"), "very custom") self.assertEqual(logJson.get("normal"), "value") def testJsonCustomLogicAddsField(self): class CustomJsonFormatter(jsonlogger.JsonFormatter): def process_log_record(self, log_record): log_record["custom"] = "value" # Old Style "super" since Python 2.6's logging.Formatter is old # style return jsonlogger.JsonFormatter.process_log_record(self, log_record) self.logHandler.setFormatter(CustomJsonFormatter()) self.logger.info("message") logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson.get("custom"), "value") def testExcInfo(self): fr = jsonlogger.JsonFormatter() self.logHandler.setFormatter(fr) try: raise Exception('test') except Exception: self.logger.exception("hello") expected_value = traceback.format_exc() # Formatter removes trailing new line if expected_value.endswith('\n'): expected_value = expected_value[:-1] logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson.get("exc_info"), expected_value) def testEnsureAsciiTrue(self): fr = jsonlogger.JsonFormatter() self.logHandler.setFormatter(fr) self.logger.info('Привет') msg = self.buffer.getvalue().split('"message": "', 1)[1].split('"', 1)[0] self.assertEqual(msg, r"\u041f\u0440\u0438\u0432\u0435\u0442") def testEnsureAsciiFalse(self): fr = jsonlogger.JsonFormatter(json_ensure_ascii=False) self.logHandler.setFormatter(fr) self.logger.info('Привет') msg = self.buffer.getvalue().split('"message": "', 1)[1].split('"', 1)[0] self.assertEqual(msg, "Привет") if __name__ == '__main__': if len(sys.argv[1:]) > 0: if sys.argv[1] == 'xml': testSuite = unittest.TestLoader().loadTestsFromTestCase( TestJsonLogger) xmlrunner.XMLTestRunner(output='reports').run(testSuite) else: unittest.main() python-json-logger-0.1.11/tests/__init__.py0000644000175000017500000000000013144070056022216 0ustar zakzajaczakzajac00000000000000python-json-logger-0.1.11/setup.cfg0000644000175000017500000000010313447466063020605 0ustar zakzajaczakzajac00000000000000[bdist_wheel] universal = 1 [egg_info] tag_build = tag_date = 0 python-json-logger-0.1.11/PKG-INFO0000644000175000017500000000162013447466063020066 0ustar zakzajaczakzajac00000000000000Metadata-Version: 1.2 Name: python-json-logger Version: 0.1.11 Summary: A python library adding a json log formatter Home-page: http://github.com/madzak/python-json-logger Author: Zakaria Zajac Author-email: zak@madzak.com License: BSD Description: UNKNOWN Platform: UNKNOWN Classifier: Development Status :: 3 - Alpha Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Topic :: System :: Logging Requires-Python: >=2.7 python-json-logger-0.1.11/setup.py0000644000175000017500000000222413447462201020472 0ustar zakzajaczakzajac00000000000000from setuptools import setup, find_packages setup( name="python-json-logger", version="0.1.11", url="http://github.com/madzak/python-json-logger", license="BSD", description="A python library adding a json log formatter", author="Zakaria Zajac", author_email="zak@madzak.com", package_dir={'': 'src'}, packages=find_packages("src", exclude="tests"), # https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires python_requires='>=2.7', test_suite="tests.tests", classifiers=[ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', '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', 'Programming Language :: Python :: 3.7', 'Topic :: System :: Logging', ] ) python-json-logger-0.1.11/MANIFEST.in0000644000175000017500000000005413144070056020512 0ustar zakzajaczakzajac00000000000000include LICENSE recursive-include tests *.pypython-json-logger-0.1.11/src/0000755000175000017500000000000013447466063017561 5ustar zakzajaczakzajac00000000000000python-json-logger-0.1.11/src/python_json_logger.egg-info/0000755000175000017500000000000013447466063025164 5ustar zakzajaczakzajac00000000000000python-json-logger-0.1.11/src/python_json_logger.egg-info/dependency_links.txt0000644000175000017500000000000113447466063031232 0ustar zakzajaczakzajac00000000000000 python-json-logger-0.1.11/src/python_json_logger.egg-info/top_level.txt0000644000175000017500000000002113447466063027707 0ustar zakzajaczakzajac00000000000000pythonjsonlogger python-json-logger-0.1.11/src/python_json_logger.egg-info/PKG-INFO0000644000175000017500000000162013447466063026260 0ustar zakzajaczakzajac00000000000000Metadata-Version: 1.2 Name: python-json-logger Version: 0.1.11 Summary: A python library adding a json log formatter Home-page: http://github.com/madzak/python-json-logger Author: Zakaria Zajac Author-email: zak@madzak.com License: BSD Description: UNKNOWN Platform: UNKNOWN Classifier: Development Status :: 3 - Alpha Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Topic :: System :: Logging Requires-Python: >=2.7 python-json-logger-0.1.11/src/python_json_logger.egg-info/SOURCES.txt0000644000175000017500000000050313447466063027046 0ustar zakzajaczakzajac00000000000000LICENSE MANIFEST.in setup.cfg setup.py src/python_json_logger.egg-info/PKG-INFO src/python_json_logger.egg-info/SOURCES.txt src/python_json_logger.egg-info/dependency_links.txt src/python_json_logger.egg-info/top_level.txt src/pythonjsonlogger/__init__.py src/pythonjsonlogger/jsonlogger.py tests/__init__.py tests/tests.pypython-json-logger-0.1.11/src/pythonjsonlogger/0000755000175000017500000000000013447466063023174 5ustar zakzajaczakzajac00000000000000python-json-logger-0.1.11/src/pythonjsonlogger/jsonlogger.py0000644000175000017500000001771113447465277025734 0ustar zakzajaczakzajac00000000000000''' This library is provided to allow standard python logging to output log data as JSON formatted strings ''' import logging import json import re from datetime import date, datetime, time import traceback import importlib from inspect import istraceback from collections import OrderedDict # skip natural LogRecord attributes # http://docs.python.org/library/logging.html#logrecord-attributes RESERVED_ATTRS = ( 'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'msg', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName') def merge_record_extra(record, target, reserved): """ Merges extra attributes from LogRecord object into target dictionary :param record: logging.LogRecord :param target: dict to update :param reserved: dict or list with reserved keys to skip """ for key, value in record.__dict__.items(): # this allows to have numeric keys if (key not in reserved and not (hasattr(key, "startswith") and key.startswith('_'))): target[key] = value return target class JsonEncoder(json.JSONEncoder): """ A custom encoder extending the default JSONEncoder """ def default(self, obj): if isinstance(obj, (date, datetime, time)): return self.format_datetime_obj(obj) elif istraceback(obj): return ''.join(traceback.format_tb(obj)).strip() elif type(obj) == Exception \ or isinstance(obj, Exception) \ or type(obj) == type: return str(obj) try: return super(JsonEncoder, self).default(obj) except TypeError: try: return str(obj) except Exception: return None def format_datetime_obj(self, obj): return obj.isoformat() class JsonFormatter(logging.Formatter): """ A custom formatter to format logging records as json strings. Extra values will be formatted as str() if not supported by json default encoder """ def __init__(self, *args, **kwargs): """ :param json_default: a function for encoding non-standard objects as outlined in http://docs.python.org/2/library/json.html :param json_encoder: optional custom encoder :param json_serializer: a :meth:`json.dumps`-compatible callable that will be used to serialize the log record. :param json_indent: an optional :meth:`json.dumps`-compatible numeric value that will be used to customize the indent of the output json. :param prefix: an optional string prefix added at the beginning of the formatted string :param json_indent: indent parameter for json.dumps :param json_ensure_ascii: ensure_ascii parameter for json.dumps :param reserved_attrs: an optional list of fields that will be skipped when outputting json log record. Defaults to all log record attributes: http://docs.python.org/library/logging.html#logrecord-attributes :param timestamp: an optional string/boolean field to add a timestamp when outputting the json log record. If string is passed, timestamp will be added to log record using string as key. If True boolean is passed, timestamp key will be "timestamp". Defaults to False/off. """ self.json_default = self._str_to_fn(kwargs.pop("json_default", None)) self.json_encoder = self._str_to_fn(kwargs.pop("json_encoder", None)) self.json_serializer = self._str_to_fn(kwargs.pop("json_serializer", json.dumps)) self.json_indent = kwargs.pop("json_indent", None) self.json_ensure_ascii = kwargs.pop("json_ensure_ascii", True) self.prefix = kwargs.pop("prefix", "") reserved_attrs = kwargs.pop("reserved_attrs", RESERVED_ATTRS) self.reserved_attrs = dict(zip(reserved_attrs, reserved_attrs)) self.timestamp = kwargs.pop("timestamp", False) # super(JsonFormatter, self).__init__(*args, **kwargs) logging.Formatter.__init__(self, *args, **kwargs) if not self.json_encoder and not self.json_default: self.json_encoder = JsonEncoder self._required_fields = self.parse() self._skip_fields = dict(zip(self._required_fields, self._required_fields)) self._skip_fields.update(self.reserved_attrs) def _str_to_fn(self, fn_as_str): """ If the argument is not a string, return whatever was passed in. Parses a string such as package.module.function, imports the module and returns the function. :param fn_as_str: The string to parse. If not a string, return it. """ if not isinstance(fn_as_str, str): return fn_as_str path, _, function = fn_as_str.rpartition('.') module = importlib.import_module(path) return getattr(module, function) def parse(self): """ Parses format string looking for substitutions This method is responsible for returning a list of fields (as strings) to include in all log messages. """ standard_formatters = re.compile(r'\((.+?)\)', re.IGNORECASE) return standard_formatters.findall(self._fmt) def add_fields(self, log_record, record, message_dict): """ Override this method to implement custom logic for adding fields. """ for field in self._required_fields: log_record[field] = record.__dict__.get(field) log_record.update(message_dict) merge_record_extra(record, log_record, reserved=self._skip_fields) if self.timestamp: key = self.timestamp if type(self.timestamp) == str else 'timestamp' log_record[key] = datetime.utcnow() def process_log_record(self, log_record): """ Override this method to implement custom logic on the possibly ordered dictionary. """ return log_record def jsonify_log_record(self, log_record): """Returns a json string of the log record.""" return self.json_serializer(log_record, default=self.json_default, cls=self.json_encoder, indent=self.json_indent, ensure_ascii=self.json_ensure_ascii) def format(self, record): """Formats a log record and serializes to json""" message_dict = {} if isinstance(record.msg, dict): message_dict = record.msg record.message = None else: record.message = record.getMessage() # only format time if needed if "asctime" in self._required_fields: record.asctime = self.formatTime(record, self.datefmt) # Display formatted exception, but allow overriding it in the # user-supplied dict. if record.exc_info and not message_dict.get('exc_info'): message_dict['exc_info'] = self.formatException(record.exc_info) if not message_dict.get('exc_info') and record.exc_text: message_dict['exc_info'] = record.exc_text # Display formatted record of stack frames # default format is a string returned from :func:`traceback.print_stack` try: if record.stack_info and not message_dict.get('stack_info'): message_dict['stack_info'] = self.formatStack(record.stack_info) except AttributeError: # Python2.7 doesn't have stack_info. pass try: log_record = OrderedDict() except NameError: log_record = {} self.add_fields(log_record, record, message_dict) log_record = self.process_log_record(log_record) return "%s%s" % (self.prefix, self.jsonify_log_record(log_record)) python-json-logger-0.1.11/src/pythonjsonlogger/__init__.py0000644000175000017500000000000013144070056025256 0ustar zakzajaczakzajac00000000000000