Python Module-Log

Source: Internet
Author: User

Log is our key tool to troubleshoot problems, write log records, and when we have problems, you can quickly locate the code range to modify. Python has a good log module for our developers.

1. Log module: Logging

Example:

Import Logging

Logging.debug ("This is debug message")

Logging.info ("This is info message")

logging.warning ("This is warning message")

logging.error ("This is error message")

logging.critical ("This is critical message")


Results:

WARNING:root:This is WARNING message

ERROR:root:This is error message

CRITICAL:root:This is CRITICAL message


Description: Level up, down, up, default to info, so only higher-level log information is printed


2. Raise the log level to debug via the Logging.basicconfig function

Function Description:

Level: Sets the log levels by default to logging. WARNING

FileName: Specifies the log file name.

FileMode: Same as file function, specify open mode of log file, ' W ' or ' a '


Format : Specifies the formats and contents of the output, and format can output a lot of useful information:

% (levelname) S: Print log level name

% (filename) s: Prints the current name of the executing program

% (funcName) s: Print the current function of the log

% (Lineno) d: Print the current line number of the log

% (asctime) s: Time to print logs

% (thread) d: Print thread ID

% (process) d: Print process ID

% (message) s: Print log information


DATEFMT: Specify time format, same as Time.strftime ()

Stream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr, stream is ignored when stream and filename are specified simultaneously

Logging.getlogger ([name]): Create a Log Object


Example:

Import Logging

logging.basicconfig (level =logging. DEBUG, format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',datefmt='%y/%m/%d%h:%m:%s ', filename=' Mytest.log ', filemode=' W ')

Logger = Logging.getlogger (__name__)

Logging.debug ("This is debug message")

Logging.info ("This is info message")

logging.warning ("This is warning message")

logging.error ("This is error message")

logging.critical ("This is critical message")


Results:

A directory named Mytest.log file is generated with the content:

2017/10/24 16:30:23 a1.python.py[line:436] Debug this is debug message

2017/10/24 16:30:23 a1.python.py[line:437] Info This is INFO message

2017/10/24 16:30:23 a1.python.py[line:438] WARNING This is WARNING message

2017/10/24 16:30:23 a1.python.py[line:439] Error This is ERROR message

2017/10/24 16:30:23 a1.python.py[line:440] CRITICAL This is CRITICAL message


Description: Returns a logger instance that returns root logger if no name is specified, and if name is the same, the returned logger instance is the same and only one, that is, the name and the logger instance are one by one corresponding. This means that there is no need to pass logger instances in each module. As long as you know name, you can get the same logger instance.

Logging.getlogger (__name__) __name__ in the above example refers to the __main__


Python Module-Log

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.