Last active
April 6, 2018 06:58
-
-
Save devlights/ead1dfd154f4f30b6f3eba8d0a4cbcea to your computer and use it in GitHub Desktop.
[python] logging.config.dictConfigのサンプル
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
{ | |
"version": 1, | |
"disable_existing_loggers": "true", | |
"formatters": { | |
"console": { | |
"format": "[%(asctime)s][%(levelname)s] %(name)s %(filename)s:%(funcName)s:%(lineno)d | %(message)s", | |
"datefmt": "%Y/%m/%d %H:%M:%S" | |
} | |
}, | |
"handlers": { | |
"console": { | |
"level": "DEBUG", | |
"class": "logging.StreamHandler", | |
"formatter": "console", | |
"stream": "ext://sys.stderr" | |
} | |
}, | |
"loggers": { | |
"": { | |
"handlers": ["console"], | |
"level": "ERROR", | |
"propagate": "false" | |
} | |
} | |
} |
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
""" | |
logging.config.dictConfig() のサンプル | |
REFERENCES:: | |
https://docs.python.jp/3/library/logging.config.html#logging-config-dictschema | |
https://docs.sentry.io/clients/python/integrations/logging/ | |
""" | |
import logging.config | |
logging.config.dictConfig({ | |
'version': 1, | |
'disable_existing_loggers': True, | |
'formatters': { | |
'console': { | |
'format': '[%(asctime)s][%(levelname)s] %(name)s ' | |
'%(filename)s:%(funcName)s:%(lineno)d | %(message)s', | |
'datefmt': '%Y/%m/%d %H:%M:%S', | |
} | |
}, | |
'handlers': { | |
'console': { | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'formatter': 'console', | |
'stream': 'ext://sys.stderr' | |
} | |
}, | |
'loggers': { | |
'': { | |
'handlers': ['console'], | |
'level': 'DEBUG', | |
'propagate': False | |
}, | |
'__main__': { | |
'level': 'WARN', | |
'propagate': True | |
} | |
} | |
}) | |
logger = logging.getLogger(__name__) | |
def go(): | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warning('warning message') | |
try: | |
1 / 0 | |
except ZeroDivisionError: | |
logger.error('error message', exc_info=True) | |
try: | |
1 / 0 | |
except ZeroDivisionError: | |
logger.fatal('fatal message', exc_info=True) | |
if __name__ == '__main__': | |
go() |
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
""" | |
logging.config.dictConfig() のサンプル2 | |
jsonファイルに設定を記載して、それを読み込むサンプルです。 | |
REFERENCES:: | |
https://docs.python.jp/3/library/logging.config.html#logging-config-dictschema | |
https://docs.sentry.io/clients/python/integrations/logging/ | |
https://stackoverflow.com/questions/38323810/does-pythons-logging-config-dictconfig-apply-the-loggers-configuration-setti | |
""" | |
import logging | |
import logging.config | |
import json | |
import pathlib | |
config_file = pathlib.Path('logging_config.json') | |
with config_file.open(mode='r', encoding='utf-8') as fd_conf: | |
logging.config.dictConfig(json.load(fd_conf)) | |
logger = logging.getLogger(__name__) | |
def go(): | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warning('warning message') | |
try: | |
1 / 0 | |
except ZeroDivisionError: | |
logger.error('error message', exc_info=True) | |
if __name__ == '__main__': | |
go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment