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: Optional[str] = None,
)

Fix loggers to use str.format().

log(
level: int,
msg: Any,
*args: Any,
exc_info: Union[None, bool, Tuple[type, BaseException, Optional[TracebackType]], Tuple[None, None, None], BaseException] = None,
stack_info: bool = False,
extra: Optional[Mapping[str, object]] = None,
**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. stacklevel is supported, but is silently ignored in Python 3.7.

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

Cycle log files, then give the required file handler.

srctools.logger.get_logger(
name: str = '',
alias: Optional[str] = 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: Union[str, _os.PathLike[str]] = None,
main_logger: str = '',
on_error: Callable[[Type[BaseException], BaseException, Optional[TracebackType]], None] = None,
) Logger

Set up the logger and logging handlers.

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

Parameters:
  • filename – If this is set, all logs will be written to this file as well.
  • on_error – should be a function to call when uncaught exceptions are thrown, taking (type, value, traceback). If the exception is a BaseException, the app will quit silently.
  • 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.