Giter Site home page Giter Site logo

hypernormalisation / timbermafia Goto Github PK

View Code? Open in Web Editor NEW
3.0 5.0 1.0 677 KB

Python package that makes implementing good-looking and flexible logging easy.

License: GNU General Public License v3.0

Python 59.99% Jupyter Notebook 40.01%
python3 logging python

timbermafia's Introduction

timbermafia

timbermafia is a drop-in replacement for application logging in python, supporting 256-bit colour output over aligned columns and expanded formatting options.

Users can choose from a selection of predefined styles and colour palettes, or can easily make their own.

Installation

timbermafia is available on the Python Package Index (PyPI).

pip install timbermafia

It requires python version 3.6 or later.

Basic usage

You can get started using timbermafia with a simple:

import timbermafia as tm
tm.basic_config()

placed in your script or application. This configuration function is very flexible, and can be used to specify styles, colour palettes, logging and date formats, output streams or files, and more.

timbermafia.basic_config is by default similar to logging.basicConfig, in that it modifies the logging module's root logger. This may not be what you want: for instance, every library with logging calls in your application will produce output!

To prevent this, instead produce a named logging.Logger object by passing the name keyword to the function, and retrieve it later with logging.getLogger.

Try it out!

Once installed with pip, a script called demo_timbermafia is placed in the user's bin. The user can specify a style, colour palette, and logging format to be used:

The available styles and colour palettes can be shown with

import timbermafia as tm
tm.print_styles()
tm.print_palettes()

Advanced usage

The timbermafia.basic_config function is fairly flexible, and should cover most use cases for configuring logging to streams and files.

If you have a specific logging requirement for other logging handlers, or require a more fine control of handler levels, you can acquire an instance of the custom formatter with something like:

import timbermafia as tm
f = tm.get_timbermafia_formatter(style='compact', palette='ocean')

and use the formatter in your own logging handlers.

Titles in logging

Timbermafia supports monkey patching of the logging.Logger class to enable any Logger object to print a title with a divider to the output.

To enable this run

import timbermafia as tm
tm.monkey_patch_logger()

and then from any logger do e.g.:

import logging
log = logging.getLogger()
log.header('My title')
log.info('You can separate your output into sections.')
log.debug('Titles respect colour schemes.')

to produce the following in your logging output.

Formats

timbermafia expands what is possible in a logging format, adding support for a variety of features.

Vertically-aligned columns.

The user can specify columns to be used in the output, which for long messages, names, function names etc. can be used to print legible multi-line output.

The column widths adapt based on what fields are present in them, so columns containing a high amount of output get more room.

An example format using 4 columns, containing respectively the:

  • date/time
  • level name
  • process name and log name
  • log message

can be specified with a column escape, by default "_". Whatever follows the column escape until the next whitespace is registered as output that will visually separate the column.

So our format can be e.g.

{asctime} _| {levelname} _| {processName} {name}  __:: {message}

which produces the following output with the "synth" colour palette:

Note that we double-escape the final separator so that it prints over all lines of multi-line output, and that multiple characters, or indeed no characters, can be used as a column separator.

Enhanced fmt_spec

Individual log records can be formatted according to a format specification, or fmt_spec.

For example, the following output will force the message content to be bold and bright green, overriding any formatting based upon the log level, and underline the time to punctuate the start of a new log record:

{asctime:u} _ {name} _ {message:b,>82}

Let's try that out with the "ocean" palette:

The following are recognised in the format spec via a comma-separated list:

  • b: bold
  • e: emphasis/italic
  • u: underline
  • any int: the corresponding ANSI code, e.g. 5,9 will set slow blink and crossed-out text
  • >int: set the foreground colour to the 8-bit colour code.
  • <int: set the background colour to the 8-bit colour code.

Note that what ANSI codes will be possible is dependent on what terminal or terminal emulator is being used.

A full list of the ANSI colour codes matched to some sample output can be printed to the terminal with the included script display_ansi_colours.

Custom styles and palettes

Each style and palette can be generated into a Style or Palette object, with a flexible customisation supported.

A full guide to customising styles and palettes can be found in this notebook.

timbermafia's People

Contributors

hypernormalisation avatar

Stargazers

Andreas Motl avatar Gavin avatar Dr Will Breaden Madden avatar

Watchers

James Cloos avatar Dr Will Breaden Madden avatar Gavin avatar  avatar  avatar

Forkers

wdbm

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.