bluemira.base.logs

Logging system setup and control.

Classes

LogLevel

Linking level names and corresponding numbers.

LoggerAdapter

Adapt the base logging class for our uses

BluemiraRichHandler

Rich handler modified for different output types

BluemiraRichFileHandler

Allow some filtering on file log handlers

ConsoleFlush

Rich console modified for progress bar use

LoggingContext

A context manager for temporarily adjusting the logging level

Functions

stop_progress(logger, stop[, wait])

Kill progress bar as soon as possible

logger_setup(→ logging.Logger)

Create logger with two handlers.

set_log_level([verbose, increase, logger_names])

Get new log level and check if it is possible.

get_log_level(→ str | int)

Return the current logging level.

_modify_handler(new_level, logger)

Change level of the logger from user's input.

Module Contents

class bluemira.base.logs.LogLevel(_, colour: str | None = '')

Bases: enum.Enum

Inheritance diagram of bluemira.base.logs.LogLevel

Linking level names and corresponding numbers.

Parameters:

colour (str | None)

CRITICAL = (5, 'darkred')
ERROR = (4, 'red')
WARNING = (3, 'orange')
INFO = (2, 'blue')
DEBUG = (1, 'green')
NOTSET = (0, None)
colour = ''
classmethod _missing_(value: int | str) LogLevel
Parameters:

value (int | str)

Return type:

LogLevel

value_for_logging() int

Return builtin logging level value

Return type:

int

bluemira.base.logs.stop_progress(logger: LoggerAdapter, stop: threading.Event, wait: float = 4)

Kill progress bar as soon as possible

Parameters:
  • logger (LoggerAdapter) – Logger instance

  • stop (threading.Event) – Kill signal event

  • wait (float) – time (s) to wait before killing progress bar

class bluemira.base.logs.LoggerAdapter(name, level=NOTSET)

Bases: logging.Logger

Inheritance diagram of bluemira.base.logs.LoggerAdapter

Adapt the base logging class for our uses

progress: rich.progress.Progress | None = None
_stop_p: threading.Event
_base(func: collections.abc.Callable[[str], None], msg: str, *args, flush: bool = False, progress_timeout: float = 4, _clean: bool = False, **kwargs)
Parameters:
  • func (collections.abc.Callable[[str], None])

  • msg (str)

  • flush (bool)

  • progress_timeout (float)

  • _clean (bool)

makeRecord(*args, **kwargs) logging.LogRecord

Overridden makeRecord to pass variables to handler

Return type:

logging.LogRecord

debug(msg: str, *args, flush: bool = False, **kwargs)

Debug

Parameters:
  • msg (str)

  • flush (bool)

info(msg: str, *args, flush: bool = False, **kwargs)

Info

Parameters:
  • msg (str)

  • flush (bool)

warning(msg: str, *args, flush: bool = False, **kwargs)

Warning

Parameters:
  • msg (str)

  • flush (bool)

error(msg: str, *args, flush: bool = False, **kwargs)

Error

Parameters:
  • msg (str)

  • flush (bool)

critical(msg: str, *args, flush: bool = False, **kwargs)

Critical

Parameters:
  • msg (str)

  • flush (bool)

clean(msg: str, loglevel: str | LogLevel = LogLevel.INFO, *args, flush: bool = False, **kwargs)

Unmodified logging

Parameters:
  • msg (str)

  • loglevel (str | LogLevel)

  • flush (bool)

class bluemira.base.logs.BluemiraRichHandler(level: int | str = logging.NOTSET, console: rich.console.Console | None = None, *, show_time: bool = True, omit_repeated_times: bool = True, show_level: bool = True, show_path: bool = True, enable_link_path: bool = True, highlighter: rich.highlighter.Highlighter | None = None, markup: bool = False, rich_tracebacks: bool = False, tracebacks_width: int | None = None, tracebacks_code_width: int | None = 88, tracebacks_extra_lines: int = 3, tracebacks_theme: str | None = None, tracebacks_word_wrap: bool = True, tracebacks_show_locals: bool = False, tracebacks_suppress: Iterable[str | types.ModuleType] = (), tracebacks_max_frames: int = 100, locals_max_length: int = 10, locals_max_string: int = 80, log_time_format: str | rich._log_render.FormatTimeCallable = '[%x %X]', keywords: List[str] | None = None)

Bases: rich.logging.RichHandler

Inheritance diagram of bluemira.base.logs.BluemiraRichHandler

Rich handler modified for different output types

Parameters:
  • level (Union[int, str])

  • console (Optional[rich.console.Console])

  • show_time (bool)

  • omit_repeated_times (bool)

  • show_level (bool)

  • show_path (bool)

  • enable_link_path (bool)

  • highlighter (Optional[rich.highlighter.Highlighter])

  • markup (bool)

  • rich_tracebacks (bool)

  • tracebacks_width (Optional[int])

  • tracebacks_code_width (Optional[int])

  • tracebacks_extra_lines (int)

  • tracebacks_theme (Optional[str])

  • tracebacks_word_wrap (bool)

  • tracebacks_show_locals (bool)

  • tracebacks_suppress (Iterable[Union[str, types.ModuleType]])

  • tracebacks_max_frames (int)

  • locals_max_length (int)

  • locals_max_string (int)

  • log_time_format (Union[str, rich._log_render.FormatTimeCallable])

  • keywords (Optional[List[str]])

render(*, record: logging.LogRecord, traceback: rich.traceback.Traceback | None, message_renderable: rich.console.ConsoleRenderable) rich.console.ConsoleRenderable

Rich handler rendering in a panel under as requested

Returns:

The text to be rendered by the logger

Parameters:
  • record (logging.LogRecord)

  • traceback (rich.traceback.Traceback | None)

  • message_renderable (rich.console.ConsoleRenderable)

Return type:

rich.console.ConsoleRenderable

class bluemira.base.logs.BluemiraRichFileHandler(level: int | str = logging.NOTSET, console: rich.console.Console | None = None, *, show_time: bool = True, omit_repeated_times: bool = True, show_level: bool = True, show_path: bool = True, enable_link_path: bool = True, highlighter: rich.highlighter.Highlighter | None = None, markup: bool = False, rich_tracebacks: bool = False, tracebacks_width: int | None = None, tracebacks_code_width: int | None = 88, tracebacks_extra_lines: int = 3, tracebacks_theme: str | None = None, tracebacks_word_wrap: bool = True, tracebacks_show_locals: bool = False, tracebacks_suppress: Iterable[str | types.ModuleType] = (), tracebacks_max_frames: int = 100, locals_max_length: int = 10, locals_max_string: int = 80, log_time_format: str | rich._log_render.FormatTimeCallable = '[%x %X]', keywords: List[str] | None = None)

Bases: BluemiraRichHandler

Inheritance diagram of bluemira.base.logs.BluemiraRichFileHandler

Allow some filtering on file log handlers

Parameters:
  • level (Union[int, str])

  • console (Optional[rich.console.Console])

  • show_time (bool)

  • omit_repeated_times (bool)

  • show_level (bool)

  • show_path (bool)

  • enable_link_path (bool)

  • highlighter (Optional[rich.highlighter.Highlighter])

  • markup (bool)

  • rich_tracebacks (bool)

  • tracebacks_width (Optional[int])

  • tracebacks_code_width (Optional[int])

  • tracebacks_extra_lines (int)

  • tracebacks_theme (Optional[str])

  • tracebacks_word_wrap (bool)

  • tracebacks_show_locals (bool)

  • tracebacks_suppress (Iterable[Union[str, types.ModuleType]])

  • tracebacks_max_frames (int)

  • locals_max_length (int)

  • locals_max_string (int)

  • log_time_format (Union[str, rich._log_render.FormatTimeCallable])

  • keywords (Optional[List[str]])

