This article will show you how to get started with the Python logging module for app development with minimal knowledge. Since we have compiled only the minimum knowledge that can be used immediately in development, we hope that you can use it as a starting point while supplementing the missing information.
It's like a print statement (but don't use it this way as logging doesn't make sense)
import logging
logging.warning( 'Watch out!') # Print a message to the console
logging.info( 'I said so') # The default log level is a warning, so nothing should be output to the console
logger
logger object
Set the logger to use
Set log level (DEBUG, WARNING, etc.)
Decide whether to send the log to the console or a file (use handlers such as StreamHandler, TimedRotatingFileHandler)
logger = logging.getLogger( 'app')
Build a logger object
The logger is named app
root
├──your_app.py
├──logging.conf
└──logging_dev.conf
root
├──app
│└──your_app.py
└──conf
├──logging.conf
└──logging_dev.conf
your_app.py
import logging
# read the conf file
logging.config.fileConfig('logging_dev.conf', disable_existing_loggers=False)
# create logger
logger = logging.getLogger('app')
def main():
try:
1/0 # ZeroDivisionError
# your code goes here
except Exception as e:
logger.exception(e.args)
if __name__ == '__main__':
main()
logging-prod.conf
[loggers]
keys=root,app
[formatters]
keys=default
[handlers]
keys=frotate,default
[formatter_default]
format=%(asctime)s %(levelname)s %(message)s
datefmt=%Y/%m/%d %H:%M:%S
[handler_frotate]
class=handlers.TimedRotatingFileHandler
formatter=default
args=('logs/logger.log', 'W0', 1, 100)
[handler_default]
class=StreamHandler
formatter=default
args=(sys.stdout,)
[logger_app]
level=INFO
handlers=frotate
qualname=app
[logger_root]
level=INFO
handlers=default
logging-dev.conf
[loggers]
keys=root,app
[formatters]
keys=default
[handlers]
keys=frotate,default
[formatter_default]
format=%(asctime)s %(levelname)s %(message)s
datefmt=%Y/%m/%d %H:%M:%S
[handler_frotate]
class=handlers.TimedRotatingFileHandler
formatter=default
args=('logs/logger_dev.log', 'W0', 1, 100)
[handler_default]
class=StreamHandler
formatter=default
args=(sys.stdout,)
[logger_app]
level=NOTSET
handlers=frotate
qualname=app
[logger_root]
level=NOTSET
handlers=default
I have briefly introduced the minimum usage of the logging module. If you need to know more details, please take a look at the documents introduced in [Reference](see #).
Recommended Posts