Python's way of logging log to console and file output at the same time

Source: Internet
Author: User
Tags print format

The example in this article describes how Python logging the console and file output logs at the same time. Share to everyone for your reference. Specifically as follows:

Python provides a very convenient log module that enables simultaneous output logs to the console and files.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #-*-coding:utf-8-*-Import Logging # configuration log information Logging.basicconfig (Level=lo Gging. DEBUG, format= '% (asctime) s% (name) -12s% (levelname) -8s% (message) s ', datefmt= '%m-%d%h:%m ', filename= ' Myapp.log ', Filemode= ' W ') # defines 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 defined console log handler to root logger Logging.getlogger ("). AddHandler (console) logging.info (' Jackdaws Love Me 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 The who grabbed pay to quack. ') Logge r2.eRror (' The five boxing wizards jump quickly. ')

I hope this article will help you with your Python programming.

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.