summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOndřej Kobližek <kobla@debian.org>2020-10-14 12:36:14 +0200
committergit-ubuntu importer <ubuntu-devel-discuss@lists.ubuntu.com>2020-10-14 16:42:41 +0000
commitc3f6c9aecefe90cfd5f560eab35500a4054a63c4 (patch)
tree6ebc63995136cdb447b6ee8f728376c33a90345a
parentc0d0b68ea689134e52bc72e3d001af23ff061ed3 (diff)
parentab0589da5af008618137f27f31234cf136bffe9b (diff)
Imported using git-ubuntu import.
-rw-r--r--PKG-INFO179
-rw-r--r--debian/changelog6
-rw-r--r--setup.py9
-rw-r--r--src/python_json_logger.egg-info/PKG-INFO179
-rw-r--r--src/pythonjsonlogger/jsonlogger.py8
-rw-r--r--tests/tests.py10
6 files changed, 383 insertions, 8 deletions
diff --git a/PKG-INFO b/PKG-INFO
index 60be372..c2405ed 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,12 +1,184 @@
-Metadata-Version: 1.2
+Metadata-Version: 2.1
Name: python-json-logger
-Version: 2.0.0
+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: UNKNOWN
+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
@@ -21,3 +193,4 @@ 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
diff --git a/debian/changelog b/debian/changelog
index 713dd95..d9df12a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+python-pythonjsonlogger (2.0.1-1) unstable; urgency=medium
+
+ * New upstream release.
+
+ -- Ondřej Kobližek <kobla@debian.org> Wed, 14 Oct 2020 12:36:14 +0200
+
python-pythonjsonlogger (2.0.0-1) unstable; urgency=medium
[ Ondřej Nový ]
diff --git a/setup.py b/setup.py
index b15006c..5b8850a 100644
--- a/setup.py
+++ b/setup.py
@@ -1,12 +1,19 @@
from 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.0",
+ 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'},
diff --git a/src/python_json_logger.egg-info/PKG-INFO b/src/python_json_logger.egg-info/PKG-INFO
index 60be372..c2405ed 100644
--- a/src/python_json_logger.egg-info/PKG-INFO
+++ b/src/python_json_logger.egg-info/PKG-INFO
@@ -1,12 +1,184 @@
-Metadata-Version: 1.2
+Metadata-Version: 2.1
Name: python-json-logger
-Version: 2.0.0
+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: UNKNOWN
+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
@@ -21,3 +193,4 @@ 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
diff --git a/src/pythonjsonlogger/jsonlogger.py b/src/pythonjsonlogger/jsonlogger.py
index 5852cff..9ba1605 100644
--- a/src/pythonjsonlogger/jsonlogger.py
+++ b/src/pythonjsonlogger/jsonlogger.py
@@ -88,6 +88,8 @@ class JsonFormatter(logging.Formatter):
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
@@ -104,6 +106,7 @@ class JsonFormatter(logging.Formatter):
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)
@@ -148,7 +151,10 @@ class JsonFormatter(logging.Formatter):
Override this method to implement custom logic for adding fields.
"""
for field in self._required_fields:
- log_record[field] = record.__dict__.get(field)
+ 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)
diff --git a/tests/tests.py b/tests/tests.py
index 7a488e6..9ca4261 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -42,6 +42,16 @@ class TestJsonLogger(unittest.TestCase):
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',