python logging Is to add... To the code log, The module used is logging
python log Yes 5 Class :
''' DEBUG INFO WARNING ERROR CRITICAL '''
If logging.basicConfig
Method does not set level
Parameters , Then the default output level
by Warning
level ,Warning
Below grade log That is to say INFO
and DEBUG
No output .
If level
Set to logging.DEBUG
All kinds of log All output .
Set up filename
, take log
The output is written to the specified file , Do not set console Output log.
logging.getLogger('test_logger')
If the parameter is set to __name__
Then each module has its own log. In different modules , If this parameter is the same , Then belong to the same log.
import logging
logging.basicConfig(format="%(asctime)s %(levelname)-8s: [%(filename)s:%(lineno)d] %(message)s",
level=logging.DEBUG,
filename="logs.txt")
logger = logging.getLogger('test_logger')
logger.info('log msg.')
logger.warning('warning msg')
logger.debug("debug msg")
logger.critical("critical msg")
logger.error("error msg")
logs.txt
Content :
2022-06-24 18:26:02,631 INFO : [app.py:7] log msg.
2022-06-24 18:26:02,631 WARNING : [app.py:8] warning msg
2022-06-24 18:26:02,631 DEBUG : [app.py:9] debug msg
2022-06-24 18:26:02,631 CRITICAL: [app.py:10] critical msg
2022-06-24 18:26:02,631 ERROR : [app.py:11] error msg