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.