This topic describes how to use Logtail to collect Python logs.
Background information
The logging module of Python is a logging system that is compatible with third-party modules or applications. The logging module defines multiple log severity levels and logging methods. The logging module consists of four components: loggers, handlers, filters, and formatters.
Formatters specify the output format of logs. The fields in the configurations of
a formatter are in the %(key)s format.
import logging
import logging.handlers
LOG_FILE = 'tst.log'
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # Create a handler object.
%(asctime)s - %(filename)s:%(lineno)s - %(levelno)s %(levelname)s %(pathname)s %(module)s %(funcName)s %(created)f %(thread)d %(threadName)s %(process)d %(name)s - %(message)s // Define the output format of logs.
formatter = logging.Formatter(fmt) # Create a formatter object.
handler.setFormatter(formatter) # Add the formatter to the handler.
logger = logging.getLogger('tst') # Retrieve a logger that is named tst.
logger.addHandler(handler) # Add the handler to the logger.
logger.setLevel(logging.DEBUG)
logger.info('first info message')
logger.debug('first debug message')
The following table describes the fields in the formatter configurations.
Field | Description |
---|---|
%(name)s | The name of the logger that generates a log. |
%(levelno)s | The severity level of a log in the numeric format. Valid values: 10, 20, 30, 40, and 50. |
%(levelname)s | The severity level of a log in the text format. Valid values: DEBUG, INFO, WARNING, ERROR, and CRITICAL. |
%(pathname)s | The full path name of the source file where the logging call is initiated. |
%(filename)s | The name of the source file. |
%(module)s | The name of the module where the logging call is initiated. |
%(funcName)s | The name of the function from which the logging call is initiated. |
%(lineno)d | The line number in the source file where the logging call is initiated. |
%(created)f | The time when a log is created. The value is a Unix timestamp. It represents the number of seconds that have elapsed since January 1, 1970, 00:00:00 (UTC). |
%(relativeCreated)d | The difference between the time when a log is created and the time when the logging module is loaded. Unit: milliseconds. |
%(asctime)s | The time when a log is created. Example: 2003-07-08 16:49:45,896. The digits after the comma (,) indicate the millisecond portion of the time. |
%(msecs)d | The millisecond portion of the time when a log is created. |
%(thread)d | The ID of the thread. |
%(threadName)s | The name of the thread. |
%(process)d | The ID of the process. |
%(message)s | The log content. |
The following example shows sample log entries:
2015-03-04 23:21:59,682 - log_test.py:16 - tst - first info message
2015-03-04 23:21:59,682 - log_test.py:17 - tst - first debug message