forked from ungleich-public/cdist
Merge pull request #691 from darko-poljak/log-timestamping
Add timestamp to log messages.
This commit is contained in:
commit
1e34fc20be
6 changed files with 51 additions and 12 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -165,19 +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
|
||||
cdist.log.setupParallelLogging()
|
||||
log = logging.getLogger("cdist")
|
||||
|
||||
if args.parallel:
|
||||
import signal
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
28
cdist/log.py
28
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue