heros.logging¶
Methods and classes for logging capabilities of HEROs
Attributes¶
Classes¶
Attribute all log records emitted within this context to a specific HERO. |
|
Instances of the Logger class represent a single logging channel. A |
|
Logging formatter that adds ANSI colors to logging output. |
Functions¶
|
Register a LocalHERO as living in this process. |
|
Remove a LocalHERO from the set of HEROs living in this process. |
|
Resolve the HERO name that log records emitted now belong to. |
|
Install a log record factory that stamps every record with the origin HERO name. |
|
Module Contents¶
- heros.logging.SPAM = 5¶
- heros.logging._current_hero: contextvars.ContextVar¶
- heros.logging._registered_heros: set[str]¶
- heros.logging.register_hero(name: str) None[source]¶
Register a LocalHERO as living in this process.
Records emitted from contexts without an active HERO (e.g. background threads of dependencies) are attributed to the registered HERO.
- heros.logging.unregister_hero(name: str) None[source]¶
Remove a LocalHERO from the set of HEROs living in this process.
- heros.logging.resolve_origin() str[source]¶
Resolve the HERO name that log records emitted now belong to.
- Returns:
The name of the currently active HERO if set, else the name of the single HERO registered in this process,
PID:<pid>if no or more than one HERO is registered
- class heros.logging.hero_logging_context(name: str = 'unknown', name_attr: str | None = None)[source]¶
Bases:
contextlib.ContextDecoratorAttribute all log records emitted within this context to a specific HERO.
While the context is active, every log record created in it is stamped with the name of the HERO, so that records emitted by dependencies of a heroized class can be attributed to the HERO whose capability is currently executed.
Can be used in three ways:
as a context manager:
`with hero_logging_context("my_hero"): ...`as a function or method decorator:
`@hero_logging_context("my_hero")`uses a fixed name, while`@hero_logging_context(name_attr="_name")`reads the name at call time from the instance the decorated method belongs toas a class decorator: @hero_logging_context(name_attr=”name”) wraps all plain-function methods of the class (and its base classes).
- Parameters:
name – name of the HERO
name_attr – name of the attribute of the instance the decorated method belongs to. Takes precedence over name.
- name = 'unknown'¶
- name_attr = None¶
- _tokens: list[contextvars.Token] = []¶
- heros.logging._install_hero_record_factory() None[source]¶
Install a log record factory that stamps every record with the origin HERO name.
The factory wraps the currently installed factory (idempotently, guarded by a marker attribute) and sets record.origin on every created record. Since the record factory is invoked for each record at creation time, before any handler or propagation logic, this also covers third-party loggers and loggers with propagate=False.
- class heros.logging.Logger(*args, **kwargs)[source]¶
Bases:
logging.LoggerInstances of the Logger class represent a single logging channel. A “logging channel” indicates an area of an application. Exactly how an “area” is defined is up to the application developer. Since an application can have any number of areas, logging channels are identified by a unique string. Application areas can be nested (e.g. an area of “input processing” might include sub-areas “read CSV files”, “read XLS files” and “read Gnumeric files”). To cater for this natural nesting, channel names are organized into a namespace hierarchy where levels are separated by periods, much like the Java or Python package namespace. So in the instance given above, channel names might be “input” for the upper level, and “input.csv”, “input.xls” and “input.gnu” for the sub-levels. There is no arbitrary limit to the depth of nesting.
- class heros.logging.ColoredFormatter(*args, fmt: str = '%(asctime)-15s [%(levelname)s] %(name)s@%(origin)s [%(filename)s:%(lineno)d %(funcName)s]: %(message)s', **kwargs)[source]¶
Bases:
logging.FormatterLogging formatter that adds ANSI colors to logging output.
- RESET = '\x1b[0m'¶
- RED = '\x1b[31m'¶
- GREEN = '\x1b[32m'¶
- YELLOW = '\x1b[33m'¶
- supports_color¶
- 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.
- heros.logging.get_logger(name: str = 'heros') logging.Logger[source]¶
- heros.logging.log¶