Python Learning notes (11) Log

Source: Internet
Author: User

Log level

Debug<info<warning<error<critical

Default print Warning,error, critical three levels of log

Import logging
Logging.debug (' Debug Message ') Logging.info (' info message ') logging.warning (' warning message ') logging.error (' Error Message ') logging.critical (' critical message ') results:

WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message

Format the print log

Import logging
Logging.basicconfig (level=logging. DEBUG, format= '% (asctime) s% (filename) s [line:% (Lineno) d]% (levelname) s% (message) s ', datefmt = '%a%d%b%Y%h:%m:%s ', fil Ename= ' Test.log ', filemode= ' W ') logging.debug (' Debug Message ') Logging.info (' info message ') logging.warning (' Warning Message ') Logging.error (' Error message ') logging.critical (' Critical message ')

Mon APR 2018 10:41:51 log. py [line:10] Debug Debug message
Mon APR 2018 10:41:51 log. py [Line:11] Info Info message
Mon APR 2018 10:41:51 log. py [Line:12] WARNING WARNING message
Mon APR 2018 10:41:51 log. py [line:13] Error error message
Mon APR 2018 10:41:51 log. py [line:14] CRITICAL CRITICAL message

Level print Lowest grade

Asctime time filename Lineno number of lines LevelName log level message information

DATEFMT formatted time%a week%d day%b month%y year%h%m minutes%s seconds

FileName File FileMode write mode file and screen print import logging logger = Logging.getlogger ()
FH = logging. Filehandler (' test.log ') ch = logging. Streamhandler ()
Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s =% (message) s ')
Fh.setformatter (Formatter) ch.setformatter (formatter)
Logger.addhandler (FH) logger.addhandler (CH)
Logger.setlevel (logging. Debug) logger.debug (' Logger debug message ') Logger.info (' Logger info message ') logger.warning (' Logger warning message ') Logger.error (' Logger error message ') logger.critical (' Logger critical message ')

Python Learning notes (11) 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.