From 0708e200c8a0c6e9cd26e66644450e6371382548 Mon Sep 17 00:00:00 2001 From: Fredrik Eriksson Date: Mon, 21 Dec 2020 15:48:22 +0100 Subject: [PATCH] add support for syslog on freebsd --- rotator/__main__.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/rotator/__main__.py b/rotator/__main__.py index c754585..1e5c2fa 100644 --- a/rotator/__main__.py +++ b/rotator/__main__.py @@ -1,7 +1,9 @@ import argparse import logging import logging.handlers +import os import re +import stat import sys import rotator @@ -83,11 +85,23 @@ def main(): handler.setLevel(logging.WARNING) log.addHandler(handler) - handler = logging.handlers.SysLogHandler(address='/dev/log') - formatter = logging.Formatter(fmt='rotator[%(process)s] %(message)s') - handler.setFormatter(formatter) - handler.setLevel(logging.INFO) - log.addHandler(handler) + handler = None + for logsocket in ('/var/run/log', '/dev/log'): + try: + mode = os.stat(logsocket).st_mode + except FileNotFoundError: + continue + + if stat.S_ISSOCK(mode): + handler = logging.handlers.SysLogHandler(address=logsocket) + formatter = logging.Formatter(fmt='rotator[%(process)s] %(message)s') + handler.setFormatter(formatter) + handler.setLevel(logging.INFO) + log.addHandler(handler) + break + + if not handler: + log.warning('No syslog socket found, will not log to syslog') args = parse_args()