From 6acf6f64dcf6a5768be6f2874a719b16b5ba6a4f Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 23 Sep 2018 20:34:42 +0200 Subject: [PATCH 1/2] Add timestamp to log messages. --- cdist/argparse.py | 5 +++++ cdist/config.py | 8 +++++++- cdist/log.py | 28 ++++++++++++++++++++++++++++ docs/src/man1/cdist.rst | 10 ++++++++-- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/cdist/argparse.py b/cdist/argparse.py index 29620751..0ee9d7b1 100644 --- a/cdist/argparse.py +++ b/cdist/argparse.py @@ -251,6 +251,11 @@ def get_parsers(): 'line). If no host or host file is specified then, by ' 'default, read hosts from stdin.'), dest='hostfile', required=False) + parser['config_args'].add_argument( + '-P', '--timestamp', + help=('Timestamp log messages with the current local date and time ' + 'in the format: YYYYMMDDHHMMSS.us.'), + action='store_true', dest='timestamp') parser['config_args'].add_argument( '-p', '--parallel', nargs='?', metavar='HOST_MAX', type=functools.partial(check_lower_bounded_int, lower_bound=1, diff --git a/cdist/config.py b/cdist/config.py index 2dcb1005..bfb89419 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -175,7 +175,13 @@ class Config(object): if args.parallel or args.jobs: # If parallel execution then also log process id - cdist.log.setupParallelLogging() + if args.timestamp: + cdist.log.setupTimestampingParallelLogging() + else: + cdist.log.setupParallelLogging() + log = logging.getLogger("cdist") + elif args.timestamp: + cdist.log.setupTimestampingLogging() log = logging.getLogger("cdist") if args.parallel: diff --git a/cdist/log.py b/cdist/log.py index dba1ad2f..5d431130 100644 --- a/cdist/log.py +++ b/cdist/log.py @@ -22,6 +22,7 @@ import logging import sys +import datetime # Define additional cdist logging levels. @@ -95,15 +96,42 @@ class DefaultLog(logging.Logger): self.log(logging.TRACE, msg, *args, **kwargs) +class TimestampingLog(DefaultLog): + + def filter(self, record): + """Add timestamp to messages""" + + super().filter(record) + now = datetime.datetime.now() + timestamp = now.strftime("%Y%m%d%H%M%S.%f") + record.msg = "[" + timestamp + "] " + str(record.msg) + + return True + + class ParallelLog(DefaultLog): FORMAT = '%(levelname)s: [%(process)d]: %(message)s' +class TimestampingParallelLog(TimestampingLog, ParallelLog): + pass + + def setupDefaultLogging(): del logging.getLogger().handlers[:] logging.setLoggerClass(DefaultLog) +def setupTimestampingLogging(): + del logging.getLogger().handlers[:] + logging.setLoggerClass(TimestampingLog) + + +def setupTimestampingParallelLogging(): + del logging.getLogger().handlers[:] + logging.setLoggerClass(TimestampingParallelLog) + + def setupParallelLogging(): del logging.getLogger().handlers[:] logging.setLoggerClass(ParallelLog) diff --git a/docs/src/man1/cdist.rst b/docs/src/man1/cdist.rst index 79683883..0a628660 100644 --- a/docs/src/man1/cdist.rst +++ b/docs/src/man1/cdist.rst @@ -20,7 +20,8 @@ SYNOPSIS [-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]] [-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY] [--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-4] [-6] - [-A] [-a] [-f HOSTFILE] [-p [HOST_MAX]] [-S] [-s] [-t] + [-A] [-a] [-f HOSTFILE] [-P] [-p [HOST_MAX]] [-S] [-s] + [-t] [host [host ...]] cdist install [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-g CONFIG_FILE] @@ -28,7 +29,8 @@ SYNOPSIS [-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]] [-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY] [--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-4] [-6] - [-A] [-a] [-f HOSTFILE] [-p [HOST_MAX]] [-S] [-s] [-t] + [-A] [-a] [-f HOSTFILE] [-P] [-p [HOST_MAX]] [-S] [-s] + [-t] [host [host ...]] cdist inventory [-h] {add-host,add-tag,del-host,del-tag,list} ... @@ -171,6 +173,10 @@ Install command is currently in beta. **-o OUT_PATH, --out-dir OUT_PATH** Directory to save cdist output in. +**-P, --timestamp** + Timestamp log messages with the current local date and time + in the format: YYYYMMDDHHMMSS.us. + **-p [HOST_MAX], --parallel [HOST_MAX]** Operate on multiple hosts in parallel for specified maximum hosts at a time. Without argument CPU count is From 0c57b3083ec4f43ff6fc56345a3007cfbd4dc3f6 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 23 Sep 2018 21:38:03 +0200 Subject: [PATCH 2/2] Fix getting loggers. --- cdist/config.py | 21 ++++++++------------- cdist/core/cdist_type.py | 2 +- scripts/cdist | 3 +-- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index bfb89419..e8fd5384 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -165,25 +165,20 @@ class Config(object): def commandline(cls, args): """Configure remote system""" - # FIXME: Refactor relict - remove later - log = logging.getLogger("cdist") + if (args.parallel and args.parallel != 1) or args.jobs: + if args.timestamp: + cdist.log.setupTimestampingParallelLogging() + else: + cdist.log.setupParallelLogging() + elif args.timestamp: + cdist.log.setupTimestampingLogging() + log = logging.getLogger("config") # No new child process if only one host at a time. if args.parallel == 1: log.debug("Only 1 parallel process, doing it sequentially") args.parallel = 0 - if args.parallel or args.jobs: - # If parallel execution then also log process id - if args.timestamp: - cdist.log.setupTimestampingParallelLogging() - else: - cdist.log.setupParallelLogging() - log = logging.getLogger("cdist") - elif args.timestamp: - cdist.log.setupTimestampingLogging() - log = logging.getLogger("cdist") - if args.parallel: import signal diff --git a/cdist/core/cdist_type.py b/cdist/core/cdist_type.py index 40194f94..99e40e70 100644 --- a/cdist/core/cdist_type.py +++ b/cdist/core/cdist_type.py @@ -47,7 +47,7 @@ class CdistType(object): """ - log = logging.getLogger("cdist") + log = logging.getLogger("cdist-type") def __init__(self, base_path, name): self.base_path = base_path diff --git a/scripts/cdist b/scripts/cdist index 088e4dc2..3110e657 100755 --- a/scripts/cdist +++ b/scripts/cdist @@ -69,8 +69,6 @@ if __name__ == "__main__": import re import os - log = logging.getLogger("cdist") - if re.match("__", os.path.basename(sys.argv[0])): import cdist.emulator emulator = cdist.emulator.Emulator(sys.argv) @@ -82,6 +80,7 @@ if __name__ == "__main__": exit_code = 2 except cdist.Error as e: + log = logging.getLogger("cdist") log.error(e) exit_code = 1