telefonicaid/pylogops

Name: pylogops

Owner: Telefónica I+D

Description: Python version of logops, the simple and performant logger.

Created: 2015-11-11 12:51:14.0

Updated: 2017-01-29 17:32:35.0

Pushed: 2017-01-16 17:54:20.0

Homepage: null

Size: 16

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

pylogops

Really simple json formatter for python projects.

Status

Build Status

Installation
install pylogps

if you want use with python2.6 you must install the backport ordereddict as well:

install ordereddict
Basic usage

You will have to specify the formatter JsonFormatter in any handler that you use in standar python logging:

rt logging
rt time
 pylogops.logger import JsonFormatter

_handler = logging.FileHandler('/tmp/my_log.log', encoding='UTF-8')
_handler.setFormatter(JsonFormatter(converter=time.localtime))
ing.basicConfig()
er = logging.getLogger("my_logger")
er.addHandler(file_handler)
er.setLevel(logging.INFO)
er.info("Msg")

this will produce a log in json:

me": "2015-12-09T17:46:01.160Z", "lvl": "INFO", "corr": null, "trans": null, "op": null, "comp": "<stdin>", "msg": "Msg"}

pylogps by default generate all the upper fields in json output, but you can select to remove null fields:

rt logging
rt time
 pylogops.logger import JsonFormatter

_handler = logging.FileHandler('/tmp/my_log.log', encoding='UTF-8')
_handler.setFormatter(JsonFormatter(remove_blanks=True))
ing.basicConfig()
er = logging.getLogger("my_logger")
er.addHandler(file_handler)
er.setLevel(logging.INFO)
er.info("Msg")

this will produce a log in json:

me": "2015-12-09T16:57:00.784Z", "lvl": "INFO", "comp": "<stdin>", "msg": "Msg"}

You can configure the formatter in any way provided by python logging library, e.g (dictConfig, fileConfig, ?). You can check unit tests directory for some example.

Advanced usage
Context support

Pylogps includes a filter and a local_context to support using a context holding information about a correlator (corr), transaction (trans) and operation (op). If you include the values using local_context, those fields will be available in formatter. You need to add a Filter:

rt logging
rt time
 pylogops.logger import TrackingFilter, JsonFormatter

_handler = logging.FileHandler('/tmp/my_log.log', encoding='UTF-8')
_handler.addFilter(TrackingFilter())
_handler.setFormatter(JsonFormatter(remove_blanks=True))
ing.basicConfig()
er = logging.getLogger("my_logger")
er.addHandler(file_handler)
er.setLevel(logging.INFO)


 pylogops import local_context
l_context.transaction_id = "trans"
l_context.correlator_id = "corr"
l_context.op_type = "op"

er.info("Msg")

This will produce the json log:

me": "2015-12-10T15:23:52.117Z", "lvl": "INFO", "corr": "corr", "trans": "trans", "op": "op", "comp": "<stdin>", "msg": "Msg"}

local_context is a thread.local() that is shared in current thread for all modules; typically you will include the values in a middleware or some kind of transversal module.

Customizing json fields

You can specify the fields for output in json in this way:

rt logging
rt time
 pylogops.logger import JsonFormatter

_handler = logging.FileHandler('/tmp/my_log.log', encoding='UTF-8')
_handler.setFormatter(JsonFormatter(keys_fmt=[('lvl', 'levelname'), ('msg', 'message')]))
ing.basicConfig()
er = logging.getLogger("my_logger")
er.addHandler(file_handler)
er.setLevel(logging.INFO)
er.info("Msg")

this will produce a log in json:

l": "INFO", "msg": "Msg"}
License

Copyright 2014, 2015 Telefonica Investigación y Desarrollo, S.A.U

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.