Skip to main content

A simple json formatter for python logging.

Project description

pylogops

Really simple json formatter for python projects.

Status

Build Status

Installation

pip install pylogps

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

pip install ordereddict

Basic usage

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

import logging
import time
from pylogops.logger import JsonFormatter

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

this will produce a log in json:

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

To log in console change FileHandler by an StreamHandler:

import logging
import time
from pylogops.logger import JsonFormatter

console_handler = logging.StreamHandler()
console_handler.setFormatter(JsonFormatter(converter=time.localtime))
logging.basicConfig()
logger = logging.getLogger("my_logger")
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)
logger.info("Msg")

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

import logging
import time
from pylogops.logger import JsonFormatter

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

this will produce a log in json:

{"time": "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

Tracking filter

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:

import logging
import time
from pylogops.logger import TrackingFilter, JsonFormatter

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


from pylogops import local_context
local_context.trans = "trans"
local_context.corr = "corr"
local_context.op = "op"

logger.info("Msg")

This will produce the json log:

{"time": "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:

import logging
import time
from pylogops.logger import JsonFormatter

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

this will produce a log in json:

{"lvl": "INFO", "msg": "Msg"}

Note that if you set this formatter field you should provide ALL required fields for json (even those without change).

Static filter

A filter provided to include static content to python logger. All keyword args used in the initialization of the filter will be automatically provided as values for any formatter.

import logging
import time
from pylogops.logger import StaticFilter, JsonFormatter

console_handler = logging.StreamHandler()
console_handler.addFilter(StaticFilter(version="1.2"))
console_handler.setFormatter(JsonFormatter(remove_blanks=True, keys_fmt=[('lvl', 'levelname'), ('msg', 'message'), ('version', 'version')]))
logger = logging.getLogger("my_logger")
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)

logger.info("Msg")

This will produce the json log:

{"time": "2015-12-10T15:23:52.117Z", "lvl": "INFO", "version": "1.2", "comp": "<stdin>", "msg": "Msg"}

Upload to pypi

Use twine to upload the package and publish in pypi.

pip install twine

Prepare the package:

python3 setup.py sdist bdist_wheel

Upload:

twine upload dist/*

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylogops-1.2.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

pylogops-1.2.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pylogops-1.2.0.tar.gz.

File metadata

  • Download URL: pylogops-1.2.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for pylogops-1.2.0.tar.gz
Algorithm Hash digest
SHA256 dd10ab47b8123fa5f995190cddfdfc120ada5a0262138cf1e295cb5a2da1bc6e
MD5 b32c652003a11d3d80a93119a3c35d97
BLAKE2b-256 32edac8c1939d3256ea1e7a0af9c134c244fb3e2cf0aefeadfaf6aff05bbada1

See more details on using hashes here.

File details

Details for the file pylogops-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pylogops-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for pylogops-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3c69ff47fce26fa84a404b1d4026143b2199f12e552c929a80f8b36d3b8391c
MD5 e736a9fdaa487bc3af9fcdeac158d4c7
BLAKE2b-256 e175410ea2057f64a40b4adb629fe9aa7e0c7f76009839fb7ac29ea52c94f77e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page