GithubHelp home page GithubHelp logo

xiandongw / mdc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from afriemann/mdc

0.0 0.0 0.0 40 KB

Mapped Diagnostic Context style logging for Python

License: BSD 3-Clause "New" or "Revised" License

Makefile 3.26% Python 96.74%

mdc's Introduction

MDC

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

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.

NOTE 1.2.0: recent changes will result in changed json output if using the MDCHandler class:

>>> with MDC(foobar="baz"):
...   logging.warning("test")
{ "message": "test", "foobar": "baz" }

this is a temporary solution to avoid breaking existing APIs. Please excuse any issues this might cause. In the future output handling should be done on the client side to avoid any possible breakage. The MDCHandler class will eventually be removed.

Installation

PyPi:

$ pip install --user mdc

From source:

$ pip install --user .

Usage

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

>>> 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:

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

$ tox

mdc's People

Contributors

afriemann avatar stepland avatar richienb 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.