GithubHelp home page GithubHelp logo

nickshek / django-colors-formatter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tiliv/django-colors-formatter

0.0 2.0 0.0 128 KB

Zero-config logging formatter that uses the built-in DJANGO_COLORS setting

Python 100.00%

django-colors-formatter's Introduction

django-colors-formatter

Zero-config logging formatter that uses the built-in DJANGO_COLORS setting

Setup

In your Django LOGGING settings, just add this to your formatters, and then reference it in (for example) a console logger:

LOGGING = {
    'formatters': {
        'colored': {
            '()': 'djangocolors_formatter.DjangoColorsFormatter',
            'format': '%(levelname)s %(module)s %(message)s',
        },
    },
    'handlers': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'colored',
    },
    'loggers': 
        'myproject': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    },    
}

The formatter generates its colors with Django's own internal colorizing mechanism, which means that the DJANGO_COLORS environment variable is respected.

DjangoColorsFormatter maps Django's default HTTP status code colors to the built-in logging levels:

  • DEBUG = HTTP_NOT_MODIFIED (light=green; dark=cyan)
  • INFO = HTTP_INFO (plain/bold)
  • WARNING = HTTP_NOT_FOUND (light=red; dark=yellow)
  • ERROR = ERROR (red/bold)
  • CRITICAL = HTTP_SERVER_ERROR (magenta/bold)

Environment variable: simple configuration

Django lets you configure these values, as described here, but due to the way their palette generation works, you can't invent your own logging names. That is, if you want to change the WARNING color, you indeed have to set your environment variable to use an HTTP_NOT_FOUND color, which this formatting class will pull over for its own use.

Subclassing the formatter: free-form configuration

By subclassing the formatter, you can override the custom configure_style() method, which does the default color assignments.

def configure_style(self, style):
    style.DEBUG = style.HTTP_NOT_MODIFIED
    style.INFO = style.HTTP_INFO
    style.WARNING = style.HTTP_NOT_FOUND
    style.ERROR = style.ERROR
    style.CRITICAL = style.HTTP_SERVER_ERROR
    return style

style is just a dummy object that you assign arbitrary attributes to. At runtime, the logging level name is looked up.

The attributes should be dictionaries, which define color options using the same environment variable names. For example, the Django documentation gives the following demonstration of setting the DJANGO_COLORS variable:

export DJANGO_COLORS="error=yellow/blue,blink;notice=magenta"

This would set "ERROR" messages in blinking yellow on blue, and "NOTICE" messages in magenta foreground.

To accomplish this in a formatting subclass, you would do the following:

def configure_style(self, style):
    style.ERROR = {
        'fg': 'yellow',
        'bg': 'blue',
        'opts': ('blink',),
    }
    style.NOTICE = {
        'fg': 'magenta',
    }

This is the formatting design used internally by Django to set the default color palettes.

To stylize a custom logging level name, you must use this subclassing method and assign that name a configuration dictionary (shown just above), or assign one of the default style names from the built-in palette.

django-colors-formatter's People

Contributors

tiliv avatar

Watchers

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