logger: add --force-colors/--no-colors cli flag
This commit is contained in:
parent
4d03f238bb
commit
785e41f8b7
4 changed files with 26 additions and 5 deletions
16
logger.py
16
logger.py
|
@ -3,8 +3,10 @@ import coloredlogs
|
|||
import logging
|
||||
import sys
|
||||
|
||||
from typing import Optional
|
||||
|
||||
def setup_logging(verbose: bool, log_setup: bool = True):
|
||||
|
||||
def setup_logging(verbose: bool, force_colors: Optional[bool] = None, log_setup: bool = True):
|
||||
level_colors = coloredlogs.DEFAULT_LEVEL_STYLES | {'info': {'color': 'magenta', 'bright': True}, 'debug': {'color': 'blue', 'bright': True}}
|
||||
field_colors = coloredlogs.DEFAULT_FIELD_STYLES | {'asctime': {'color': 'white', 'faint': True}}
|
||||
level = logging.DEBUG if verbose else logging.INFO
|
||||
|
@ -15,9 +17,12 @@ def setup_logging(verbose: bool, log_setup: bool = True):
|
|||
level=level,
|
||||
level_styles=level_colors,
|
||||
field_styles=field_colors,
|
||||
isatty=force_colors,
|
||||
)
|
||||
if log_setup:
|
||||
logging.debug('Logging set up.')
|
||||
logging.debug('Logger: Logging set up.')
|
||||
if force_colors is not None:
|
||||
logging.debug(f'Logger: Force-{"en" if force_colors else "dis"}abled colors')
|
||||
|
||||
|
||||
verbose_option = click.option(
|
||||
|
@ -26,3 +31,10 @@ verbose_option = click.option(
|
|||
is_flag=True,
|
||||
help='Enables verbose logging',
|
||||
)
|
||||
|
||||
color_option = click.option(
|
||||
'--force-colors/--no-colors',
|
||||
is_flag=True,
|
||||
default=None,
|
||||
help='Force enable/disable log coloring. Defaults to autodetection.',
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue