Python logging for both console and file output logs

Source: Internet
Author: User
This example describes how Python logging to the console and file output logs at the same time. Share to everyone for your reference. Specific as follows:

Python provides a very convenient log module that enables the ability to output logs to both the console and the file.

#-*-coding:utf-8-*-import logging# configuration 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 ') # defines a handler print info and above levels of logs to Sys.stderrconsole = logging. Streamhandler () Console.setlevel (logging.info) # Set the log print format formatter = logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s ') Console.setformatter (Formatter) # Add the well-defined console log handler to root Loggerlogging.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. ')

Hopefully this article will help you with Python programming.

  • 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.