Allow logging over UDP

This commit is contained in:
Lynn Ochs 2022-11-08 07:50:15 +01:00
parent 11c900c2e5
commit d708d1eb21
2 changed files with 12 additions and 5 deletions

View File

@ -23,6 +23,7 @@ def main():
parser.add_argument("--control_socket", default="/tmp/nfc.sock")
parser.add_argument("--valid_tokens", default="/etc/door_tokens")
parser.add_argument("--log_file", default="/tmp/nfc.log")
parser.add_argument("--log_host", default=None)
parser.add_argument("--log_level", default="INFO")
parser.add_argument("--mqtt_host", default="10.10.21.2")
@ -32,7 +33,12 @@ def main():
mqttc.connect_async("10.10.21.2", keepalive=60)
mqttc.loop_start()
init_logging(level=config.log_level, output_file=config.log_file)
log_host = None
if config.log_host != None:
log_host = config.log_host.split(':')
log_host = (log_host[0], int(log_host[1]))
init_logging(level=config.log_level, output_file=config.log_file, output_host=log_host)
logger().info("Starting control")
control = Control(config, mqttc)

View File

@ -4,6 +4,7 @@ from threading import RLock, Condition
import datetime
import logging
import logging.handlers
import sys
_log_handlers: List[logging.Handler] = []
@ -27,19 +28,19 @@ def init_logging(*,
if output_file != None:
# Create a file handler
file_handler = logging.FileHandler(output_file)
file_handler.setLevel(level)
_log_handlers.append(file_handler)
if output_host != None:
dgram_handler = logging.DatagramHandler(*output_host)
dgram_handler = logging.handlers.DatagramHandler(*output_host)
dgram_handler.setLevel(logging.DEBUG)
_log_handlers.append(dgram_handler)
if output_stream != None:
console_handler = logging.StreamHandler(output_stream)
console_handler.setLevel(logging.DEBUG)
_log_handlers.append(console_handler)
for h in _log_handlers:
h.setLevel(level)
# Default log output
logging.basicConfig(force=True, level=level, format=_log_format, handlers=_log_handlers)