Log to Syslog by default
This commit is contained in:
@@ -20,6 +20,7 @@ import signal
|
|||||||
import socket
|
import socket
|
||||||
import stat
|
import stat
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import syslog
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -33,6 +34,27 @@ from hddfancontrol import colored_logging
|
|||||||
exit_evt = threading.Event()
|
exit_evt = threading.Event()
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingSysLogHandler(logging.Handler):
|
||||||
|
|
||||||
|
""" Class similar in goal to logging.handlers.SysLogHandler, but uses the syslog call instead of socket. """
|
||||||
|
|
||||||
|
def __init__(self, facility, options=syslog.LOG_PID):
|
||||||
|
syslog.openlog(logoption=options, facility=facility)
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def emit(self, record):
|
||||||
|
""" See logging.Handler.emit. """
|
||||||
|
msg = self.format(record)
|
||||||
|
h = logging.handlers.SysLogHandler
|
||||||
|
level = h.priority_names[h.priority_map[record.levelname]]
|
||||||
|
syslog.syslog(level, msg)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
""" See logging.Handler.close. """
|
||||||
|
syslog.closelog()
|
||||||
|
super().close()
|
||||||
|
|
||||||
|
|
||||||
class Drive:
|
class Drive:
|
||||||
|
|
||||||
""" Drive represented by a device file like /dev/sdX. """
|
""" Drive represented by a device file like /dev/sdX. """
|
||||||
@@ -649,7 +671,7 @@ def cl_main():
|
|||||||
"--log-file",
|
"--log-file",
|
||||||
default=None,
|
default=None,
|
||||||
dest="log_filepath",
|
dest="log_filepath",
|
||||||
help="Filepath for log output when using daemon mode")
|
help="Filepath for log output when using daemon mode, if omitted, logging goes to syslog.")
|
||||||
arg_parser.add_argument("--pid-file",
|
arg_parser.add_argument("--pid-file",
|
||||||
default=None,
|
default=None,
|
||||||
dest="pid_filepath",
|
dest="pid_filepath",
|
||||||
@@ -684,13 +706,14 @@ def cl_main():
|
|||||||
logging.getLogger().setLevel(logging_level[args.verbosity])
|
logging.getLogger().setLevel(logging_level[args.verbosity])
|
||||||
logging_fmt = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
|
logging_fmt = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
|
||||||
if args.daemonize:
|
if args.daemonize:
|
||||||
logging_formatter = logging.Formatter(fmt=logging_fmt)
|
|
||||||
if args.log_filepath is not None:
|
if args.log_filepath is not None:
|
||||||
# log to file
|
# log to file
|
||||||
logging_handler = logging.handlers.WatchedFileHandler(args.log_filepath)
|
logging_handler = logging.handlers.WatchedFileHandler(args.log_filepath)
|
||||||
else:
|
else:
|
||||||
# no logging output
|
# log to syslog
|
||||||
logging_handler = logging.handlers.NullHandler()
|
logging_fmt = "%(levelname)s [%(name)s] %(message)s"
|
||||||
|
logging_handler = LoggingSysLogHandler(syslog.LOG_DAEMON)
|
||||||
|
logging_formatter = logging.Formatter(fmt=logging_fmt)
|
||||||
else:
|
else:
|
||||||
# log to stderr
|
# log to stderr
|
||||||
logging_formatter = colored_logging.ColoredFormatter(fmt=logging_fmt)
|
logging_formatter = colored_logging.ColoredFormatter(fmt=logging_fmt)
|
||||||
|
|||||||
Reference in New Issue
Block a user