srctools.logger

Wrapper around logging to provide our own functionality.

This adds the ability to log using str.format() instead of %.

class srctools.logger.LoggerAdapter(
logger: Logger,
alias: str | None = None,
)

Fix loggers to use str.format().

log(
level: int,
msg: Any,
*args: Any,
exc_info: None | bool | Tuple[Type[BaseException], BaseException, TracebackType | None] | Tuple[None, None, None] | BaseException = None,
stack_info: bool = False,
extra: Mapping[str, object] | None = None,
stacklevel: int = 0,
**kwargs: Any,
) None

This version of log() is for str.format() compatibility.

The message is wrapped in a LogMessage object, which is given the args and kwargs.

srctools.logger.get_handler(
filename: str | PathLike[str],
) FileHandler

Cycle log files, then give the required file handler.

srctools.logger.get_logger(
name: str = '',
alias: str | None = None,
) Logger

Get the named logger object.

This puts the logger into the srctools namespace, and wraps it to use str.format() instead of % formatting. If set, alias is the name to show for the module.

srctools.logger.init_logging(
filename: StringPath | None = None,
main_logger: str = '',
on_error: Callable[[Type[BaseException], BaseException, TracebackType | None], None] | None = None,
) Logger
srctools.logger.init_logging(
filename: StringPath | None = None,
main_logger: str = '',
*,
error: Callable[[BaseException], object],
) Logger

Set up the logger and logging handlers.

This also sets sys.excepthook(), so uncaught exceptions are captured.

If an unhandled BaseException is raised, this will not log or call the callback.

Parameters:
  • filename – If this is set, all logs will be written to this file as well.

  • error – should be a function to call when uncaught exceptions are thrown.

  • on_error – Deprecated version of error with the old-style exception tuple representation from Python 3.10 and below.

  • main_logger – Specify the name of the logger to produce under the srctools hierachy.

srctools.logger.context(name: str) Generator[str, None, None]

Context manager to allow specifying additional information for any logs contained in this block.

The specified string gets included in the log messages.