How to centrally manage the settings of the loggin module of python and output it in a consistent format with the date and time, the file name where the log was written, the log level (debug, info, error, ...), the output message, as shown below. Will be described.
2020-01-15 16:54:52,751 [logTest.py:9]INFO message.
Manage format and log level in the logConf.py file.
utils/logConf.py
import logging
format="%(asctime)s [%(filename)s:%(lineno)d] %(levelname)-8s %(message)s"
logging.basicConfig(level=logging.DEBUG, format=format)
A file that performs log processing
to hold. And you can log with logger.nfo or logger.error.
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from utils.logConf import logging
logger = logging.getLogger(__name__)
logger.info("This is a message.")
logger.error("This is an error.")
"""
2020-01-15 16:54:52,751 [logTest.py:9]INFO message.
2020-01-15 16:54:52,751 [logTest.py:10]ERROR Error.
"""
With the above, simple logging process is possible. If you want to output a log file additionally, you can easily add additional functions by changing the logConf.py file.
Recommended Posts