Giter Site home page Giter Site logo

logging-levels's Introduction

logging-levels

https://travis-ci.org/six8/logging-levels.png

As projects get bigger, logging.debug() becomes the dumping ground for everything that your application is doing. This usually becomes so noisy that you can't really make sense of what you're trying to debug.

Although it is usually disabled in production, sometimes you need to enable debug logging to -- you know -- actually debug something. But since everything is dumped there, it's too much of a mess to wade through.

To help with this, you can add extra logging levels. However, rarely are they added to projects and when they are, they're often incomplete. Then sometimes you go to other projects and they're not there. So you have to lookup how to add them but usually give up and just stick with dumping something in logging.debug() cause you'll remove it later -- right?

What if it was as easy as 2 lines of code to add a new log level?

Bam!:

from logging_levels import add_log_level
add_log_level(VERBOSE=9)

How about a few new log levels?:

add_log_level(VERBOSE=9, TRACE=8, NOISE=5, IMPORTANT=100)

Now log with them:

log.verbose("I've said too much")
log.trace("But I haven't said enough")
log.noise("That's me in the corner")
log.important("That's me in the spotlight")

Want to implicitly log exceptions with your fancy new log level?:

add_log_level(DANG=90, exceptions=True)

try:
    raise Exception('Oops')
except:
    # Will include exception in log
    log.dang('Something broke.')

Project Loggers

By defualt, logging_levels manipulates the global logging module. For your projects -- especially if you're creating open source modules -- you should isolate your logging module.

from logging_levels import isolated_logging, log_exceptions

logging = isolated_logging(
    STUFF=8,
    THINGS=22,
    WTF=log_exceptions(1000),
)

logging.stuff('Log some stuff')
logging.wtf('Log some exceptions')

If you create this isolated logging module in mylib/__init__.py, then you can use it throughout your project easily.

from mylib import logging
logging.error('Oops, broke something.')

Standards

To help everyone standardize on the same log levels, this library provides a function to add some missing severity levels defined by the the syslog protocol in RFC-5424.

This library also introduces some additional debugging levels and a SUPPRESSED level which is intended to be used for logging suppressed exceptions that you may want to log, but otherwise consider handled.

Use the function add_standards to add the standard levels provided by logging-levels:

from logging_levels.standards import add_standards
import logging
add_standards(logging)

log.emergency('This aggression will not stand, man.')
log.alert('Oh no! Something happened!')
log.notice('FYI this other thing happened.')
log.verbose('Debug, but so much more')
log.trace('Log every -- single -- detail')
log.suppressed('Warn a suppressed exception')

All levels after using add_standards will be (new levels are bolded):

Level Numeric Value
EMERGENCY 100
ALERT 70
CRITICAL 50
ERROR 40
SUPPRESSED 31
WARNING 30
NOTICE 25
INFO 20
DEBUG 10
VERBOSE 7
TRACE 5
NOTSET 0

Installing

pip install logging_levels

Testing

Install dev requirements:

pip install -r test.requirements.txt

Install project:

pip install -e .

Run pytest:

py.test tests

logging-levels's People

Contributors

jong avatar six8 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jong erathaowl

logging-levels's Issues

Isolated logging modules won't bubble up to global root logger

If you use an isolated logging module for a submodule and configure your logging with the root global logging module then your isolated logging module won't bubble up so that they can be logged. Need to make it so you can isolate the Logger class and still add it to the global logging module's log manager.

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.