Skip to main content

Mapped Diagnostic Context (MDC) library for python

Project description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. 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.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

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.

Description: MDC
===

.. image:: https://travis-ci.org/AFriemann/mdc.svg?branch=master
:target: https://travis-ci.org/AFriemann/mdc

This is thought to be an easy to use, import and go, library for Mapped Diagnostic Context style logging.

Logs should include all necessary fields collected by the python logging library.
Helper methods are provided to add context fields where required.

Installation
------------

PyPi:

.. code:: bash

$ pip install --user mdc

From source:

.. code:: bash

$ pip install --user .

Usage
-----

Add a handler to the root logger or set the base handler with *logging.basicConfig*:

.. code:: python

>>> import logging
>>> from mdc import MDCHandler

>>> # use the MDCHandler only
>>> logging.basicConfig(level=logging.DEBUG, handlers=[MDCHandler()])

>>> # the MDC contextmanager
>>> with MDC(foo='bar'):
... logging.warning('foobar')
{ ..., "mdc": { "foo": "bar" }, "extra": {}, ... }

>>> # the with_mdc decorator
>>> @with_mdc(test='123')
... def foobar(ctx):
... logging.warning('some warning')

>>> foobar()
{ ..., "mdc": { "test": "123" }, "extra": {}, ... }

>>> # logging with extra fields
>>> logging.error('some error', extra=dict(foo='bar'))
{ ..., "mdc": {}, "extra": { "foo": "bar" }, ... }

By default log messages will include the following fields:

.. code:: json

{
"message": "deleting context b8321b4f-19ff-4c98-b011-5a97178e7ad6",
"logger": "mdc",
"timestamp": "2018-03-07T21:39:12.010851",
"level": "DEBUG",
"mdc": {
"foo": "bar",
"index": 76
},
"extra": {},
"python": {
"module": "__init__",
"function": "MDC",
"path": "/home/user/git/mdc/mdc/__init__.py",
"file": "__init__.py",
"line": 56,
"process": {
"name": "MainProcess",
"id": 31678
},
"thread": {
"name": "MainThread",
"id": 140433289192768
}
}
}

Running tests:

.. code:: bash

$ tox


Keywords: logging,mdc
Platform: UNKNOWN

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

mdc-1.1.0.post1.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file mdc-1.1.0.post1.tar.gz.

File metadata

  • Download URL: mdc-1.1.0.post1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mdc-1.1.0.post1.tar.gz
Algorithm Hash digest
SHA256 be83c2c2c764aed6d7c38e5707834000d4807fa040c62161220f42a24b60c60d
MD5 cd75f73660edf4ff18e9d2c1924a02a5
BLAKE2b-256 d9b8939594e826708babf62601ff818ed809df249012da135457f205e9758727

See more details on using hashes here.

Supported by

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