Python--logging Log Module

Source: Internet
Author: User
Tags comparison table log log

Module for easy logging and thread-safe

Single File Log

Basicconfig () module functions

Function: Create log file and write log mode "with reference"

How to use: module name. basicconfig (filename= "log file name", format= "(DateTime), (permission user), (write level), (generate log file name), (log contents)", datefmt= "datetime format", Level= "Log Level")

Format: Logging.basicconfig (filename= ' log.log ', format= '% (asctime) s-% (name) s-% (LevelName) s-% (module) s:% (message) s ', datefmt= '%y-%m-%d%h:%m:%s%p ', level=logging. WARNING)

Built-in level write function "content written by"

Critical () module function, critical level write log
Fatal () module function, fatal level write log
Error () module function, error level write log
Warning () module function, warning level write log
Warn () module function, warn level write log
info () module function, info level write log
Debug () module function, debug level write log

Level comparison Table
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10

#!/usr/bin/env python#-*-Coding:utf8-*-import logginglogging.basicconfig (filename= ' Log.log ', format=                    '% (asctime) s-% (name) s-% (LevelName) s-% (module) s:% (message) s ', # (DateTime), (rights user), (write level), (generate log file name), (log content) datefmt= '%y-%m-%d%h:%m:%s%p ', #日期时间格式 level=logging.  WARNING) #日志等级, log level requires uppercase # rank comparison Chart # CRITICAL = 50# FATAL = critical# ERROR = 40# WARNING = 30# WARN = warning# INFO = 20# DEBUG = 10# Note: Write level is greater than log level, write to log data # Write level, write log logging.critical (' sss ') #写入日志, write level requires lowercase logging.fatal ("333") #写入日志, Write level requires lowercase logging.error ("2222") #写入日志, write level requires lowercase logging.warning ("5555") #写入日志, write level requires lowercase logging.warn ("999") #写入日志, Write level requires lowercase logging.info ("598") #写入日志, write level requires lowercase logging.debug ("5646") #写入日志, write level requires lowercase #log log to write Data # 2016-09-06 15:10:38 PM-  root-critical-index:sss# 2016-09-06 15:10:38 pm-root-critical-index:333# 2016-09-06 15:10:38 pm-root-error -index:2222# 2016-09-06 15:10:38 pm-root-warning-index:5555# 2016-09-06 15:10:38 pm-root-warning-index: 999# above can be seen that the write level is greater than the log level before the log data is written 

Log () module function

Function: Custom write level, write log "have parameter"

How to use: module name. log (number of levels written, "Write content")

Formats such as: Logging.log (+, ' log ')

#!/usr/bin/env python#-*-Coding:utf8-*-import logginglogging.basicconfig (filename= ' Log.log ',                    format= '% ( Asctime) S-% (name) s-% (LevelName) s-% (module) s:  % (message) s ', # (DateTime), (permission user), (write level), (generate log file name), (log content)                    datefmt= '%y-%m-%d%h:%m:%s%p ', #日期时间格式                    level=logging.  WARNING) #日志等级, log level requires uppercase # rank comparison Chart # CRITICAL = 50# FATAL = critical# ERROR = 40# WARNING = 30# WARN = warning# INFO = 20# DEBUG = 10# Note: Write level is greater than log level, only write log data # Custom write level, write log logging.log (' log ')

Multi-file log (recommended)

Filehandler () module functions

Function: Open or create log file "with reference"

How to use: module name. filehandler ("File path name", "open Mode", encoding= ' character encoding ')

Format such as: Logging. Filehandler (' L1_1.log ', ' a ', encoding= ' utf-8 ')

Formatter () module functions

Function: Create log Write format "with reference"

How to use: module name. Formatter (fmt= "% (date and time) s-% (log rank name) s-% (write rank name) s-% (generate log program name) s:% (log content) S")

Format such as: Logging. Formatter (fmt= "% (asctime) s-% (name) s-% (LevelName) s-% (module) s:% (message) s")

Setformatter () module functions

Function: Apply log Write format to, open file "parameter"

How to use: Open a log file variable. setformatter (create log Write format variable)

Formats such as: File_1_1.setformatter (FMT)

Logger () module functions

Function: Create a log level, note that this log level can be set by reference to the level comparison table, the need to capitalize "ginseng"

How to use: module name. Logger ("Custom log rank name", level= log level parameter)

Format such as: Logging. Logger (' s1 ', level=logging. ERROR)

AddHandler () module functions

Function: Append log level to open file "ginseng"

How to use: Create a log level variable. AddHandler (open log file variable)

Format such as: Logger1.addhandler (file_1_1)

Close () module functions

Function: Close open log File "no parameter"

How to use: open a log file variable. Close ()

Format such as: File_1_1.close ()

#!/usr/bin/env python#-*-Coding:utf8-*-import Logging #导入模块 # definition file 1file_1_1 = logging. Filehandler (' L1_1.log ', ' a ', encoding= ' utf-8 ') #以a模式打开或创建日志文件fmt = logging. Formatter (fmt= "% (asctime) s-% (name) s-% (LevelName) s-% (module) s:  % (message) s") #创建日志写入格式file_1_1. Setformatter ( FMT) #将日志写入格式应用到, open the file # definition file 2file_1_2 = logging. Filehandler (' L1_2.log ', ' a ', encoding= ' utf-8 ') #以a模式打开或创建日志文件fmt = logging. Formatter (fmt= "% (message) s") #创建日志写入格式file_1_2. Setformatter (FMT) #将日志写入格式应用到, open the file # definition log logger1 = logging. Logger (' s1 ', level=logging. ERROR) #创建日志等级, note that this log level can be set by reference to the hierarchy table, which requires capital Logger1.addhandler (file_1_1) #将日志等级追加到打开的文件logger1. AddHandler (file_1_2) # Append log level to open File # Write log logger1.critical (' 1111 ') #写入等级函数写入日志, note that this write-level function is set in reference to the hierarchy table, requires lowercase, or can be used to customize the class with the log () module function, referring to log () module function file_1_1.close () #关闭打开的日志文件file_1_2. Close () #关闭打开的日志文件 # This will write a log data to two log files at the same time, note: To write log data to several log files, Just follow the above to open several log files

Python--logging Log Module

Related Article

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.