diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-18 17:26:16 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-18 17:26:16 -0700 |
commit | 5e0bf173c870f0f732d68d9db338f40d1d0a6a5a (patch) | |
tree | 7bbcc00fa7ee4c7c3ab66c714fba0ca10d4f65fd | |
parent | 24b818fd7c26ccc4c31191c1ae274a5fff506038 (diff) | |
download | vyos-cloud-init-5e0bf173c870f0f732d68d9db338f40d1d0a6a5a.tar.gz vyos-cloud-init-5e0bf173c870f0f732d68d9db338f40d1d0a6a5a.zip |
1. Simplify basic logging (which will not always be turned on in the new cloud init main entrypoint
2. Have the ability to reset the logging handlers
a. This seems needed when we initially have basic logging turned on, then later we come in and change the logging.
It seems required for some odd reason to go in and reset the handlers for the root/cloudinit loggers (needs some more investigation).
-rw-r--r-- | cloudinit/log.py | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/cloudinit/log.py b/cloudinit/log.py index 5fcb77ef..478946f8 100644 --- a/cloudinit/log.py +++ b/cloudinit/log.py @@ -46,24 +46,14 @@ DEF_CON_FORMAT = '%(asctime)s - %(filename)s[%(levelname)s]: %(message)s' def setupBasicLogging(): root = logging.getLogger() - # Warnings go to the console console = logging.StreamHandler(sys.stderr) console.setFormatter(logging.Formatter(DEF_CON_FORMAT)) - console.setLevel(WARNING) + console.setLevel(DEBUG) root.addHandler(console) - # Everything else goes to this file (if we can) - try: - cfile = logging.FileHandler('/var/log/cloud-init.log') - cfile.setFormatter(logging.Formatter(DEF_CON_FORMAT)) - cfile.setLevel(DEBUG) - root.addHandler(cfile) - except (IOError, OSError): - # Likely that u can't write to that file... - # Make console now have DEBUG?? - console.setLevel(DEBUG) root.setLevel(DEBUG) + def setupLogging(cfg=None): # See if the config provides any logging conf... if not cfg: @@ -86,7 +76,7 @@ def setupLogging(cfg=None): # See if any of them actually load... am_tried = 0 am_worked = 0 - for log_cfg in log_cfgs: + for i, log_cfg in enumerate(log_cfgs): try: am_tried += 1 # Assume its just a string if not a filename @@ -97,13 +87,14 @@ def setupLogging(cfg=None): # Attempt to load its config logging.config.fileConfig(log_cfg) am_worked += 1 - except Exception: - pass + except Exception as e: + sys.stderr.write(("WARN: Setup of logging config %s" + " failed due to: %s\n") % (i + 1, e)) # If it didn't work, at least setup a basic logger (if desired) basic_enabled = cfg.get('log_basic', True) if not am_worked: - sys.stderr.write(("Warning, no logging configured!" + sys.stderr.write(("WARN: no logging configured!" " (tried %s configs)\n") % (am_tried)) if basic_enabled: sys.stderr.write("Setting up basic logging...\n") @@ -123,5 +114,22 @@ except ImportError: def emit(self, record): pass -logger = logging.getLogger() -logger.addHandler(NullHandler()) + +def _resetLogger(log): + if not log: + return + handlers = list(log.handlers) + for h in handlers: + h.flush() + h.close() + log.removeHandler(h) + log.setLevel(NOTSET) + log.addHandler(NullHandler()) + + +def resetLogging(): + _resetLogger(logging.getLogger()) + _resetLogger(getLogger()) + + +resetLogging() |