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.