Python Logging Module

Source: Internet
Author: User

#-*-Coding:utf-8-*-

Import logging
Import Sys

# Gets the logger instance and returns root if the argument is empty logger
Logger = Logging.getlogger ("AppName")

# Specify logger output format
Formatter = logging. Formatter ('% (asctime) s% (levelname) -8s:% (message) s ')

# file Log
File_handler = logging. Filehandler ("Test.log")
File_handler.setformatter (Formatter) # can specify output format by Setformatter

# console Log
Console_handler = logging. Streamhandler (Sys.stdout)
Console_handler.formatter = Formatter # can also be assigned directly to formatter

# The Log processor added for logger
Logger.addhandler (File_handler)
Logger.addhandler (Console_handler)

# Specify the lowest output level of the log, default to warn level
Logger.setlevel (Logging.info)

# output different levels of log
Logger.debug (' This is debug info ')
Logger.info (' This is Information ')
Logger.warn (' This is warning message ')
Logger.error (' This is error message ')
Logger.fatal (' This was fatal message, it is same as logger.critical ')
Logger.critical (' This is critical message ')

# 2016-10-08 21:59:19,493 Info:this is information
# 2016-10-08 21:59:19,493 warning:this is WARNING message
# 2016-10-08 21:59:19,493 error:this is ERROR message
# 2016-10-08 21:59:19,493 critical:this is fatal message, it's same as logger.critical
# 2016-10-08 21:59:19,493 critical:this is CRITICAL message

# Remove some log processors
Logger.removehandler (File_handler)

Python Logging Module

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.