mirror of
https://gitlab.com/kupfer/kupferbootstrap.git
synced 2025-02-22 21:25:43 -05:00
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
|
@ -147,6 +147,7 @@ class RuntimeConfiguration(DataClass):
|
|||
script_source_dir: Optional[str]
|
||||
arch: Optional[Arch]
|
||||
uid: Optional[int]
|
||||
progress_bars: Optional[bool]
|
||||
|
||||
|
||||
class ConfigLoadState(DataClass):
|
||||
|
|
|
@ -61,6 +61,7 @@ CONFIG_RUNTIME_DEFAULTS: RuntimeConfiguration = RuntimeConfiguration.fromDict({
|
|||
'script_source_dir': None,
|
||||
'arch': None,
|
||||
'uid': None,
|
||||
'progress_bars': None,
|
||||
})
|
||||
|
||||
|
||||
|
|
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.',
|
||||
)
|
||||
|
|
13
main.py
13
main.py
|
@ -6,7 +6,7 @@ import subprocess
|
|||
from traceback import format_exc, format_exception_only, format_tb
|
||||
from typing import Optional
|
||||
|
||||
from logger import logging, setup_logging, verbose_option
|
||||
from logger import color_option, logging, setup_logging, verbose_option
|
||||
from wrapper import nowrapper_option, enforce_wrap
|
||||
|
||||
from config.cli import config, config_option, cmd_config
|
||||
|
@ -24,8 +24,15 @@ from image.cli import cmd_image
|
|||
@verbose_option
|
||||
@config_option
|
||||
@nowrapper_option
|
||||
def cli(verbose: bool = False, config_file: Optional[str] = None, wrapper_override: Optional[bool] = None, error_shell: bool = False):
|
||||
setup_logging(verbose)
|
||||
@color_option
|
||||
def cli(
|
||||
verbose: bool = False,
|
||||
config_file: Optional[str] = None,
|
||||
wrapper_override: Optional[bool] = None,
|
||||
error_shell: bool = False,
|
||||
force_colors: Optional[bool] = None,
|
||||
):
|
||||
setup_logging(verbose, force_colors=force_colors)
|
||||
config.runtime.verbose = verbose
|
||||
config.runtime.no_wrap = wrapper_override is False
|
||||
config.runtime.error_shell = error_shell
|
||||
|
|
Loading…
Add table
Reference in a new issue