The easiest way that comes to mind is to simply write a log message after adding formatting and a handler, but before setting the logger level. If the registration system is fully configured by your program, then no registrar will set the level until you explicitly set it, and therefore all messages will be recorded until you set the level.
Another way is to set a filter instead of setting the log level. A filter can reject all messages below a certain level, unless the message matches the specified template or until you call the filter enable method or some such thing. However, you sacrifice some efficiency; The standard implementation of Logger
designed to immediately check the level when any of the logging methods is called, and to discard the message if it is not at a sufficiently high level, while filters are checked later in the process.
If something else outside your code already sets the log level to WARNING
, and you cannot control it, then it really isnโt. You will have to either temporarily reset the logger level (which can become messy if you do not use a top-level logger, since you need to check and reset the level on several โgenerationsโ of loggers) or set your own Logger
subclass before creating any logs. In this case, I would say that it is probably not your responsibility to verify that the logging system works anyway.
source share