The Python logging module logging is used through configuration files,
Vim logger_config.ini
[Loggers]
Keys = root, infoLogger, errorlogger
[Logger_root]
Level = DEBUG
Handlers = infohandler, errorhandler
[Logger_infoLogger]
Handlers = infohandler
Qualname = infoLogger
Propagate = 0
[Logger_errorlogger]
Handlers = errorhandler
Qualname = errorlogger
Propagate = 0
######################################## #######
[Handlers]
Keys = infohandler, errorhandler
[Handler_infohandler]
Class = StreamHandler
Level = INFO
Formatter = form02
Args = (sys. stdout ,)
[Handler_errorhandler]
Class = FileHandler
Level = ERROR
Formatter = form01
Args = ('logs/mylog. log', 'A ')
######################################## #######
[Formatters]
Keys = form01, form02
[Formatter_form01]
Format = % (asctime) s % (filename) s % (levelname) s % (message) s
Datefmt = % Y-% m-% d % H: % M: % S
[Formatter_form02]
Format = % (asctime) s % (filename) s % (levelname) s % (message) s
Datefmt = % Y-% m-% d % H: % M: % S
Field description:
[Loggers]
# Define the logger module. root is the parent class and must exist. Others are custom.
# Logging. getLogger (NAME) is equivalent to registering a log print with the logging Module
# Use. In name to indicate the inheritance relationship of log
[Handlers]
# Define handler
[Formatters]
# Define the formatting output [logger_root]
# The Implementation of the logger module defined above must be in the form of [logger_xxxx]
# [Logger_xxxx] logger _ Module name
# Level: DEBUG, INFO, WARNING, ERROR, and CRITICAL
# Handlers processing classes, which can be separated by commas
# Qualname: logger name, which is obtained by the application through logging. getLogger. For names that cannot be obtained, record them to the root module.
# Whether propagate inherits the log information of the parent class, 0: No 1: Yes
[Handler_infohandler]
# [Handler_xxxx]
# Class handler class Name
# Level Log level
# Formatter, The formatter defined above
# Args handler initialize function parameters
[Formatter_form01]
# Log format
#--------------------------------------------------
# % (Asctime) s year-month-day hour-minute-second, millisecond 20:10:43, 745
# % (Filename) s file name, excluding Directories
# % (Pathname) s directory name, full path
# % (FuncName) s function name
# % (Levelname) s-level name
# % (Lineno) d row number
# % (Module) s module name
# % (Message) s message body
# % (Name) s Log Module name
# % (Process) d process id
# % (ProcessName) s process name
# % (Thread) d thread id
# % (ThreadName) s thread name
Usage:
from logging.config import fileConfig
fileConfig('loggin_config.ini')
logger=logging.getLogger('infoLogger')
logger.info('test1')
logger_error=logging.getLogger('errorhandler')
logger_error.error('test5')