Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #98 from jjnicola/log2file
Browse files Browse the repository at this point in the history
Add option for logging into a specified log file.
  • Loading branch information
jjnicola authored Mar 27, 2019
2 parents fa47dab + a56ef78 commit 43c50b6
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions ospd/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,10 @@ def filename(string):
help='CA cert file. Default: {0}'.format(CA_FILE))
parser.add_argument('-L', '--log-level', default='warning', type=log_level,
help='Wished level of logging. Default: WARNING')
parser.add_argument('--syslog', action='store_true',
help='Use syslog for logging.')
parser.add_argument('--background', action='store_true',
help='Run in background. Implies --syslog.')
parser.add_argument('--foreground', action='store_true',
help='Run in foreground and logs all messages to console.')
parser.add_argument('-l', '--log-file', type=filename,
help='Path to the logging file.')
parser.add_argument('--version', action='store_true',
help='Print version then exit.')
return parser
Expand Down Expand Up @@ -817,8 +817,8 @@ def get_common_args(parser, args=None):
common_args['certfile'] = certfile
common_args['cafile'] = cafile
common_args['log_level'] = log_level
common_args['syslog'] = options.syslog or options.background
common_args['background'] = options.background
common_args['foreground'] = options.foreground
common_args['log_file'] = options.log_file
common_args['version'] = options.version

return common_args
Expand Down Expand Up @@ -857,7 +857,21 @@ def main(name, klass):
if cargs['version']:
print_version(wrapper)
sys.exit()
if cargs['syslog']:

if cargs['foreground']:
console = logging.StreamHandler()
console.setFormatter(
logging.Formatter(
'%(asctime)s %(name)s: %(levelname)s: %(message)s'))
logging.getLogger().addHandler(console)
elif cargs['log_file']:
logfile = logging.handlers.WatchedFileHandler(cargs['log_file'])
logfile.setFormatter(
logging.Formatter(
'%(asctime)s %(name)s: %(levelname)s: %(message)s'))
logging.getLogger().addHandler(logfile)
go_to_background()
else:
syslog = logging.handlers.SysLogHandler('/dev/log')
syslog.setFormatter(
logging.Formatter('%(name)s: %(levelname)s: %(message)s'))
Expand All @@ -866,14 +880,6 @@ def main(name, klass):
syslog_fd = syslog.socket.fileno()
os.dup2(syslog_fd, 1)
os.dup2(syslog_fd, 2)
else:
console = logging.StreamHandler()
console.setFormatter(
logging.Formatter(
'%(asctime)s %(name)s: %(levelname)s: %(message)s'))
logging.getLogger().addHandler(console)

if cargs['background']:
go_to_background()

if not wrapper.check():
Expand Down

0 comments on commit 43c50b6

Please sign in to comment.