diff options
Diffstat (limited to 'src/lib/Bcfg2/Logger.py')
-rw-r--r-- | src/lib/Bcfg2/Logger.py | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/src/lib/Bcfg2/Logger.py b/src/lib/Bcfg2/Logger.py index 81b45550f..26c1d52f6 100644 --- a/src/lib/Bcfg2/Logger.py +++ b/src/lib/Bcfg2/Logger.py @@ -57,9 +57,11 @@ class TermiosFormatter(logging.Formatter): lines = int(math.ceil(float(len(record.msg)) / columns)) for lineNumber in range(lines): indices = [idx for idx in [(colNum * lines) + lineNumber - for colNum in range(columns)] if idx < len(record.msg)] - format = (len(indices) * (" %%-%ds " % columnWidth)) - returns.append(format % tuple([record.msg[idx] for idx in indices])) + for colNum in range(columns)] + if idx < len(record.msg)] + retformat = (len(indices) * (" %%-%ds " % columnWidth)) + returns.append(retformat % tuple([record.msg[idx] + for idx in indices])) else: returns.append(str(record.msg)) if record.exc_info: @@ -86,6 +88,8 @@ class FragmentingSysLogHandler(logging.handlers.SysLogHandler): error = record.exc_info record.exc_info = None msgdata = record.msg + if len(msgdata) == 0: + return while msgdata: newrec = copy.copy(record) newrec.msg = msgdata[:250] @@ -122,20 +126,15 @@ class FragmentingSysLogHandler(logging.handlers.SysLogHandler): """ pass - -def add_console_handler(level): +def add_console_handler(level=logging.DEBUG): """Add a logging handler that logs at a level to sys.stdout.""" console = logging.StreamHandler(sys.stdout) - if level is True: - console.setLevel(logging.DEBUG) - else: - console.setLevel(level) + console.setLevel(level) # tell the handler to use this format console.setFormatter(TermiosFormatter()) logging.root.addHandler(console) - -def add_syslog_handler(procname, syslog_facility): +def add_syslog_handler(procname, syslog_facility, level=logging.DEBUG): """Add a logging handler that logs as procname to syslog_facility.""" try: try: @@ -146,7 +145,7 @@ def add_syslog_handler(procname, syslog_facility): syslog = FragmentingSysLogHandler(procname, ('localhost', 514), syslog_facility) - syslog.setLevel(logging.DEBUG) + syslog.setLevel(level) syslog.setFormatter(logging.Formatter('%(name)s[%(process)d]: %(message)s')) logging.root.addHandler(syslog) except socket.error: @@ -154,15 +153,13 @@ def add_syslog_handler(procname, syslog_facility): except: print("Failed to activate syslogging") - -def add_file_handler(to_file): +def add_file_handler(to_file, level=logging.DEBUG): """Add a logging handler that logs to to_file.""" filelog = logging.FileHandler(to_file) - filelog.setLevel(logging.DEBUG) + filelog.setLevel(level) filelog.setFormatter(logging.Formatter('%(asctime)s %(name)s[%(process)d]: %(message)s')) logging.root.addHandler(filelog) - def setup_logging(procname, to_console=True, to_syslog=True, syslog_facility='daemon', level=0, to_file=None): """Setup logging for Bcfg2 software.""" @@ -170,11 +167,16 @@ def setup_logging(procname, to_console=True, to_syslog=True, return if to_console: - add_console_handler(to_console) + if to_console == True: + clvl = min(logging.WARNING, level) + else: + clvl = min(to_console, level) + add_console_handler(clvl) if to_syslog: - add_syslog_handler(procname, syslog_facility) + slvl = min(level, logging.INFO) + add_syslog_handler(procname, syslog_facility, level=slvl) if to_file is not None: - add_file_handler(to_file) + add_file_handler(to_file, level=level) - logging.root.setLevel(level) + logging.root.setLevel(logging.DEBUG) logging.already_setup = True |