fundamentals.logs¶
Logger setup for python projects
- Author
David Young
Module Contents¶
Classes¶
rotating file handler for logging |
|
A fake logger object so user can set |
Functions¶
Setup and return a console logger |
|
setup dryx style python logging |
API¶
- class fundamentals.logs.ColorFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source][source]¶
Bases:
logging.Formatter- format[source][source]¶
‘%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)’
- class fundamentals.logs.GroupWriteRotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False, errors=None)[source][source]¶
Bases:
logging.handlers.RotatingFileHandlerrotating file handler for logging
Initialization
Open the specified file and use it as the stream for logging.
By default, the file grows indefinitely. You can specify particular values of maxBytes and backupCount to allow the file to rollover at a predetermined size.
Rollover occurs whenever the current log file is nearly maxBytes in length. If backupCount is >= 1, the system will successively create new files with the same pathname as the base file, but with extensions “.1”, “.2” etc. appended to it. For example, with a backupCount of 5 and a base file name of “app.log”, you would get “app.log”, “app.log.1”, “app.log.2”, … through to “app.log.5”. The file being written to is always “app.log” - when it gets filled up, it is closed and renamed to “app.log.1”, and if files “app.log.1”, “app.log.2” etc. exist, then they are renamed to “app.log.2”, “app.log.3” etc. respectively.
If maxBytes is zero, rollover never occurs.
- fundamentals.logs.addLoggingLevel(levelName, levelNum, methodName=None)[source][source]¶
-
Comprehensively adds a new logging level to the
loggingmodule and the currently configured logging class.levelNamebecomes an attribute of theloggingmodule with the valuelevelNum.methodNamebecomes a convenience method for bothloggingitself and the class returned bylogging.getLoggerClass()(usually justlogging.Logger). IfmethodNameis not specified,levelName.lower()is used.To avoid accidental clobberings of existing attributes, this method will raise an
AttributeErrorif the level name is already an attribute of theloggingmodule or if the method name is already presentExample
addLoggingLevel(‘TRACE’, logging.DEBUG - 5) logging.getLogger(name).setLevel(“TRACE”) logging.getLogger(name).trace(‘that worked’) logging.trace(‘so did this’) logging.TRACE 5
- fundamentals.logs.console_logger(level='WARNING')[source][source]¶
Setup and return a console logger
Key Arguments
level– the level of logging required
Return
logger– the console logger
Usage
from fundamentals import logs log = logs.console_logger( level="DEBUG" ) log.debug("Testing console logger")
- class fundamentals.logs.emptyLogger[source][source]¶
Bases:
objectA fake logger object so user can set
log=Falseif requiredUsage
if log == False: from fundamentals.logs import emptyLogger log = emptyLogger()
- fundamentals.logs.setup_dryx_logging(yaml_file)[source][source]¶
setup dryx style python logging
Key Arguments
level– the level of logging required
Return
logger– the console logger
Usage
from fundamentals import logs log = logs.setup_dryx_logging( yaml_file="/Users/Dave/.config/fundamentals/fundamentals.yaml" ) log.error("test error")
Here is an example of the settings in the yaml file:
version: 1 logging settings: formatters: file_style: format: '* %(asctime)s - %(name)s - %(levelname)s (%(pathname)s > %(funcName)s > %(lineno)d) - %(message)s ' datefmt: '%Y/%m/%d %H:%M:%S' console_style: format: '* %(asctime)s - %(levelname)s: %(pathname)s:%(funcName)s:%(lineno)d > %(message)s' datefmt: '%H:%M:%S' html_style: format: '<div id="row" class="%(levelname)s"><span class="date">%(asctime)s</span> <span class="label">file:</span><span class="filename">%(filename)s</span> <span class="label">method:</span><span class="funcName">%(funcName)s</span> <span class="label">line#:</span><span class="lineno">%(lineno)d</span> <span class="pathname">%(pathname)s</span> <div class="right"><span class="message">%(message)s</span><span class="levelname">%(levelname)s</span></div></div>' datefmt: '%Y-%m-%d <span class= "time">%H:%M <span class= "seconds">%Ss</span></span>' handlers: console: class: logging.StreamHandler level: DEBUG formatter: console_style stream: ext://sys.stdout file: class: logging.handlers.GroupWriteRotatingFileHandler level: WARNING formatter: file_style filename: /Users/Dave/.config/fundamentals/fundamentals.log mode: w+ maxBytes: 102400 backupCount: 1 root: level: WARNING handlers: [file,console]