Created
March 16, 2016 11:50
-
-
Save elzibubble/3ef859d641da7bcc9bd6 to your computer and use it in GitHub Desktop.
Test repeated calls to logging.fileConfig
This file contains 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
[loggers] | |
keys: root,aaa,bbb | |
[handlers] | |
keys: screen,watchedfile | |
[formatters] | |
keys: context | |
[logger_root] | |
qualname: root | |
handlers: screen | |
level: NOTSET | |
[logger_aaa] | |
qualname: aaa | |
handlers: | |
level: INFO | |
[logger_bbb] | |
qualname: bbb | |
handlers: | |
level: ERROR | |
[handler_watchedfile] | |
class: handlers.WatchedFileHandler | |
args: ('main.log',) | |
formatter: context | |
[handler_screen] | |
class=StreamHandler | |
formatter=context | |
level=INFO | |
args=(sys.stdout,) | |
# datefmt must be set otherwise you end up with too many (msecs) fields | |
[formatter_context] | |
class: oslo_log.formatters.ContextFormatter | |
args: (datefmt=datefmt) | |
format: %(asctime)s.%(msecs)03d %(process)d %(levelname)s %(name)s [%(request_id)s %(user)s %(tenant)s] %(instance)s%(message)s | |
datefmt: %Y-%m-%d %H:%M:%S |
This file contains 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
[loggers] | |
keys: root,aaa,ddd | |
[handlers] | |
keys: screen,watchedfile | |
[formatters] | |
keys: context | |
[logger_root] | |
qualname: root | |
handlers: screen | |
level: NOTSET | |
[logger_aaa] | |
qualname: aaa | |
handlers: | |
level: WARN | |
[logger_ddd] | |
qualname: ddd | |
handlers: | |
level: ERROR | |
[handler_watchedfile] | |
class: handlers.WatchedFileHandler | |
args: ('main.log',) | |
formatter: context | |
[handler_screen] | |
class=StreamHandler | |
formatter=context | |
level=INFO | |
args=(sys.stdout,) | |
# datefmt must be set otherwise you end up with too many (msecs) fields | |
[formatter_context] | |
class: oslo_log.formatters.ContextFormatter | |
args: (datefmt=datefmt) | |
format: %(asctime)s.%(msecs)03d %(process)d %(levelname)s %(name)s [%(request_id)s %(user)s %(tenant)s] %(instance)s%(message)s | |
datefmt: %Y-%m-%d %H:%M:%S |
This file contains 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
import os | |
import logging.config as logcfg | |
import oslo_config.cfg as cfg | |
from oslo_log import log as log | |
os.system('clear') | |
CONF = cfg.CONF | |
log.register_options(CONF) | |
CONF() | |
CONF.log_config_append = "log.ini" | |
def colour(msg, code): | |
return "\x1b[3%dm%s\x1b[0m" % (code, msg) | |
def spam(logger): | |
logger.error(colour("error", 1)) | |
logger.warning(colour("warning", 3)) | |
logger.info(colour("info", 2)) | |
logger.debug(colour("debug", 4)) | |
# Seems default_log_levels is ignored with log_config_append set | |
foo = log.get_default_log_levels() + ['ddd=WARN'] | |
log.set_defaults(default_log_levels=foo) | |
log.setup(CONF, 'logxp') | |
root = log.getLogger("") | |
aaa = log.getLogger("aaa") | |
bbb = log.getLogger("bbb") | |
ddd = log.getLogger("ddd") | |
spam(root) | |
spam(aaa) | |
spam(bbb) | |
spam(ddd) | |
print "\n--- Reloading, disable_existing_loggers=False" | |
logcfg.fileConfig('log2.ini', disable_existing_loggers=False) | |
spam(root) | |
spam(aaa) | |
spam(bbb) | |
spam(ddd) | |
# Existing loggers can have level changed and it takes | |
# A logger unmentioned in the ini can be created and later be mentioned | |
# An existing logger (aaa) which is no longer in the INI retains its previously | |
# configured level | |
print "\n--- Reloading, disable_existing_loggers=True" | |
logcfg.fileConfig('log2.ini', disable_existing_loggers=True) | |
spam(root) | |
spam(aaa) | |
spam(bbb) | |
spam(ddd) | |
# An existing logger (aaa) which is no longer in the INI stops logging |
This file contains 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
2016-03-16 11:49:47.578 25533 ERROR root [-] error | |
2016-03-16 11:49:47.578 25533 WARNING root [-] warning | |
2016-03-16 11:49:47.579 25533 INFO root [-] info | |
2016-03-16 11:49:47.579 25533 ERROR aaa [-] error | |
2016-03-16 11:49:47.579 25533 WARNING aaa [-] warning | |
2016-03-16 11:49:47.579 25533 INFO aaa [-] info | |
2016-03-16 11:49:47.579 25533 ERROR bbb [-] error | |
2016-03-16 11:49:47.580 25533 ERROR ddd [-] error | |
2016-03-16 11:49:47.580 25533 WARNING ddd [-] warning | |
2016-03-16 11:49:47.580 25533 INFO ddd [-] info | |
--- Reloading, disable_existing_loggers=False | |
2016-03-16 11:49:47.582 25533 ERROR root [-] error | |
2016-03-16 11:49:47.582 25533 WARNING root [-] warning | |
2016-03-16 11:49:47.582 25533 INFO root [-] info | |
2016-03-16 11:49:47.583 25533 ERROR aaa [-] error | |
2016-03-16 11:49:47.583 25533 WARNING aaa [-] warning | |
2016-03-16 11:49:47.583 25533 ERROR bbb [-] error | |
2016-03-16 11:49:47.583 25533 ERROR ddd [-] error | |
--- Reloading, disable_existing_loggers=True | |
2016-03-16 11:49:47.585 25533 ERROR root [-] error | |
2016-03-16 11:49:47.585 25533 WARNING root [-] warning | |
2016-03-16 11:49:47.585 25533 INFO root [-] info | |
2016-03-16 11:49:47.586 25533 ERROR aaa [-] error | |
2016-03-16 11:49:47.586 25533 WARNING aaa [-] warning | |
2016-03-16 11:49:47.586 25533 ERROR ddd [-] error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment