python-coloredlogs icon indicating copy to clipboard operation
python-coloredlogs copied to clipboard

Examples of changing colours?

Open GanizaniSitara opened this issue 3 years ago • 1 comments

Are there any examples of changing colours? The official documentation seems to show everything as environment variables. How do you do this in code? Is it passed as a parameter to the install function similarly to fmt= ?

GanizaniSitara avatar Apr 17 '22 08:04 GanizaniSitara

Yep! Fields are passed into level_styles and field_styles.

The color fields seem to be directly driven from: humanfriendly.terminal.ansi_style

Can likely copy/update the existing dictionary from coloredlogs. DEFAULT_LEVEL_STYLES as well.

import coloredlogs
import logging

styles = {
    'trace': {
        'color': 'black',
        'bright': True
    },
    'debug': {
        'color': 'white'
    },
    'info': {
        'color': 'green'
    },
    'warning': {
        'color': 'yellow'
    },
    'error': {
        'color': 'red'
    },
    'critical': {
        'bold': True,
        'color': 'red'
    }
}
coloredlogs.install(level=logging.INFO, fmt='%(levelname)-8.8s [%(filename)s:%(lineno)s %(funcName)20s()] %(message)s',
                     level_styles=styles)
logger = logging.getLogger(__name__)
logger.info("hello world")  
logger.critical("hello world")  

clayton13 avatar Sep 21 '22 21:09 clayton13