Created
December 28, 2021 16:06
-
-
Save s-hiiragi/39e53474361be06966a5abf5c77d112a to your computer and use it in GitHub Desktop.
(Python) コマンドライン引数からログ出力レベルを変更するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: | |
# python main.py --loglevel <level> | |
# | |
# level: | |
# CRITICAL | |
# ERROR | |
# WARNING | |
# INFO | |
# DEBUG | |
import logging | |
import argparse | |
levels = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'] | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--loglevel', choices=levels) | |
args = parser.parse_args() | |
return args | |
def main(): | |
args = parse_args() | |
logging.basicConfig(level=args.loglevel) # 1度のみ有効 | |
logger = logging.getLogger(__name__) | |
logger.critical('CRITICAL') | |
logger.error('ERROR') | |
logger.warning('WARNING') | |
logger.info('INFO') | |
logger.debug('DEBUG') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment