Go to "Python" to output logs to console and file logging

Source: Internet
Author: User
Tags print format

#-*-Coding:utf-8-*-
Import logging

# Configure log information
Logging.basicconfig (level=logging. DEBUG,
format= '% (asctime) s% (name) -12s% (levelname) -8s% (message) s ',
datefmt= '%m-%d%h:%m ',
Filename= ' Myapp.log ',
Filemode= ' W ')
# define a handler print info and above level log to Sys.stderr
console = logging. Streamhandler ()
Console.setlevel (Logging.info)
# Set Log Print format
Formatter = logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s ')
Console.setformatter (Formatter)
# Add the well-defined console log handler to the root logger
Logging.getlogger ("). AddHandler (console)

Logging.info (' Jackdaws love my big sphinx of quartz. ')

Logger1 = Logging.getlogger (' myapp.area1 ')
Logger2 = Logging.getlogger (' myapp.area2 ')

Logger1.debug (' Quick zephyrs blow, vexing daft Jim. ')
Logger1.info (' How quickly daft jumping zebras vex. ')
Logger2.warning (' Jail Zesty vixen who grabbed pay from quack. ')
Logger2.error (' The five boxing wizards jump quickly. ')

Logs are printed in different formats and levels to the console, written to the log file Myapp.log

Original reproduced in: http://blog.csdn.net/ghostfromheaven/article/details/8249298

Go to "Python" to output logs to console and file logging

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.