A usage example of a logging module in Python
- 2020-04-02 14:12:53
- OfStack
This article describes the logging module usage example, to share with you for your reference. Specific methods are 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 results of the program are as follows:
[2014-09-29 10:23:58,905] [root] WARNING: a warning c:
I hope this article has helped you with your Python programming.