diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-08 17:58:37 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-08 17:58:37 -0700 |
commit | 675341b3b1431eda2513d87d741c5b72d74e4405 (patch) | |
tree | 165b23e1a04be551f1303ea9f957c86d1e8c9d87 /cloudinit/log.py | |
parent | e389ecb7af6387de477b1a50e48044b51e65a98c (diff) | |
download | vyos-cloud-init-675341b3b1431eda2513d87d741c5b72d74e4405.tar.gz vyos-cloud-init-675341b3b1431eda2513d87d741c5b72d74e4405.zip |
Working on making this have the old setup (where strings are taken in for config) as well as file names (for those that have files).
Diffstat (limited to 'cloudinit/log.py')
-rw-r--r-- | cloudinit/log.py | 44 |
1 files changed, 17 insertions, 27 deletions
diff --git a/cloudinit/log.py b/cloudinit/log.py index 4d270045..2dda405d 100644 --- a/cloudinit/log.py +++ b/cloudinit/log.py @@ -2,8 +2,13 @@ import logging import logging.handlers +import logging.config + +import os import sys +from StringIO import StringIO + # Logging levels for easy access CRITICAL = logging.CRITICAL FATAL = logging.FATAL @@ -14,10 +19,6 @@ INFO = logging.INFO DEBUG = logging.DEBUG NOTSET = logging.NOTSET -# File log rotation settings -ROTATE_AMOUNT = 10 # Only keep the past 9 + 1 active -ROTATE_SIZE = 10 * 1024 * 1024 # 10 MB - class ConsoleFormatter(logging.Formatter): @@ -31,50 +32,39 @@ class ConsoleFormatter(logging.Formatter): record.message = record.getMessage() rdict = dict(record.__dict__) rdict['minilevelname'] = self._get_mini_level(record) - # Skipping exception info for the console... return self._fmt % (rdict) -def setupLogging(level, filename=None, filelevel=logging.DEBUG): - root = getLogger() - consolelg = logging.StreamHandler(sys.stdout) - consolelg.setFormatter(ConsoleFormatter('%(minilevelname)s%(message)s')) - consolelg.setLevel(level) - root.addHandler(consolelg) - if filename: - filelg = logging.handlers.RotatingFileHandler(filename, maxBytes=ROTATE_SIZE, backupCount=ROTATE_AMOUNT) - filelg.setFormatter(logging.Formatter('%(levelname)s: @%(name)s : %(message)s')) - filelg.setLevel(filelevel) - root.addHandler(filelg) - root.setLevel(level) - - -def logging_set_from_cfg(cfg): +def setupLogging(cfg): log_cfgs = [] - logcfg = util.get_cfg_option_str(cfg, "log_cfg", False) - if logcfg: + log_cfg = cfg.get('logcfg') + if log_cfg: # if there is a 'logcfg' entry in the config, respect # it, it is the old keyname - log_cfgs = [logcfg] + log_cfgs = [log_cfg] elif "log_cfgs" in cfg: for cfg in cfg['log_cfgs']: if isinstance(cfg, list): log_cfgs.append('\n'.join(cfg)) else: - log_cfgs.append() + log_cfgs.append(cfg) if not len(log_cfgs): sys.stderr.write("Warning, no logging configured\n") return + am_worked = 0 for logcfg in log_cfgs: try: - logging.config.fileConfig(StringIO.StringIO(logcfg)) - return + if not os.path.isfile(logcfg): + logcfg = StringIO(logcfg) + logging.config.fileConfig(logcfg) + am_worked += 1 except: pass - raise Exception("no valid logging found\n") + if not am_worked: + sys.stderr.write("Warning, no logging configured\n") def getLogger(name='cloudinit'): |