SNLogger¶
- class snpit_utils.logger.SNLogger(midformat=None, datefmt='%Y-%m-%d %H:%M:%S', show_millisec=False, level=10, handler=None, propagate=False)[source]¶
Bases:
object
Holds a unified logging instance that can be used throughout SNPIT code.
Normal use: just call one of
SNLogger.exception( message ) SNLogger.critical( message ) SNLogger.error( message ) SNLogger.warning( message ) SNLogger.info( message ) SNLogger.debug( message )
and the message will be printed, with a header, to standard error.
If you’re using the logger in a case where you have many processes going via python’s mutiprocessing module, at the beginning of a subprocess you might want to call
SNLogger.multiprocessing_replace()
That will add something to the header of each logging line that includes a process number, making it easier to track down which log messages came from the same process. That process number is parsed from the process’ name, if it has one and there’s a number in it; otherwise, it’s the PID of the process. (Often with something like a multiprocessing pool, processes are named something like ForkPoolWorker-1, where the 1 increments.)
If you want access to the underlying logging.Logger object, call SNLogger.get(). The underlying object is instantiated the first time it’s used. If you want to manually instantiate it, call instance(), with arguments defined in __init__.
The single instance is held inside a SNLogger singleton. (Sort of. The actual singleton changes sometimes.) In principle, you could also make other SNLogger objects, but that’s not the standard way to use it. (In principle, this could be a memory leak, as python logging will remember all of the previously existing loggers. As long as SNLogger.replace() is not called frequently, this should not be a big deal.)
Initialize a SNLogger object, and the logging.Logger object it holds.
- Parameters:
midformat (string, default None) – The standard formatter emits log messages like “[yyyy-mm-dd HH:MM:SS - INFO] Message”. If given, this adds something between the date and the log level (“[yyyy-mm-dd HH:MM:SS - {midformat} - INFO]…”). Useful, for instance, in multiprocessing to keep track of which process the message came from.
datefmt (string, default '%Y-%m-%d %H:%M:%S') – The date format to use, using standard logging.Formatter datefmt syntax.
show_millisec (bool, default False) – Add millseconds after a . following the date formatted by datefmt.
level (logging level constant, default logging.WARNING) – This can be changed later with set_level().
handler (a logging Handler or None) – Normally, SCLogger will send all log messages to sys.stderr. If you want it to go somewhere else, create an approprite logging.Handler subclass and pass it here.
propagate (book, default False) –
cf: python logging.Logger.propagate
We default to False to avoid replication of logging messages if, somehow, there’s another logger above the one we create here. (TODO: understand how python logging ancestors work. I’ve never expicitly set a parent logger, and yet somehow in code loggers often seem to have ancestors.)
Methods Summary
critical
(*args, **kwargs)debug
(*args, **kwargs)error
(*args, **kwargs)exception
(*args, **kwargs)get
()Return the logging.Logger object.
info
(*args, **kwargs)instance
(*args, **kwargs)Return the singleton instance of SNLogger.
multiprocessing_replace
(*args, **kwargs)Shorthand for replace with midformat parsed from the current multiprocessing process.
replace
([midformat, datefmt, show_millisec, ...])Replace the logging.Logger object with a new one.
setLevel
([level])Set the log level of the logging.Logger object.
set_level
([level])Set the log level of the logging.Logger object.
warning
(*args, **kwargs)Methods Documentation
- classmethod instance(*args, **kwargs)[source]¶
Return the singleton instance of SNLogger.
The first time this is called, it will create the object. Normally, you never need to call this class method explicitly, as all the various other class methods call it. If you want any of the defaults to be different from the defaults set in __init__ below, you will need to call this class method before the first time you use SNLogger for anything else. (However, you can also call SNLogger.replace() to change the options used.)
- classmethod multiprocessing_replace(*args, **kwargs)[source]¶
Shorthand for replace with midformat parsed from the current multiprocessing process.
- classmethod replace(midformat=None, datefmt=None, show_millisec=None, level=None, handler=None)[source]¶
Replace the logging.Logger object with a new one.
Subsequent calls to SNLogger.get(), .info(), etc. will now return the new one. Will inherit the various arguments from the current logger if they aren’t specified here.
See __init__ for parameters.
Returns the logging.Logger object you’d get from get().