Log

class dman.core.log.IndentedFormatter(fmt='%(indent)s%(label)s%(message)s', datefmt=None, style='%', validate=True, capitalize_levelname: bool = False)[source]

Formatter that supports indentation and label specification.

New formatting options: indent and label

Example:

>>> fmt = IndentedFormatter(fmt="%(levelname)s: %(indent)s%(label)s%(message)s")
>>> logger = Logger('example', level=INFO)
>>> h = backend.StreamHandler()
>>> h.setFormatter(formatter)
>>> log.addHandler(hdlr)
>>> with log.layer('value', 'label'):
...     log.info('test')
...     log.info('test', 'label')
<label type=value>
test
[label]: test
<end label type=value>
format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

dman.core.log.format_type(obj)[source]

Get string label for type.

dman.core.log.default_formatter(fmt: str = '%(indent)s%(label)s%(message)s', datefmt: Optional[str] = None, capitalize_levelname: bool = False)[source]

Return the default dman formatter.

Parameters:
  • fmt (str, optional) – Format string.

  • datefmt (str, optional) – Date format specification. Defaults to None.

  • capitalize_levelname (bool, optional) – Capitalize the log-level name. Defaults to False.

class dman.core.log.LoggingHighlighter[source]

Apply coloring to dman log messages. Highlights label, headers, paths and strings.

class dman.core.log.MinimalHighlighter[source]

Only highlight label.

class dman.core.log.ColorHighlighter(base_color=None, base=<class 'dman.core.log.LoggingHighlighter'>)[source]

Highlights all basic text with specified color, still applying base to get specific colors for other pieces of text.

highlight(text)[source]

Apply highlighting in place to text.

Parameters:

text (~Text) – A text object highlight.

dman.core.log.get_highlighter(color: str, minimal: bool)[source]

Get a highlighter.

Parameters:
  • color (str) – The color of the base text.

  • minimal (bool) – When set to False, LoggingHighlighter is applied to the string after setting the base color.

dman.core.log.trace2rich(trace: Trace)[source]

Convert traceback to internal representation used by rich.

class dman.core.log.DManHandler(level: Union[int, str] = 0, console: Optional[Console] = None, *, show_time: bool = True, omit_repeated_times: bool = True, show_level: bool = True, show_path: bool = True, enable_link_path: bool = True, highlighter: Optional[Highlighter] = None, markup: bool = False, rich_tracebacks: bool = False, tracebacks_width: Optional[int] = None, tracebacks_extra_lines: int = 3, tracebacks_theme: Optional[str] = None, tracebacks_word_wrap: bool = True, tracebacks_show_locals: bool = False, tracebacks_suppress: Iterable[Union[str, module]] = (), locals_max_length: int = 10, locals_max_string: int = 80, log_time_format: Union[str, Callable[[datetime], Text]] = '[%x %X]', keywords: Optional[List[str]] = None)[source]

The default logging handler used by dman.

emit(record: LogRecord) None[source]

Invoked by logging.

render(*, record: LogRecord, traceback, message_renderable)[source]

Render log for display.

Parameters:
  • record (LogRecord) – logging Record.

  • traceback (Optional[Traceback]) – Traceback instance or None for no Traceback.

  • message_renderable (ConsoleRenderable) – Renderable (typically Text) containing log message contents.

Returns:

Renderable to display log.

Return type:

ConsoleRenderable

dman.core.log.default_handler(stream=None, use_rich: bool = True, **kwargs)[source]

Get the default handler used by dman.

The stream is passed to the handler, which is either a standard backend.StreamHandler or a DManHandler instance. Any other keyword arguments are passed to the __init__ method of the handler.

Additionally the option console_style can be set to a dictionary. The keyword arguments contained within are passed to the rich.console.Console initializer and the output console is passed to the DManHandler.

dman.core.log.default_config(level: Optional[int] = None)[source]

Load the default logger configuration.

dman.core.log.config(*, level=None, filename: Optional[str] = None, filemode: str = 'a', stream=None, format: Optional[str] = None, datefmt: Optional[str] = None, handlers: Optional[List[Handler]] = None, force: bool = False, **kwargs)[source]

Configure the dman logger.

This function does nothing if the dman logger already has handlers configured, unless if the force keyword argument is set to true.

The default behavior is to create a DmanHandler using the default_handler() method. Any keyword arguments not specified below are passed to that method.

