When writing code while looking at the debug log, it is often troublesome to change the output ON / OFF. So, here's a trick to change the log level from the command line.
First, I will attach a code example for using the logging
module normally and outputting the log to the console. Below, I am using python3.7.
logging_test.py
import logging
from logging import DEBUG, INFO
def set_logger():
logger = logging.getLogger('logname')
stream = logging.StreamHandler()
logger.addHandler(stream)
logger.setLevel(INFO)
return logger
def main():
logger = set_logger()
logger.debug('debug')
logger.info('info')
if __name__ == "__main__":
main()
I will explain briefly.
--The log is set with set_logger
. The return value logger
is the instance used for log output in the program.
--stream
determines the output destination of the log. This time it's console output, so I'm using logging.StreamHandler ()
. Use logging.FileHandler ()
to output to a file.
--Add the output destination specified by stream
to the logger instance withlogger.addHandler (stream)
.
--Set the log level with logger.setLevel (INFO)
. This time, it is set to ʻINFO (information)level. --And output the log with
logger.debug ('debug')and
logger.info ('info')`.
Here is the result of executing the above code.
>python logging_test.py
info
>
The INFO level log is output, but the DEBUG level log is not output. To output DEBUG level logs, the log level setting in the script must be logger.setLevel (DEBUG)
.
There is a python command line parser called Click
. This makes it easy to implement the process of passing command line arguments when running a python file. Speaking of CLI creation libraries, Python Fire
is very easy and convenient, but I personally recommend Click, which allows you to explicitly specify variables that can be passed to the CLI.
Use Click to tweak the code a bit.
logging_test.py
import click
import logging
from logging import DEBUG, INFO
def set_logger(debug_mode):
logger = logging.getLogger('logname')
stream = logging.StreamHandler()
logger.addHandler(stream)
if debug_mode is True:
logger.setLevel(DEBUG)
else:
logger.setLevel(INFO)
return logger
@click.command()
@click.option('--debug_mode', '-d', is_flag=True,
help='Show debug log')
def main(debug_mode):
logger = set_logger(debug_mode)
logger.debug('debug')
logger.info('info')
if __name__ == "__main__":
main()
The big change is that the log level settings and the main
function have decorators (@ click. ~
). Let's talk about this.
--First, the decorator @ click.option ('--debug_mode','-d', is_flag = True, help ='Show debug log')
given to the main
function sets the variable debug_mode
. This is the part of the setting to be passed to the main
function as a command line argument. The arguments are, in order from the left, the explanation when the long option, short option, flag (True / False), and help option are specified. If you add --debug_mode
or -d
at run time, the debug_mode
variable will be True
.
--Next, the debug_mode
variable received as a command line argument is passed to the set_logger
function. If the -d
option is specified, the log level will be DEBUG, and if not specified, it will be INFO.
Here is a comparison of the execution results when executed without specifying -d
and when executed with it specified.
>python logging_test.py
info
>python logging_test.py -d
debug
info
>
If you specify -d
, even the DEBUG level log is output. By doing this, you can set the debug log output settings without changing the source code log level each time.
Finally, here are the sites I usually refer to regarding logging and Click.
-[Introduction to Python] Let's keep a record of processing with the logging module! -Python: Click on the command line parser was too convenient
Recommended Posts