Skip to content

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

License

Notifications You must be signed in to change notification settings

JV-conseil-Internet-Consulting/django-colors-formatter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

django-colors-formatter

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

Install

pip install git+https://github.com/JV-conseil-Internet-Consulting/django-colors-formatter

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': {
            '()': 'django_colors_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.

About

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

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages

  • Python 96.4%
  • Shell 3.6%