Add timestamp to log messages.

This commit is contained in:
Darko Poljak 2018-09-23 20:34:42 +02:00
parent 02982ca8be
commit 6acf6f64dc
4 changed files with 48 additions and 3 deletions

View file

@ -251,6 +251,11 @@ def get_parsers():
'line). If no host or host file is specified then, by ' 'line). If no host or host file is specified then, by '
'default, read hosts from stdin.'), 'default, read hosts from stdin.'),
dest='hostfile', required=False) 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( parser['config_args'].add_argument(
'-p', '--parallel', nargs='?', metavar='HOST_MAX', '-p', '--parallel', nargs='?', metavar='HOST_MAX',
type=functools.partial(check_lower_bounded_int, lower_bound=1, type=functools.partial(check_lower_bounded_int, lower_bound=1,

View file

@ -175,8 +175,14 @@ class Config(object):
if args.parallel or args.jobs: if args.parallel or args.jobs:
# If parallel execution then also log process id # If parallel execution then also log process id
if args.timestamp:
cdist.log.setupTimestampingParallelLogging()
else:
cdist.log.setupParallelLogging() cdist.log.setupParallelLogging()
log = logging.getLogger("cdist") log = logging.getLogger("cdist")
elif args.timestamp:
cdist.log.setupTimestampingLogging()
log = logging.getLogger("cdist")
if args.parallel: if args.parallel:
import signal import signal

View file

@ -22,6 +22,7 @@
import logging import logging
import sys import sys
import datetime
# Define additional cdist logging levels. # Define additional cdist logging levels.
@ -95,15 +96,42 @@ class DefaultLog(logging.Logger):
self.log(logging.TRACE, msg, *args, **kwargs) 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): class ParallelLog(DefaultLog):
FORMAT = '%(levelname)s: [%(process)d]: %(message)s' FORMAT = '%(levelname)s: [%(process)d]: %(message)s'
class TimestampingParallelLog(TimestampingLog, ParallelLog):
pass
def setupDefaultLogging(): def setupDefaultLogging():
del logging.getLogger().handlers[:] del logging.getLogger().handlers[:]
logging.setLoggerClass(DefaultLog) logging.setLoggerClass(DefaultLog)
def setupTimestampingLogging():
del logging.getLogger().handlers[:]
logging.setLoggerClass(TimestampingLog)
def setupTimestampingParallelLogging():
del logging.getLogger().handlers[:]
logging.setLoggerClass(TimestampingParallelLog)
def setupParallelLogging(): def setupParallelLogging():
del logging.getLogger().handlers[:] del logging.getLogger().handlers[:]
logging.setLoggerClass(ParallelLog) logging.setLoggerClass(ParallelLog)

View file

@ -20,7 +20,8 @@ SYNOPSIS
[-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]] [-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]]
[-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY] [-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY]
[--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-4] [-6] [--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 ...]] [host [host ...]]
cdist install [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-g CONFIG_FILE] 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}]] [-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]]
[-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY] [-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY]
[--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-4] [-6] [--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 ...]] [host [host ...]]
cdist inventory [-h] {add-host,add-tag,del-host,del-tag,list} ... 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** **-o OUT_PATH, --out-dir OUT_PATH**
Directory to save cdist output in. 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]** **-p [HOST_MAX], --parallel [HOST_MAX]**
Operate on multiple hosts in parallel for specified Operate on multiple hosts in parallel for specified
maximum hosts at a time. Without argument CPU count is maximum hosts at a time. Without argument CPU count is