The Python logging module logging is used through configuration files,

Source: Internet
Author: User

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')
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.