python-json-logger-2.0.1/0000775000372000037200000000000013741101714016120 5ustar travistravis00000000000000python-json-logger-2.0.1/MANIFEST.in0000664000372000037200000000005413741101665017662 0ustar travistravis00000000000000include LICENSE recursive-include tests *.pypython-json-logger-2.0.1/PKG-INFO0000664000372000037200000001705113741101714017221 0ustar travistravis00000000000000Metadata-Version: 2.1 Name: python-json-logger Version: 2.0.1 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: [![Build Status](https://travis-ci.org/madzak/python-json-logger.svg?branch=master)](https://travis-ci.org/madzak/python-json-logger) [![License](https://img.shields.io/pypi/l/python-json-logger.svg)](https://pypi.python.org/pypi/python-json-logger/) [![Version](https://img.shields.io/pypi/v/python-json-logger.svg)](https://pypi.python.org/pypi/python-json-logger/) Overview ======= This library is provided to allow standard python logging to output log data as json objects. With JSON we can make our logs more readable by machines and we can stop writing custom parsers for syslog type records. News ======= Hi, I see this package is quiet alive and I am sorry for ignoring it so long. I will be stepping up my maintenance of this package so please allow me a week to get things back in order (and most likely a new minor version) and i'll post and update here once I am caught up. Installing ========== Pip: pip install python-json-logger Pypi: https://pypi.python.org/pypi/python-json-logger Manual: python setup.py install Usage ===== ## Integrating with Python's logging framework Json outputs are provided by the JsonFormatter logging formatter. You can add the custom formatter like below: **Please note: version 0.1.0 has changed the import structure, please update to the following example for proper importing** ```python import logging from pythonjsonlogger import jsonlogger logger = logging.getLogger() logHandler = logging.StreamHandler() formatter = jsonlogger.JsonFormatter() logHandler.setFormatter(formatter) logger.addHandler(logHandler) ``` ## Customizing fields The fmt parser can also be overidden if you want to have required fields that differ from the default of just `message`. These two invocations are equivalent: ```python class CustomJsonFormatter(jsonlogger.JsonFormatter): def parse(self): return self._fmt.split(';') formatter = CustomJsonFormatter('one;two') # is equivalent to: formatter = jsonlogger.JsonFormatter('%(one)s %(two)s') ``` You can also add extra fields to your json output by specifying a dict in place of message, as well as by specifying an `extra={}` argument. Contents of these dictionaries will be added at the root level of the entry and may override basic fields. You can also use the `add_fields` method to add to or generally normalize the set of default set of fields, it is called for every log event. For example, to unify default fields with those provided by [structlog](http://www.structlog.org/) you could do something like this: ```python class CustomJsonFormatter(jsonlogger.JsonFormatter): def add_fields(self, log_record, record, message_dict): super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict) if not log_record.get('timestamp'): # this doesn't use record.created, so it is slightly off now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') log_record['timestamp'] = now if log_record.get('level'): log_record['level'] = log_record['level'].upper() else: log_record['level'] = record.levelname formatter = CustomJsonFormatter('%(timestamp)s %(level)s %(name)s %(message)s') ``` Items added to the log record will be included in *every* log message, no matter what the format requires. ## Adding custom object serialization For custom handling of object serialization you can specify default json object translator or provide a custom encoder ```python def json_translate(obj): if isinstance(obj, MyClass): return {"special": obj.special} formatter = jsonlogger.JsonFormatter(json_default=json_translate, json_encoder=json.JSONEncoder) logHandler.setFormatter(formatter) logger.info({"special": "value", "run": 12}) logger.info("classic message", extra={"special": "value", "run": 12}) ``` ## Using a Config File To use the module with a config file using the [`fileConfig` function](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig), use the class `pythonjsonlogger.jsonlogger.JsonFormatter`. Here is a sample config file. ```ini [loggers] keys = root,custom [logger_root] handlers = [logger_custom] level = INFO handlers = custom qualname = custom [handlers] keys = custom [handler_custom] class = StreamHandler level = INFO formatter = json args = (sys.stdout,) [formatters] keys = json [formatter_json] format = %(message)s class = pythonjsonlogger.jsonlogger.JsonFormatter ``` Example Output ============== Sample JSON with a full formatter (basically the log message from the unit test). Every log message will appear on 1 line like a typical logger. ```json { "threadName": "MainThread", "name": "root", "thread": 140735202359648, "created": 1336281068.506248, "process": 41937, "processName": "MainProcess", "relativeCreated": 9.100914001464844, "module": "tests", "funcName": "testFormatKeys", "levelno": 20, "msecs": 506.24799728393555, "pathname": "tests/tests.py", "lineno": 60, "asctime": ["12-05-05 22:11:08,506248"], "message": "testing logging format", "filename": "tests.py", "levelname": "INFO", "special": "value", "run": 12 } ``` External Examples ================= - [Wesley Tanaka - Structured log files in Python using python-json-logger](https://wtanaka.com/node/8201) 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 :: 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: Programming Language :: Python :: 3.8 Classifier: Topic :: System :: Logging Requires-Python: >=3.4 Description-Content-Type: text/markdown python-json-logger-2.0.1/README.md0000664000372000037200000001251113741101665017404 0ustar travistravis00000000000000[![Build Status](https://travis-ci.org/madzak/python-json-logger.svg?branch=master)](https://travis-ci.org/madzak/python-json-logger) [![License](https://img.shields.io/pypi/l/python-json-logger.svg)](https://pypi.python.org/pypi/python-json-logger/) [![Version](https://img.shields.io/pypi/v/python-json-logger.svg)](https://pypi.python.org/pypi/python-json-logger/) Overview ======= This library is provided to allow standard python logging to output log data as json objects. With JSON we can make our logs more readable by machines and we can stop writing custom parsers for syslog type records. News ======= Hi, I see this package is quiet alive and I am sorry for ignoring it so long. I will be stepping up my maintenance of this package so please allow me a week to get things back in order (and most likely a new minor version) and i'll post and update here once I am caught up. Installing ========== Pip: pip install python-json-logger Pypi: https://pypi.python.org/pypi/python-json-logger Manual: python setup.py install Usage ===== ## Integrating with Python's logging framework Json outputs are provided by the JsonFormatter logging formatter. You can add the custom formatter like below: **Please note: version 0.1.0 has changed the import structure, please update to the following example for proper importing** ```python import logging from pythonjsonlogger import jsonlogger logger = logging.getLogger() logHandler = logging.StreamHandler() formatter = jsonlogger.JsonFormatter() logHandler.setFormatter(formatter) logger.addHandler(logHandler) ``` ## Customizing fields The fmt parser can also be overidden if you want to have required fields that differ from the default of just `message`. These two invocations are equivalent: ```python class CustomJsonFormatter(jsonlogger.JsonFormatter): def parse(self): return self._fmt.split(';') formatter = CustomJsonFormatter('one;two') # is equivalent to: formatter = jsonlogger.JsonFormatter('%(one)s %(two)s') ``` You can also add extra fields to your json output by specifying a dict in place of message, as well as by specifying an `extra={}` argument. Contents of these dictionaries will be added at the root level of the entry and may override basic fields. You can also use the `add_fields` method to add to or generally normalize the set of default set of fields, it is called for every log event. For example, to unify default fields with those provided by [structlog](http://www.structlog.org/) you could do something like this: ```python class CustomJsonFormatter(jsonlogger.JsonFormatter): def add_fields(self, log_record, record, message_dict): super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict) if not log_record.get('timestamp'): # this doesn't use record.created, so it is slightly off now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') log_record['timestamp'] = now if log_record.get('level'): log_record['level'] = log_record['level'].upper() else: log_record['level'] = record.levelname formatter = CustomJsonFormatter('%(timestamp)s %(level)s %(name)s %(message)s') ``` Items added to the log record will be included in *every* log message, no matter what the format requires. ## Adding custom object serialization For custom handling of object serialization you can specify default json object translator or provide a custom encoder ```python def json_translate(obj): if isinstance(obj, MyClass): return {"special": obj.special} formatter = jsonlogger.JsonFormatter(json_default=json_translate, json_encoder=json.JSONEncoder) logHandler.setFormatter(formatter) logger.info({"special": "value", "run": 12}) logger.info("classic message", extra={"special": "value", "run": 12}) ``` ## Using a Config File To use the module with a config file using the [`fileConfig` function](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig), use the class `pythonjsonlogger.jsonlogger.JsonFormatter`. Here is a sample config file. ```ini [loggers] keys = root,custom [logger_root] handlers = [logger_custom] level = INFO handlers = custom qualname = custom [handlers] keys = custom [handler_custom] class = StreamHandler level = INFO formatter = json args = (sys.stdout,) [formatters] keys = json [formatter_json] format = %(message)s class = pythonjsonlogger.jsonlogger.JsonFormatter ``` Example Output ============== Sample JSON with a full formatter (basically the log message from the unit test). Every log message will appear on 1 line like a typical logger. ```json { "threadName": "MainThread", "name": "root", "thread": 140735202359648, "created": 1336281068.506248, "process": 41937, "processName": "MainProcess", "relativeCreated": 9.100914001464844, "module": "tests", "funcName": "testFormatKeys", "levelno": 20, "msecs": 506.24799728393555, "pathname": "tests/tests.py", "lineno": 60, "asctime": ["12-05-05 22:11:08,506248"], "message": "testing logging format", "filename": "tests.py", "levelname": "INFO", "special": "value", "run": 12 } ``` External Examples ================= - [Wesley Tanaka - Structured log files in Python using python-json-logger](https://wtanaka.com/node/8201) python-json-logger-2.0.1/src/0000775000372000037200000000000013741101714016707 5ustar travistravis00000000000000python-json-logger-2.0.1/src/pythonjsonlogger/0000775000372000037200000000000013741101714022322 5ustar travistravis00000000000000python-json-logger-2.0.1/src/pythonjsonlogger/jsonlogger.py0000664000372000037200000002101513741101665025051 0ustar travistravis00000000000000''' 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, timezone 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 rename_fields: an optional dict, used to rename field names in the output. Rename message to @message: {'message': '@message'} :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", "") self.rename_fields = kwargs.pop("rename_fields", {}) 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: if field in self.rename_fields: log_record[self.rename_fields[field]] = record.__dict__.get(field) else: 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.fromtimestamp(record.created, tz=timezone.utc) 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 serialize_log_record(self, log_record): """Returns the final representation of the log record.""" return "%s%s" % (self.prefix, self.jsonify_log_record(log_record)) 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 self.serialize_log_record(log_record) python-json-logger-2.0.1/src/pythonjsonlogger/__init__.py0000664000372000037200000000000013741101665024426 0ustar travistravis00000000000000python-json-logger-2.0.1/src/python_json_logger.egg-info/0000775000372000037200000000000013741101714024312 5ustar travistravis00000000000000python-json-logger-2.0.1/src/python_json_logger.egg-info/PKG-INFO0000664000372000037200000001705113741101714025413 0ustar travistravis00000000000000Metadata-Version: 2.1 Name: python-json-logger Version: 2.0.1 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: [![Build Status](https://travis-ci.org/madzak/python-json-logger.svg?branch=master)](https://travis-ci.org/madzak/python-json-logger) [![License](https://img.shields.io/pypi/l/python-json-logger.svg)](https://pypi.python.org/pypi/python-json-logger/) [![Version](https://img.shields.io/pypi/v/python-json-logger.svg)](https://pypi.python.org/pypi/python-json-logger/) Overview ======= This library is provided to allow standard python logging to output log data as json objects. With JSON we can make our logs more readable by machines and we can stop writing custom parsers for syslog type records. News ======= Hi, I see this package is quiet alive and I am sorry for ignoring it so long. I will be stepping up my maintenance of this package so please allow me a week to get things back in order (and most likely a new minor version) and i'll post and update here once I am caught up. Installing ========== Pip: pip install python-json-logger Pypi: https://pypi.python.org/pypi/python-json-logger Manual: python setup.py install Usage ===== ## Integrating with Python's logging framework Json outputs are provided by the JsonFormatter logging formatter. You can add the custom formatter like below: **Please note: version 0.1.0 has changed the import structure, please update to the following example for proper importing** ```python import logging from pythonjsonlogger import jsonlogger logger = logging.getLogger() logHandler = logging.StreamHandler() formatter = jsonlogger.JsonFormatter() logHandler.setFormatter(formatter) logger.addHandler(logHandler) ``` ## Customizing fields The fmt parser can also be overidden if you want to have required fields that differ from the default of just `message`. These two invocations are equivalent: ```python class CustomJsonFormatter(jsonlogger.JsonFormatter): def parse(self): return self._fmt.split(';') formatter = CustomJsonFormatter('one;two') # is equivalent to: formatter = jsonlogger.JsonFormatter('%(one)s %(two)s') ``` You can also add extra fields to your json output by specifying a dict in place of message, as well as by specifying an `extra={}` argument. Contents of these dictionaries will be added at the root level of the entry and may override basic fields. You can also use the `add_fields` method to add to or generally normalize the set of default set of fields, it is called for every log event. For example, to unify default fields with those provided by [structlog](http://www.structlog.org/) you could do something like this: ```python class CustomJsonFormatter(jsonlogger.JsonFormatter): def add_fields(self, log_record, record, message_dict): super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict) if not log_record.get('timestamp'): # this doesn't use record.created, so it is slightly off now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') log_record['timestamp'] = now if log_record.get('level'): log_record['level'] = log_record['level'].upper() else: log_record['level'] = record.levelname formatter = CustomJsonFormatter('%(timestamp)s %(level)s %(name)s %(message)s') ``` Items added to the log record will be included in *every* log message, no matter what the format requires. ## Adding custom object serialization For custom handling of object serialization you can specify default json object translator or provide a custom encoder ```python def json_translate(obj): if isinstance(obj, MyClass): return {"special": obj.special} formatter = jsonlogger.JsonFormatter(json_default=json_translate, json_encoder=json.JSONEncoder) logHandler.setFormatter(formatter) logger.info({"special": "value", "run": 12}) logger.info("classic message", extra={"special": "value", "run": 12}) ``` ## Using a Config File To use the module with a config file using the [`fileConfig` function](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig), use the class `pythonjsonlogger.jsonlogger.JsonFormatter`. Here is a sample config file. ```ini [loggers] keys = root,custom [logger_root] handlers = [logger_custom] level = INFO handlers = custom qualname = custom [handlers] keys = custom [handler_custom] class = StreamHandler level = INFO formatter = json args = (sys.stdout,) [formatters] keys = json [formatter_json] format = %(message)s class = pythonjsonlogger.jsonlogger.JsonFormatter ``` Example Output ============== Sample JSON with a full formatter (basically the log message from the unit test). Every log message will appear on 1 line like a typical logger. ```json { "threadName": "MainThread", "name": "root", "thread": 140735202359648, "created": 1336281068.506248, "process": 41937, "processName": "MainProcess", "relativeCreated": 9.100914001464844, "module": "tests", "funcName": "testFormatKeys", "levelno": 20, "msecs": 506.24799728393555, "pathname": "tests/tests.py", "lineno": 60, "asctime": ["12-05-05 22:11:08,506248"], "message": "testing logging format", "filename": "tests.py", "levelname": "INFO", "special": "value", "run": 12 } ``` External Examples ================= - [Wesley Tanaka - Structured log files in Python using python-json-logger](https://wtanaka.com/node/8201) 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 :: 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: Programming Language :: Python :: 3.8 Classifier: Topic :: System :: Logging Requires-Python: >=3.4 Description-Content-Type: text/markdown python-json-logger-2.0.1/src/python_json_logger.egg-info/dependency_links.txt0000664000372000037200000000000113741101714030360 0ustar travistravis00000000000000 python-json-logger-2.0.1/src/python_json_logger.egg-info/top_level.txt0000664000372000037200000000002113741101714027035 0ustar travistravis00000000000000pythonjsonlogger python-json-logger-2.0.1/src/python_json_logger.egg-info/SOURCES.txt0000664000372000037200000000051513741101714026177 0ustar travistravis00000000000000LICENSE MANIFEST.in README.md 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-2.0.1/setup.cfg0000664000372000037200000000010713741101714017737 0ustar travistravis00000000000000[bdist_wheel] python-tag = py34 [egg_info] tag_build = tag_date = 0 python-json-logger-2.0.1/LICENSE0000664000372000037200000000241213741101665017131 0ustar travistravis00000000000000Copyright (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-2.0.1/setup.py0000664000372000037200000000263213741101665017642 0ustar travistravis00000000000000from setuptools import setup, find_packages # read the contents of your README file from os import path this_directory = path.abspath(path.dirname(__file__)) with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: long_description = f.read() setup( name="python-json-logger", version="2.0.1", url="http://github.com/madzak/python-json-logger", license="BSD", description="A python library adding a json log formatter", long_description=long_description, long_description_content_type='text/markdown', 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='>=3.4', 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 :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Topic :: System :: Logging', ] ) python-json-logger-2.0.1/tests/0000775000372000037200000000000013741101714017262 5ustar travistravis00000000000000python-json-logger-2.0.1/tests/tests.py0000664000372000037200000002030313741101665021001 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import unittest import unittest.mock import logging import json import sys import traceback import random try: import xmlrunner # noqa except ImportError: pass try: from StringIO import StringIO # noqa 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-{}".format(random.randint(1, 101))) 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 testRenameBaseField(self): fr = jsonlogger.JsonFormatter(rename_fields={'message': '@message'}) 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})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") @unittest.mock.patch('time.time', return_value=1500000000.0) def testJsonDefaultEncoderWithTimestamp(self, time_mock): fr = jsonlogger.JsonFormatter(timestamp=True) self.logHandler.setFormatter(fr) self.logger.info("Hello") self.assertTrue(time_mock.called) logJson = json.loads(self.buffer.getvalue()) self.assertEqual(logJson.get("timestamp"), "2017-07-14T02:40:00+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, "Привет") def testCustomObjectSerialization(self): def encode_complex(z): if isinstance(z, complex): return (z.real, z.imag) else: type_name = z.__class__.__name__ raise TypeError("Object of type '{}' is no JSON serializable".format(type_name)) formatter = jsonlogger.JsonFormatter(json_default=encode_complex, json_encoder=json.JSONEncoder) self.logHandler.setFormatter(formatter) value = { "special": complex(3, 8), } self.logger.info(" message", extra=value) msg = self.buffer.getvalue() self.assertEqual(msg, "{\"message\": \" message\", \"special\": [3.0, 8.0]}\n") 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-2.0.1/tests/__init__.py0000664000372000037200000000000013741101665021366 0ustar travistravis00000000000000