Git Product home page Git Product logo

python-json-logger's Introduction

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.

Installing

Pip:

pip install python-json-logger

Pypi:

https://pypi.python.org/pypi/python-json-logger

Manual:

python setup.py install

Usage

Json outputs are provided by the JsonFormatter logging formatter. You can add the customer formatter like below:

Please note: version 0.1.0 has changed the import structure, please update to the following example for proper importing

    import logging
    from pythonjsonlogger import jsonlogger

    logger = logging.getLogger()

    logHandler = logging.StreamHandler()
    formatter = jsonlogger.JsonFormatter()
    logHandler.setFormatter(formatter)
    logger.addHandler(logHandler)

The fmt parser can also be overidden if you want to use an alternate from the default.

    class CustomJsonFormatter(jsonlogger.JsonFormatter):
      def parse(self):
          return eval(self._fmt)

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. For custom handling of object serialization you can specify default json object translator or provide a custom encoder

    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})

Example

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.

{
    "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
}

python-json-logger's People

Contributors

deanq avatar gregtap avatar lothiraldan avatar madzak avatar mrluanma avatar peritus avatar svisser avatar zebuline avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.