This article mainly introduces the usage examples of the logging module in Python, and introduces the usage of the logging module in the form of instances, which has some practical value, for more information about how to use the logging module, see the examples in this article. The specific method is as follows:
Import logging import OS log = logging. getLogger () formatter = logging. formatter ('[% (asctime) s] [% (name) s] % (levelname) s: % (message) s') stream_handler = logging. streamHandler () file_handler = logging. fileHandler (OS. path. join ("c: \", "analysis. log ") file_handler.setFormatter (formatter) stream_handler.setFormatter (formatter) log. addHandler (file_handler) log. addHandler (stream_handler) log. setLevel (logging. DEBUG) log. warn ("a warning % s" % "c :\\")
The program running result is as follows:
[10:23:58, 905] [root] WARNING: a warning c :\
I hope this article will help you with Python programming.