Skip to main content

logging.Formatter allowing for customized and colored logging output.

Project description

``styled-log``: logging.Formatter subclass that allows for custom formatted and styled logging.
=================================================================================================


By default, powered by [Colorama](https://github.com/tartley/colorama).

Allows for both ansi-styling capabilities for logger attributes, AND ansi-styling for particular values of that attribute.

https://gitlab.com/ittVannak/styled-log/blob/master/styled-log.gif

## Examples:

##### Quick use:
```
import logging

# don't call basicConfig

logger = logging.getLogger('test_logger')
styled_formatter = StyledFormatter()

writes_to_console = logging.StreamHandler(sys.stderr)
writes_to_console.setFormatter(styled_formatter)

logger.addHandler(writes_to_console)
```

##### Log structure customization:
```
import logging

# don't call basicConfig

logger = logging.getLogger('test_logger')

custom_map = OrderedDict()
custom_map['%(asctime)s'] = ('blue_fore',)
custom_map['%(module)s'] = ('yellow_fore',)
custom_map['%(lineno)s'] = ('red_fore',)
custom_map['%(message)s'] = None
custom_map['%(levelname)s'] = {
'DEBUG' : ('cyan_fore',),
'INFO' : ('blue_fore',),
'ERROR' : ('red_fore', 'bright_style'),
'WARNING' : ('red_fore', 'bright_style'),
'CRITICAL': ('red_fore', 'bright_style')
}

styled_formatter = StyledFormatter(custom_map)

writes_to_console = logging.StreamHandler(sys.stderr)
writes_to_console.setFormatter(styled_formatter)

logger.addHandler(writes_to_console)
logger.info('hey there!')

2019-03-24 10:35:59,091 ; <ipython-input-2-7091e3083bda> ; 190 ; hey there! ; INFO
```

##### Maximum customization:
```

import logging

# don't call basicConfig

logger = logging.getLogger('test_logger')

custom_ansi_map = {
'red': '\x1b[31m',
'blue':'\x1b[34m',
'green':'\x1b[32m'
}

load_ansi_map(custom_ansi_map)

# now every subsequent styling string we provide must be either 'red','blue', or 'green'

custom_map = OrderedDict()
custom_map['%(asctime)s'] = ('blue',)
custom_map['%(module)s'] = ('red',)
custom_map['%(lineno)s'] = ('red',)
custom_map['%(message)s'] = None
custom_map['%(levelname)s'] = {
'DEBUG' : ('green', ),
'INFO' : ('blue', ),
'ERROR' : ('red', ),
'WARNING' : ('red', ),
'CRITICAL': ('red', )
}

styled_formatter = StyledFormatter(custom_map)

writes_to_console = logging.StreamHandler(sys.stderr)
writes_to_console.setFormatter(styled_formatter)

logger.addHandler(writes_to_console)
logger.info('hey there!')

2019-03-24 10:35:59,091 ; <ipython-input-2-7091e3083bda> ; 190 ; hey there! ; INFO
```

## Design

There are many ways this could be implemented, but my primary goals were:
1. Designing for readability -- i.e. built around an ansi-map.
2. Not reinventing another ansi library.
3. Keeping ansi-code logic separate from logger formatting logic
4. Keeping to the logging library's design principles

Knowing that, here are some basic concepts in this library:


* **global ansi map**: dictionary of human-readable names to ansi style codes.

* **load_ansi_map**: function that sets global ansi map value. Allows user to supply their own human-readable names to ansi style codes.

* **styledFormatter**: subclass of logging.Formatter, allowing for styled bylines for each logging message. **Styling arguments for this class must align with the human-readable names in the global ansi map.**

* **style**: function that actually references the global ansi map and applies the ansi code.

* **show_ansi_map**: function that shows the effects of the different ansi codes on a string.

## Important Notes
1. Again, **styling arguments for styledFormatter must align with the keys in the global ansi map.** This means
1. if you chose to provide a custom ansi map, it the map must contain the keys referenced in the optional styledFormatter arguments
2. or, you explicitly provide the values for those arguments to align with your ansi map.
2. Even though you can provide ```%()d/f``` attributes as normal, we are string formatting, so they will come back as strings, though I don't expect that to be an issue.

## Questions:
**Why tuples?**
1. You can apply more than one ansi code to a string. For example, it might make sense for "ALERT" to be both red and bright, in which case the call to style (when using the default ansi map) would be:
```style(text="ALERT", ("red_fore", "bright_style"))```

**Why a global dictionary**?
1. To separate concerns of the log byline format from the ansi codes.
2. One less argument in the styledFormatter instantiation.
3. I prefer the usability of an explicit ```load_ansi_map(custom_ansi_map)```
4. Allows user to programmatically change styles without having to reinstantiate their formatter. Consider styling like:
```low_intensity: some_ansi, mid_intensity:another_ansi, high_intensity: a_third_ansi``` and then being able to switch those values on the fly.

## Struggles:
1. How much customization is too much - I still don't know the answer.
2. How much liberty can I take from the typical formatting argument structure?
3. Finding a way to leverage ```__slots__```.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

styled_log-1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

styled_log-1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file styled_log-1.0.tar.gz.

File metadata

  • Download URL: styled_log-1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for styled_log-1.0.tar.gz
Algorithm Hash digest
SHA256 51fb9e41db4aa5fc4c97ca187121163e71e43db2caf26b480456e91983b7c8c1
MD5 18f5f91b24eef5d290148202bddd6bb8
BLAKE2b-256 276fa30255f7d08ef5b05d34621c0402ecc0d6c6935ed1c688d8858f7302597d

See more details on using hashes here.

File details

Details for the file styled_log-1.0-py3-none-any.whl.

File metadata

  • Download URL: styled_log-1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for styled_log-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c950a5e5054c7a000b550d5742db8077b106e2d773ea76750f35627904d5c5a1
MD5 9d08ffecb4308520794e6b538b92e66f
BLAKE2b-256 2d697dd5a52b23eeed0dcb1eb9ff364f19acecc6936f32f2f73b75a27fb3ae12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page