if fail2ban running as systemd-service, for logging to the systemd-journal, the `logtarget` could be set to STDOUT

small fixes by logging in stdout (+ system targets also allowed in lowercase now)
pull/1618/head
sebres 2016-11-24 12:13:47 +01:00
parent 1cd67ecaa2
commit 45174c5eaf
5 changed files with 15 additions and 5 deletions

View File

@ -274,6 +274,7 @@ class Fail2banCmdLine():
def exit(code=0): def exit(code=0):
logSys.debug("Exit with code %s", code) logSys.debug("Exit with code %s", code)
# because of possible buffered output in python, we should flush it before exit: # because of possible buffered output in python, we should flush it before exit:
logging.shutdown()
sys.stdout.flush() sys.stdout.flush()
sys.stderr.flush() sys.stderr.flush()
# exit # exit

View File

@ -530,17 +530,19 @@ class Server:
# @param target the logging target # @param target the logging target
def setLogTarget(self, target): def setLogTarget(self, target):
# check reserved targets in uppercase, don't change target, because it can be file:
systarget = target.upper()
with self.__loggingLock: with self.__loggingLock:
# don't set new handlers if already the same # don't set new handlers if already the same
# or if "INHERITED" (foreground worker of the test cases, to prevent stop logging): # or if "INHERITED" (foreground worker of the test cases, to prevent stop logging):
if self.__logTarget == target: if self.__logTarget == target:
return True return True
if target == "INHERITED": if systarget == "INHERITED":
self.__logTarget = target self.__logTarget = target
return True return True
# set a format which is simpler for console use # set a format which is simpler for console use
fmt = "%(asctime)s %(name)-24s[%(process)d]: %(levelname)-7s %(message)s" fmt = "%(asctime)s %(name)-24s[%(process)d]: %(levelname)-7s %(message)s"
if target == "SYSLOG": if systarget == "SYSLOG":
# Syslog daemons already add date to the message. # Syslog daemons already add date to the message.
fmt = "%(name)s[%(process)d]: %(levelname)s %(message)s" fmt = "%(name)s[%(process)d]: %(levelname)s %(message)s"
facility = logging.handlers.SysLogHandler.LOG_DAEMON facility = logging.handlers.SysLogHandler.LOG_DAEMON
@ -559,9 +561,9 @@ class Server:
"Syslog socket file: %s does not exists" "Syslog socket file: %s does not exists"
" or is not a socket" % self.__syslogSocket) " or is not a socket" % self.__syslogSocket)
return False return False
elif target == "STDOUT": elif systarget == "STDOUT":
hdlr = logging.StreamHandler(sys.stdout) hdlr = logging.StreamHandler(sys.stdout)
elif target == "STDERR": elif systarget == "STDERR":
hdlr = logging.StreamHandler(sys.stderr) hdlr = logging.StreamHandler(sys.stderr)
else: else:
# Target should be a file # Target should be a file

View File

@ -8,6 +8,8 @@ PartOf=iptables.service firewalld.service
Type=simple Type=simple
ExecStartPre=/bin/mkdir -p /var/run/fail2ban ExecStartPre=/bin/mkdir -p /var/run/fail2ban
ExecStart=/usr/bin/fail2ban-server -xf start ExecStart=/usr/bin/fail2ban-server -xf start
# if should be logged in systemd journal, use following line or set logtarget to stdout in fail2ban.local
# ExecStart=/usr/bin/fail2ban-server -xf --logtarget=stdout start
ExecStop=/usr/bin/fail2ban-client stop ExecStop=/usr/bin/fail2ban-client stop
ExecReload=/usr/bin/fail2ban-client reload ExecReload=/usr/bin/fail2ban-client reload
PIDFile=/var/run/fail2ban/fail2ban.pid PIDFile=/var/run/fail2ban/fail2ban.pid

View File

@ -23,6 +23,9 @@ pidfile path
logging level logging level
.HP .HP
\fB\-\-logtarget\fR <FILE>|STDOUT|STDERR|SYSLOG \fB\-\-logtarget\fR <FILE>|STDOUT|STDERR|SYSLOG
logging target
.br
Note. If fail2ban running as systemd-service, for logging to the systemd-journal, the logtarget could be set to STDOUT
.HP .HP
\fB\-\-syslogsocket\fR auto|<FILE> \fB\-\-syslogsocket\fR auto|<FILE>
.TP .TP

View File

@ -130,7 +130,9 @@ The items that can be set are:
verbosity level of log output: CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG, TRACEDEBUG, HEAVYDEBUG or corresponding numeric value (50-5). Default: ERROR (equal 40) verbosity level of log output: CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG, TRACEDEBUG, HEAVYDEBUG or corresponding numeric value (50-5). Default: ERROR (equal 40)
.TP .TP
.B logtarget .B logtarget
log target: filename, SYSLOG, STDERR or STDOUT. Default: STDERR log target: filename, SYSLOG, STDERR or STDOUT. Default: STDOUT if not set in fail2ban.conf/fail2ban.local
.br
Note. If fail2ban running as systemd-service, for logging to the systemd-journal, the logtarget could be set to STDOUT
.br .br
Only a single log target can be specified. Only a single log target can be specified.
If you change logtarget from the default value and you are using logrotate -- also adjust or disable rotation in the If you change logtarget from the default value and you are using logrotate -- also adjust or disable rotation in the