Python/Logging: Difference between revisions
< Python
Jump to navigation
Jump to search
No edit summary |
|||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
== Load config in YAML format. == | |||
<source lang="python"> | <source lang="python"> | ||
import yaml | import yaml | ||
| Line 8: | Line 10: | ||
for name in cfg_dict['handlers']: | for name in cfg_dict['handlers']: | ||
handler = cfg_dict['handlers'][name] | handler = cfg_dict['handlers'][name] | ||
# Replace ~ (home) as absolute path. | |||
if 'filename' in handler and handler['filename'].startswith('~'): | if 'filename' in handler and handler['filename'].startswith('~'): | ||
handler['filename'] = os.path.expanduser(handler['filename']) | handler['filename'] = os.path.expanduser(handler['filename']) | ||
logging.config.dictConfig(cfg_dict) | logging.config.dictConfig(cfg_dict) | ||
</source> | |||
== Inject a handler == | |||
<source lang="python"> | |||
exit(1) | |||
</source> | </source> | ||
Latest revision as of 06:22, 8 May 2020
Load config in YAML format.
import yaml
import logging
cfg_yaml = '{}/conf/logging.yaml'.format(get_package_dir())
with open(cfg_yaml, 'r') as cfg_file:
cfg_dict = yaml.load(open(cfg_yaml, 'r'), Loader=yaml.SafeLoader)
for name in cfg_dict['handlers']:
handler = cfg_dict['handlers'][name]
# Replace ~ (home) as absolute path.
if 'filename' in handler and handler['filename'].startswith('~'):
handler['filename'] = os.path.expanduser(handler['filename'])
logging.config.dictConfig(cfg_dict)
Inject a handler
exit(1)