mirror of https://github.com/fail2ban/fail2ban
added new logtarget "SYSOUT" to log from fail2ban working in foreground as systemd-service (in opposite to "STDOUT" don't log time-stamps).
parent
100b531aff
commit
f31195a4fc
|
@ -30,7 +30,7 @@ loglevel = INFO
|
|||
# using logrotate -- also adjust or disable rotation in the
|
||||
# corresponding configuration file
|
||||
# (e.g. /etc/logrotate.d/fail2ban on Debian systems)
|
||||
# Values: [ STDOUT | STDERR | SYSLOG | FILE ] Default: STDERR
|
||||
# Values: [ STDOUT | STDERR | SYSLOG | SYSOUT | FILE ] Default: STDERR
|
||||
#
|
||||
logtarget = /var/log/fail2ban.log
|
||||
|
||||
|
|
|
@ -99,7 +99,7 @@ class Fail2banCmdLine():
|
|||
output(" -s <FILE> socket path")
|
||||
output(" -p <FILE> pidfile path")
|
||||
output(" --loglevel <LEVEL> logging level")
|
||||
output(" --logtarget <FILE>|STDOUT|STDERR|SYSLOG")
|
||||
output(" --logtarget <TARGET> logging target, use file-name or stdout, stderr, syslog or sysout.")
|
||||
output(" --syslogsocket auto|<FILE>")
|
||||
output(" -d dump configuration. For debugging")
|
||||
output(" --dp, --dump-pretty dump the configuration using more human readable representation")
|
||||
|
|
|
@ -210,7 +210,8 @@ class Fail2banServer(Fail2banCmdLine):
|
|||
if server: # pragma: no cover
|
||||
server.quit()
|
||||
exit(-1)
|
||||
logSys.debug('Starting server done')
|
||||
if background:
|
||||
logSys.debug('Starting server done')
|
||||
|
||||
except Exception as e:
|
||||
if self._conf["verbose"] > 1:
|
||||
|
|
|
@ -143,7 +143,7 @@ def str2LogLevel(value):
|
|||
raise ValueError("Invalid log level %r" % value)
|
||||
return ll
|
||||
|
||||
def getVerbosityFormat(verbosity, fmt=' %(message)s'):
|
||||
def getVerbosityFormat(verbosity, fmt=' %(message)s', addtime=True):
|
||||
"""Custom log format for the verbose runs
|
||||
"""
|
||||
if verbosity > 1: # pragma: no cover
|
||||
|
@ -152,7 +152,9 @@ def getVerbosityFormat(verbosity, fmt=' %(message)s'):
|
|||
if verbosity > 2:
|
||||
fmt = ' +%(relativeCreated)5d %(thread)X %(name)-25.25s %(levelname)-5.5s' + fmt
|
||||
else:
|
||||
fmt = ' %(asctime)-15s %(thread)X %(levelname)-5.5s' + fmt
|
||||
fmt = ' %(thread)X %(levelname)-5.5s' + fmt
|
||||
if addtime:
|
||||
fmt = ' %(asctime)-15s' + fmt
|
||||
return fmt
|
||||
|
||||
|
||||
|
|
|
@ -27,7 +27,6 @@ __license__ = "GPL"
|
|||
import threading
|
||||
from threading import Lock, RLock
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
import signal
|
||||
import stat
|
||||
|
@ -561,10 +560,8 @@ class Server:
|
|||
self.__logTarget = target
|
||||
return True
|
||||
# set a format which is simpler for console use
|
||||
fmt = "%(asctime)s %(name)-24s[%(process)d]: %(levelname)-7s %(message)s"
|
||||
fmt = "%(name)-24s[%(process)d]: %(levelname)-7s %(message)s"
|
||||
if systarget == "SYSLOG":
|
||||
# Syslog daemons already add date to the message.
|
||||
fmt = "%(name)s[%(process)d]: %(levelname)s %(message)s"
|
||||
facility = logging.handlers.SysLogHandler.LOG_DAEMON
|
||||
if self.__syslogSocket == "auto":
|
||||
import platform
|
||||
|
@ -581,7 +578,7 @@ class Server:
|
|||
"Syslog socket file: %s does not exists"
|
||||
" or is not a socket" % self.__syslogSocket)
|
||||
return False
|
||||
elif systarget == "STDOUT":
|
||||
elif systarget in ("STDOUT", "SYSOUT"):
|
||||
hdlr = logging.StreamHandler(sys.stdout)
|
||||
elif systarget == "STDERR":
|
||||
hdlr = logging.StreamHandler(sys.stderr)
|
||||
|
@ -615,8 +612,14 @@ class Server:
|
|||
if logger.getEffectiveLevel() <= logging.DEBUG: # pragma: no cover
|
||||
if self.__verbose is None:
|
||||
self.__verbose = logging.DEBUG - logger.getEffectiveLevel() + 1
|
||||
# If handler don't already add date to the message:
|
||||
addtime = systarget not in ("SYSLOG", "SYSOUT")
|
||||
# verbose log-format:
|
||||
if self.__verbose is not None and self.__verbose > 2: # pragma: no cover
|
||||
fmt = getVerbosityFormat(self.__verbose-1)
|
||||
fmt = getVerbosityFormat(self.__verbose-1,
|
||||
addtime=addtime)
|
||||
elif addtime:
|
||||
fmt = "%(asctime)s " + fmt
|
||||
# tell the handler to use this format
|
||||
hdlr.setFormatter(logging.Formatter(fmt))
|
||||
logger.addHandler(hdlr)
|
||||
|
|
|
@ -108,7 +108,7 @@ _fail2ban () {
|
|||
;;
|
||||
logtarget)
|
||||
if [[ "$cmd" == "set" ]];then
|
||||
COMPREPLY=( $( compgen -W "STDOUT STDERR SYSLOG" -- "$cur" ) )
|
||||
COMPREPLY=( $( compgen -W "STDOUT STDERR SYSLOG SYSOUT" -- "$cur" ) )
|
||||
_filedir # And files
|
||||
fi
|
||||
return 0
|
||||
|
|
|
@ -8,8 +8,8 @@ PartOf=iptables.service firewalld.service ip6tables.service ipset.service
|
|||
Type=simple
|
||||
ExecStartPre=/bin/mkdir -p /var/run/fail2ban
|
||||
ExecStart=@BINDIR@/fail2ban-server -xf start
|
||||
# if should be logged in systemd journal, use following line or set logtarget to stdout in fail2ban.local
|
||||
# ExecStart=@BINDIR@/fail2ban-server -xf --logtarget=stdout start
|
||||
# if should be logged in systemd journal, use following line or set logtarget to sysout in fail2ban.local
|
||||
# ExecStart=@BINDIR@/fail2ban-server -xf --logtarget=sysout start
|
||||
ExecStop=@BINDIR@/fail2ban-client stop
|
||||
ExecReload=@BINDIR@/fail2ban-client reload
|
||||
PIDFile=/var/run/fail2ban/fail2ban.pid
|
||||
|
|
|
@ -21,8 +21,9 @@ pidfile path
|
|||
.TP
|
||||
\fB\-\-loglevel\fR <LEVEL>
|
||||
logging level
|
||||
.HP
|
||||
\fB\-\-logtarget\fR <FILE>|STDOUT|STDERR|SYSLOG
|
||||
.TP
|
||||
\fB\-\-logtarget\fR <TARGET>
|
||||
logging target, use file\-name or stdout, stderr, syslog or sysout.
|
||||
.HP
|
||||
\fB\-\-syslogsocket\fR auto|<FILE>
|
||||
.TP
|
||||
|
|
|
@ -21,8 +21,9 @@ pidfile path
|
|||
.TP
|
||||
\fB\-\-loglevel\fR <LEVEL>
|
||||
logging level
|
||||
.HP
|
||||
\fB\-\-logtarget\fR <FILE>|STDOUT|STDERR|SYSLOG
|
||||
.TP
|
||||
\fB\-\-logtarget\fR <TARGET>
|
||||
logging target, use file\-name or stdout, stderr, syslog or sysout.
|
||||
.HP
|
||||
\fB\-\-syslogsocket\fR auto|<FILE>
|
||||
.TP
|
||||
|
|
Loading…
Reference in New Issue