Python outputs log logging to the console and files at the same time,
This example describes how to export logs to the console and files simultaneously by using Python. Share it with you for your reference. The details are as follows:
Python provides a very convenient Log Module for outputting logs to the console and files at the same time.
#-*-Coding: UTF-8-*-import logging # configure log information logging. basicConfig (level = logging. DEBUG, format = '% (asctime) s % (name)-12 s % (levelname)-8 s % (message) s ', datefmt = '% m-% d % H: % m', filename = 'myapp. log', filemode = 'W') # define a Handler to print logs of INFO and above to sys. stderrconsole = logging. streamHandler () console. setLevel (logging. INFO) # Set the log print format formatter = logging. formatter ('% (name)-12 s: % (levelname)-8 s % (message) s') console. setFormatter (formatter) # Add the 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. ')
I hope this article will help you with Python programming.