thinlog package#
Thinlog – a lightweight, fully-typed logging toolkit built on Python’s standard logging.
Thinlog extends logging with advanced filtering, structured JSON formatting,
and remote log delivery (HTTP and Telegram) while staying thin and transparent.
Public API:
configure_logging()– set up logging from aLoggingSettingsor dict.get_logger()– create aKeywordFriendlyLoggerand register it.KeywordFriendlyLogger– logger adapter that forwards keyword arguments as extra fields.LoggingSettings– typed dataclass matchinglogging.config.dictConfig()schema.RegisteredLoggers– global registry of logger names used by the wildcard feature.
- class thinlog.KeywordFriendlyLogger(logger: Logger, extra: dict[str, Any] | None = None)[source]#
Bases:
LoggerAdapter[Logger]A
logging.LoggerAdapterthat passes arbitrary keyword arguments as extra fields.Standard
loggingrequires extra data to be wrapped in anextradict.KeywordFriendlyLoggerlets callers pass keyword arguments directly – they are automatically moved into extra so that filters, formatters, and handlers can access them as record attributes.- Parameters:
logger – The underlying
logging.Loggerto adapt.extra – Optional default extra fields attached to every record.
- ignored_meta_keys = ('exc_info', 'stack_info', 'stacklevel')#
- process(msg: Any, metadata: MutableMapping[str, Any]) tuple[Any, MutableMapping[str, Any]][source]#
Move keyword arguments into the extra dict.
Keys listed in
ignored_meta_keysare left in metadata so that the standard logging machinery can handle them.- Parameters:
msg – The log message.
metadata – Keyword arguments passed to the logging call.
- Returns:
A
(msg, metadata)tuple ready for the underlying logger.
- class thinlog.LoggingSettings(version: int = 1, formatters: dict[str, dict[str, ~typing.Any]]=<factory>, filters: dict[str, dict[str, ~typing.Any]]=<factory>, handlers: dict[str, dict[str, ~typing.Any]]=<factory>, loggers: dict[str, dict[str, ~typing.Any]]=<factory>, incremental: bool = False, disable_existing_loggers: bool = False, root: dict[str, ~typing.Any]=<factory>)[source]#
Bases:
objectConfiguration container for
logging.config.dictConfig().All fields mirror the keys accepted by
logging.config.dictConfig(). Convert to a plain dict withdataclasses.asdict()before passing todictConfig(this is handled automatically byconfigure_logging()).- Parameters:
version – Schema version – must be
1.formatters – Formatter definitions keyed by name.
filters – Filter definitions keyed by name.
handlers – Handler definitions keyed by name.
loggers – Logger definitions keyed by name.
incremental – If
True, merge into the existing configuration.disable_existing_loggers – If
True, disable loggers not in loggers.root – Configuration for the root logger.
- disable_existing_loggers: bool = False#
- filters: dict[str, dict[str, Any]]#
- formatters: dict[str, dict[str, Any]]#
- handlers: dict[str, dict[str, Any]]#
- incremental: bool = False#
- loggers: dict[str, dict[str, Any]]#
- root: dict[str, Any]#
- version: int = 1#
- class thinlog.RegisteredLoggers[source]#
Bases:
objectStores logger names so
configure_logging()can apply wildcard configs to them.Loggers are registered automatically when created via
get_logger().- loggers: ClassVar[set[str]] = {}#
- thinlog.configure_logging(name: str, config: LoggingSettings | dict[str, Any], extra: dict[str, Any] | None = None, more_loggers: list[str] | None = None, include_default_logger: bool = False, include_registered_loggers: bool = False, include_root_logger: bool = False, disable_log_errors: bool = True) KeywordFriendlyLogger[source]#
Set up the logging system and return a ready-to-use logger.
Applies the configuration, starts any
QueueHandlerlistener, and registers anatexit()handler that stops the listener and callslogging.shutdown()on interpreter exit.Wildcard loggers: If the config contains a logger named
"*", its settings are copied to every logger listed in more_loggers (and those gathered fromRegisteredLoggers). A specific logger can set"merge": trueto extend rather than replace the wildcard config.- Parameters:
name – Name of the primary logger to return.
config – A
LoggingSettingsinstance or a raw dict suitable forlogging.config.dictConfig().extra – Default extra fields for the returned logger.
more_loggers – Additional logger names to configure via the wildcard.
include_default_logger – If
True, add name to more_loggers.include_registered_loggers – If
True, include all names fromRegisteredLoggers.include_root_logger – If
True, apply the wildcard config to the root logger as well.disable_log_errors – If
True(default), setlogging.raiseExceptionstoFalse.
- Returns:
A
KeywordFriendlyLoggerinstance.
- thinlog.get_logger(name: str, extra: dict[str, Any] | None, register: bool = True) KeywordFriendlyLogger[source]#
Create a
KeywordFriendlyLoggerand optionally register it.- Parameters:
name – Logger name (passed to
logging.getLogger()).extra – Default extra fields attached to every record.
register – If
True(default), add name toRegisteredLoggersso it is picked up by the wildcard feature inconfigure_logging().
- Returns:
A
LoggerAdapterwrapping the named logger.