Logging fix and --syslog option.
Logging did not work when running as a daemon because all open file handles were closed for the fork. Since we cannot get to the file handle using the 'logging' module, we'll just have to re-init the logging after the daemon starts. Added a --syslog option so that we can choose to use syslog for capturing our logs. If present, it takes precedence over any log file specified. The timestamps are removed from the log message when using syslog (it is assumed syslog will add the time).
This commit is contained in:
@@ -13,7 +13,9 @@
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os.path
|
||||
import sys
|
||||
import ConfigParser
|
||||
|
||||
|
||||
@@ -132,6 +134,10 @@ class Options(object):
|
||||
),
|
||||
help='log file to use (ignored with --nodaemon)'
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'--syslog', dest='syslog', action='store_true',
|
||||
help='use syslog for logging output'
|
||||
)
|
||||
self.parser.add_argument(
|
||||
'--user', dest='user',
|
||||
help='user to use for daemon mode'
|
||||
@@ -160,12 +166,28 @@ def setup_logging(name, args):
|
||||
if args.nodaemon:
|
||||
logfile = None
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s',
|
||||
filename=logfile
|
||||
# Timestamped formatter
|
||||
ts_formatter = logging.Formatter(
|
||||
'%(asctime)-6s: %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
# No timestamp, used with syslog
|
||||
simple_formatter = logging.Formatter(
|
||||
'%(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
if args.syslog:
|
||||
handler = logging.handlers.SysLogHandler(facility="daemon")
|
||||
handler.setFormatter(simple_formatter)
|
||||
elif logfile:
|
||||
handler = logging.FileHandler(logfile)
|
||||
handler.setFormatter(ts_formatter)
|
||||
else:
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(ts_formatter)
|
||||
|
||||
logger = logging.getLogger(name)
|
||||
logger.addHandler(handler)
|
||||
|
||||
if args.debug:
|
||||
logger.setLevel(level=logging.DEBUG)
|
||||
|
||||
Reference in New Issue
Block a user