class bluemira.base.logs.ConsoleFlush(*, color_system: Literal['auto', 'standard', '256', 'truecolor', 'windows'] | None = 'auto', force_terminal: bool | None = None, force_jupyter: bool | None = None, force_interactive: bool | None = None, soft_wrap: bool = False, theme: rich.theme.Theme | None = None, stderr: bool = False, file: IO[str] | None = None, quiet: bool = False, width: int | None = None, height: int | None = None, style: rich.style.StyleType | None = None, no_color: bool | None = None, tab_size: int = 8, record: bool = False, markup: bool = True, emoji: bool = True, emoji_variant: rich.emoji.EmojiVariant | None = None, highlight: bool = True, log_time: bool = True, log_path: bool = True, log_time_format: str | rich._log_render.FormatTimeCallable = '[%X]', highlighter: HighlighterType | None = ReprHighlighter(), legacy_windows: bool | None = None, safe_box: bool = True, get_datetime: Callable[[], datetime.datetime] | None = None, get_time: Callable[[], float] | None = None, _environ: Mapping[str, str] | None = None)

Bases: rich.console.Console

Inheritance diagram of bluemira.base.logs.ConsoleFlush

Rich console modified for progress bar use

Parameters:
  • color_system (Optional[Literal['auto', 'standard', '256', 'truecolor', 'windows']])

  • force_terminal (Optional[bool])

  • force_jupyter (Optional[bool])

  • force_interactive (Optional[bool])

  • soft_wrap (bool)

  • theme (Optional[rich.theme.Theme])

  • stderr (bool)

  • file (Optional[IO[str]])

  • quiet (bool)

  • width (Optional[int])

  • height (Optional[int])

  • style (Optional[rich.style.StyleType])

  • no_color (Optional[bool])

  • tab_size (int)

  • record (bool)

  • markup (bool)

  • emoji (bool)

  • emoji_variant (Optional[rich.emoji.EmojiVariant])

  • highlight (bool)

  • log_time (bool)

  • log_path (bool)

  • log_time_format (Union[str, rich._log_render.FormatTimeCallable])

  • highlighter (Optional[HighlighterType])

  • legacy_windows (Optional[bool])

  • safe_box (bool)

  • get_datetime (Optional[Callable[[], datetime.datetime]])

  • get_time (Optional[Callable[[], float]])

  • _environ (Optional[Mapping[str, str]])

_flushing = False
print(*args, **kwargs)

Console output function customised for progress bar.

bluemira.base.logs.logger_setup(logfilename: str = 'bluemira.log', *, level: str | int = 'INFO') logging.Logger

Create logger with two handlers.

Parameters:
  • logfilename (str) – Name of file to write logs to, default = bluemira.log

  • level (str | int) – The initial logging level to be printed to the console, default = INFO.

Returns:

The logger object.

Return type:

logging.Logger

Notes

set to debug initially.

bluemira.base.logs.set_log_level(verbose: int | str = 1, *, increase: bool = False, logger_names: collections.abc.Iterable[str] = ('bluemira',))

Get new log level and check if it is possible.

Parameters:
  • verbose (int | str) – Amount the severity level of the logger should be changed by or to

  • increase (bool) – Whether level should be increased by specified amount or changed to it

  • logger_names (collections.abc.Iterable[str]) – The loggers for which to set the level, default = (“bluemira”)

bluemira.base.logs.get_log_level(logger_name: str = 'bluemira', *, as_str: bool = True) str | int

Return the current logging level.

Parameters:
  • logger_name (str) – The named logger to get the level for.

  • as_str (bool) – If True then return the logging level as a string, else as an int.

Returns:

The logging level.

Return type:

str | int

bluemira.base.logs._modify_handler(new_level: LogLevel, logger: logging.Logger)

Change level of the logger from user’s input.

Parameters:
  • new_level (LogLevel) – Severity level for handler to be changed to, from set_log_level

  • logger (logging.Logger) – Logger to be used

class bluemira.base.logs.LoggingContext(level: str | int)

A context manager for temporarily adjusting the logging level

Parameters:

level (str | int) – The bluemira logging level to set within the context.

level
original_level = ''
__enter__()

Set the logging level to the new level when we enter the context.

__exit__(type, value, traceback)

Set the logging level to the original level when we exit the context.