13.3. Logging Formatters
SetUp:
>>> import logging
13.3.1. General
name
- Name of the logger used to log the calllevelname
- Text logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL)message
- The logged message, computed asmsg % args
. This is set whenFormatter.format
is invoked
>>> logging.basicConfig(format='%(levelname)s %(message)s')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')
13.3.2. Dates
asctime
- Human-readable time whenLogRecord
was created, example: 1969-07-21 02:56:15,123created
- Time when theLogRecord
was created (as returned bytime.time
)msecs
- Millisecond portion of the time when theLogRecord
was created
>>> logging.basicConfig(format='%(asctime).19s %(message)s')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')
13.3.3. File and Module
pathname
- Full pathname of the source file where the logging call was issued (if available)filename
- Filename portion ofpathname
module
- Module (name portion offilename
)funcName
- Name of function containing the logging calllineno
- Source line number where the logging call was issued (if available)
>>> logging.basicConfig(format='%(funcName)s:%(lineno)s %(message)s')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')
13.3.4. Process and Thread
process
- Process ID (if available)processName
- Process name (if available)thread
- Thread ID (if available)threadName
- Thread name (if available)
13.3.5. Other
You shouldn't need to format this yourself
args
- The tuple of arguments merged intomsg
to producemessage
, or a dict whose values are used for the merge (when there is only one argument, and it is a dictionary)exc_info
- Exception tuple (à lasys.exc_info
) if no exception has occurred,None
msg
- The format string passed in the original logging call. Merged withargs
to producemessage
, or an arbitrary objectstack_info
- Stack frame information (where available) from the bottom of the stack in the current thread, up to and including the stack frame of the logging call which resulted in the creation of this recordlevelno
- Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL)relativeCreated
- Time in milliseconds when theLogRecord
was created, relative to the time the logging module was loaded
13.3.6. Date Format
>>> import logging
>>>
>>>
>>> logging.basicConfig(
... format='%(asctime)s %(levelname)s %(message)s',
... datefmt='"%Y-%m-%d" "%H:%M:%S"',)
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')
13.3.7. Log Style
{
- curly brackets; compare to f-string formatting%
- percent sign; compare to formatting string with%
$
- dollar sign; compare to template vars from other languagesDefault mode is
%
percent
%
- percent sign (default):
>>> logging.basicConfig(
... format='%(asctime)s %(levelname)s %(message)s',
... style='%')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')
{
- curly brackets:
>>> logging.basicConfig(
... format='{asctime} {levelname} {message}',
... style='{')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')
$
- dollar sign:
>>> logging.basicConfig(
... format='$asctime $levelname $message',
... style='$')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')
13.3.8. Use Case - 1
CSV log format
>>> import logging
>>>
>>>
>>> logging.basicConfig(
... level='DEBUG',
... datefmt='"%Y-%m-%d" "%H:%M:%S"',
... format='{asctime}, "{levelname}", "{message}"',
... style='{',
... filename='/tmp/myfile.csv')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')