Logging System

The module extended the builtin logging module and provides a coloured version of Logger to better demonstrate the logging information of darc.

class darc.logging.ColorFormatter(fmt=None, datefmt=None, style='%', validate=True)[source]

Bases: logging.Formatter

Color formatter based on record levels.

format(record)[source]

Format the specific record as text.

Parameters

record (LogRecord) – logging record

Return type

str

Returns

Formatted logging message.

See also

_LOG_ATTR contains the color mapping for each logging level.

class darc.logging.DarcLogger(name, level=0)[source]

Bases: logging.Logger

The tailored logger for darc module.

pexc(level=40, message=None, category=None, line=None, **kwargs)[source]

Log msg % args with severity level.

Parameters
  • level (int) – Log severity level.

  • message (Optional[str]) – Optional log message in additional to the error message.

  • line (Optional[str]) – Optional source line of code (as comments).

  • category (Optional[Type[Warning]]) – Warning category.

  • kwargs (Any) –

Keyword Arguments

**kwargs – Arbitrary keyword arguments for log information.

Return type

None

To pass exception information, use the keyword argument exc_info with a true value, e.g.

logger.pexc("Houston, we have a thorny problem", type, exc_info=1)

See also

The method mocks the output of warnings.warn() for the log message.

Return type

None

Parameters
  • level (int) –

  • message (Optional[str]) –

  • category (Optional[Type[Warning]]) –

  • line (Optional[str]) –

  • kwargs (Any) –

pline(level, msg, *args, **kwargs)[source]

Log msg % args with severity level.

Parameters
  • level (int) – Log severity level.

  • msg (str) – Message to be logged.

  • *args – Arbitrary positional arguments for interploration.

  • args (Any) –

  • kwargs (Any) –

Keyword Arguments

**kwargs – Arbitrary keyword arguments for log information.

Return type

None

To pass exception information, use the keyword argument exc_info with a true value, e.g.

logger.pline(level, "Houston, we have a %s", "thorny problem", exc_info=1)

Note

The method replaces formatter with line_formatter, which has not prefixing contents in the log line.

Return type

None

Parameters
  • level (int) –

  • msg (str) –

  • args (Any) –

  • kwargs (Any) –

plog(level, msg, *args, object, pprint=None, **kwargs)[source]

Log msg % args into a pretty-printed representation with severity level.

Parameters
  • level (int) – Log severity level.

  • msg (Any) – Message to be logged.

  • *args – Arbitrary positional arguments for interploration.

  • args (Any) –

  • object (Any) –

  • pprint (Optional[dict[str, Any]]) –

  • kwargs (Any) –

Keyword Arguments
  • object – Object to be pretty printed.

  • pprint – Arbitrary arguments for pprint.pformat().

  • **kwargs – Arbitrary keyword arguments for log information.

Return type

None

To pass exception information, use the keyword argument exc_info with a true value, e.g.

logger.plog(level, "Houston, we have a %s", "thorny problem", object=object, exc_info=1)

See also

The method uses pprint.pformat() to format the target object with pprint arguments.

ptb(msg, *args, level=50, **kwargs)[source]

Log msg % args with severity level.

Parameters
  • msg (str) – Message to be logged.

  • *args – Arbitrary positional arguments for interploration.

  • args (Any) –

  • level (int) –

  • kwargs (Any) –

Keyword Arguments
  • level – Log severity level.

  • **kwargs – Arbitrary keyword arguments for log information.

Return type

None

To pass exception information, use the keyword argument exc_info with a true value, e.g.

logger.ptb(level, "Houston, we have a %s", "thorny problem", exc_info=1)

Note

The method logs the full traceback stack from traceback.format_exc().

Return type

None

Parameters
  • msg (str) –

  • args (Any) –

  • level (int) –

  • kwargs (Any) –

verbose(msg, *args, **kwargs)[source]

Log msg % args with severity VERBOSE.

Parameters
  • msg (Any) – Message to be logged.

  • *args – Arbitrary positional arguments for interploration.

  • **kwargs – Arbitrary keyword arguments for log information.

  • args (Any) –

  • kwargs (Any) –

Return type

None

To pass exception information, use the keyword argument exc_info with a true value, e.g.

logger.verbose("Houston, we have a %s", "thorny problem", exc_info=1)
Return type

None

Parameters
  • msg (Any) –

  • args (Any) –

  • kwargs (Any) –

property horizon: str

Horizon line.

See also

The property uses shutil.get_terminal_size() to calculate the desired length of the - horizon line.

Return type

str

darc.logging.render_message(message, *attr)[source]

Render message.

The function wraps the stem.util.term.format() function to provide multi-line formatting support.

Parameters
  • message (AnyStr) – Multi-line message to be rendered with colour.

  • *attr – Formatting attributes of text, c.f. stem.util.term.

  • attr (str) –

Return type

str

Returns

The rendered message.

See also

The message formatting is done by stem.util.term.format() with its various predefined formatting attributes.

darc.logging.CRITICAL = 50

CRITICAL logging level, c.f., logging.CRITICAL.

darc.logging.DEBUG = 10

DEBUG logging level, c.f., logging.DEBUG.

darc.logging.ERROR = 40

ERROR logging level, c.f., logging.ERROR.

darc.logging.INFO = 20

INFO logging level, c.f., logging.INFO.

darc.logging.VERBOSE = 5

VERBOSE logging level.

darc.logging.WARNING = 30

WARNING logging level, c.f., logging.WARNING.

darc.logging._LOG_ATTR = {5: ('Blue',), 10: ('Cyan',), 20: ('Green',), 30: ('Yellow',), 40: ('Red',), 50: ('Bg Red', 'Highlight')}

Mapping of logging levels with corresponding color format.

Type

Dict[int, Tuple[str]]

darc.logging.formatter = <darc.logging.ColorFormatter object>

Generic log formatter instance for handler.

darc.logging.handler = <StreamHandler <stderr> (NOTSET)>

Log handler instance for logger.

darc.logging.line_formatter = <darc.logging.ColorFormatter object>

Line-only log formmater instance for pline().

darc.logging.logger = <DarcLogger darc (INFO)>

Logger instance for darc.

Type

darc.logging.DarcLogger