logging.LogRecord

class logging.LogRecord(name, level, pathname, lineno, msg, args, exc_info, func=None, sinfo=None)

Contains all the information pertinent to the event being logged.

The primary information is passed in msg and args, which are combined using msg % args to create the message field of the record.

Parameters:
  • name – The name of the logger used to log the event represented by this LogRecord. Note that this name will always have this value, even though it may be emitted by a handler attached to a different (ancestor) logger.
  • level – The numeric level of the logging event (one of DEBUG, INFO etc.) Note that this is converted to two attributes of the LogRecord: levelno for the numeric value and levelname for the corresponding level name.
  • pathname – The full pathname of the source file where the logging call was made.
  • lineno – The line number in the source file where the logging call was made.
  • msg – The event description message, possibly a format string with placeholders for variable data.
  • args – Variable data to merge into the msg argument to obtain the event description.
  • exc_info – An exception tuple with the current exception information, or None if no exception information is available.
  • func – The name of the function or method from which the logging call was invoked.
  • sinfo – A text string representing stack information from the base of the stack in the current thread, up to the logging call.
getMessage()

Returns the message for this LogRecord instance after merging any user-supplied arguments with the message. If the user-supplied message argument to the logging call is not a string, str() is called on it to convert it to a string. This allows use of user-defined classes as messages, whose __str__ method can return the actual format string to be used.

Changed in version 3.2: The creation of a LogRecord has been made more configurable by providing a factory which is used to create the record. The factory can be set using getLogRecordFactory() and setLogRecordFactory() (see this for the factory’s signature).

This functionality can be used to inject your own values into a LogRecord at creation time. You can use the following pattern:

old_factory = logging.getLogRecordFactory()

def record_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.custom_attribute = 0xdecafbad
    return record

logging.setLogRecordFactory(record_factory)

With this pattern, multiple factories could be chained, and as long as they don’t overwrite each other’s attributes or unintentionally overwrite the standard attributes listed above, there should be no surprises.

doc_python
2016-10-07 17:36:24
Comments
Leave a Comment

Please login to continue.