Parameters:
  • level (_type_, optional) – Set the logger level to the specified level.

  • filename (str, optional) – Specifies that a FileHandler be created, using the specified filename, rather than a StreamHandler.

  • filemode (str, optional) – Specifies the mode to open the file, if filename is specified (if filemode is unspecified, it defaults to ‘a’).

  • stream (optional) – Use the specified stream to initialize the StreamHandler. Note that this argument is incompatible with ‘filename’ - if both are present, ‘stream’ is ignored.

  • format (str, optional) – Use the specified format string for the handler.

  • datefmt (str, optional) – Use the specified date/time format.

  • handlers (List[backend.Handler], optional) – If specified, this should be an iterable of already created handlers, which will be added to the root handler. Any handler in the list which does not have a formatter assigned will be assigned the formatter created in this function.

  • force (bool, optional) – If this keyword is specified as true, any existing handlers attached to the root logger are removed and closed, before carrying out the configuration as specified by the other arguments.. Defaults to False.

Note that you could specify a stream created using open(filename, mode) rather than passing the filename and mode in. However, it should be remembered that StreamHandler does not close its stream (since it may be using sys.stdout or sys.stderr), whereas FileHandler closes its stream when the handler is closed.

class dman.core.log.Logger(name: str, level=0, stack: Optional[list] = None)[source]

Logger instance used by dman. Provides some additional logging methods.

info(msg: str, label: Optional[str] = None, color: Optional[str] = None, use_rich_highlighter: bool = False, *args, **kwargs)[source]

Log an info message.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • color (str, optional) – The color of the text. Defaults to None.

  • use_rich_highlighter (bool, optional) – Use rich highlighting. Defaults to False.

debug(msg: str, label: Optional[str] = None, *args, **kwargs)[source]

Log a debug message.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

warning(msg: str, label: Optional[str] = None, exc_info=False, *args, **kwargs)[source]

Log a warning message

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • exc_info (bool, optional) – Add exception info. Defaults to False.

error(msg: str, label: Optional[str] = None, exc_info=False, *args, **kwargs)[source]

Log an error message

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • exc_info (bool, optional) – Add exception info. Defaults to False.

exception(msg: str, label: Optional[str] = None, exc_info=True, *args, **kwargs)[source]

Log an exception

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • exc_info (bool, optional) – Add exception info. Defaults to True.

emphasize(msg: str, label: Optional[str] = None, *args, **kwargs)[source]

Log an emphasized info message.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

io(msg: str, label: Optional[str] = None, *args, **kwargs)[source]

Log an info message associated with io.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

header(msg: str, label: str, prefix: str = 'type', *args, **kwargs)[source]

Log an info header.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • prefix (str, optional) – Prefix added before the message. Defaults to “type”.

layer(msg: str, label: str = None, prefix: str = 'type', owner: str = None, *args, **kwargs)[source]

Enter a layer context, indenting all future log messages.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • owner (Str, optional) – Owner of the layer, which is added to the stack. Defaults to None.

  • prefix (str, optional) – Prefix added before the message. Defaults to “type”.

makeRecord(*args, **kwargs)[source]

A factory method which can be overridden in subclasses to create specialized LogRecords.

dman.core.log.info(msg: str, label: Optional[str] = None, color: Optional[str] = None, use_rich_highlighter: bool = False)[source]

Log an info message.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • color (str, optional) – The color of the text. Defaults to None.

  • use_rich_highlighter (bool, optional) – Use rich highlighting. Defaults to False.

dman.core.log.debug(msg: str, label: Optional[str] = None)[source]

Log a debug message.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

dman.core.log.warning(msg: str, label: Optional[str] = None, exc_info=False)[source]

Log a warning message

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • exc_info (bool, optional) – Add exception info. Defaults to False.

dman.core.log.error(msg: str, label: Optional[str] = None, exc_info=False, stacklevel=1)[source]

Log an error message

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • exc_info (bool, optional) – Add exception info. Defaults to False.

dman.core.log.exception(msg: str, label: Optional[str] = None, exc_info=True)[source]

Log an exception

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • exc_info (bool, optional) – Add exception info. Defaults to True.

dman.core.log.emphasize(msg: str, label: Optional[str] = None)[source]

Log an emphasized info message.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

dman.core.log.io(msg: str, label: Optional[str] = None)[source]

Log an info message associated with io.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

dman.core.log.layer(msg: str, label: Optional[str] = None, prefix: str = 'type', owner: Optional[str] = None)[source]

Enter a layer context, indenting all future log messages.

Parameters:
  • msg (str) – message

  • label (str, optional) – The label added before the message (if specified in format string). Defaults to None.

  • owner (Str, optional) – Owner of the layer, which is added to the stack. Defaults to None.

  • prefix (str, optional) – Prefix added before the message. Defaults to “type”.

dman.core.log.logger_context(level: bool = None)[source]

Enter a logger context with a temporary log level.

Example:

>>> with logger_context(level=INFO):
...     info('test', 'label')