diff --git a/config/fail2ban.conf b/config/fail2ban.conf index 7c001155..52e47187 100644 --- a/config/fail2ban.conf +++ b/config/fail2ban.conf @@ -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 diff --git a/fail2ban/client/fail2bancmdline.py b/fail2ban/client/fail2bancmdline.py index 401aa9b6..269fa174 100644 --- a/fail2ban/client/fail2bancmdline.py +++ b/fail2ban/client/fail2bancmdline.py @@ -99,7 +99,7 @@ class Fail2banCmdLine(): output(" -s socket path") output(" -p pidfile path") output(" --loglevel logging level") - output(" --logtarget |STDOUT|STDERR|SYSLOG") + output(" --logtarget logging target, use file-name or stdout, stderr, syslog or sysout.") output(" --syslogsocket auto|") output(" -d dump configuration. For debugging") output(" --dp, --dump-pretty dump the configuration using more human readable representation") diff --git a/fail2ban/client/fail2banserver.py b/fail2ban/client/fail2banserver.py index 006a02cf..2dcaddf7 100644 --- a/fail2ban/client/fail2banserver.py +++ b/fail2ban/client/fail2banserver.py @@ -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: diff --git a/fail2ban/helpers.py b/fail2ban/helpers.py index 7eaa59f8..5b027b32 100644 --- a/fail2ban/helpers.py +++ b/fail2ban/helpers.py @@ -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 diff --git a/fail2ban/server/server.py b/fail2ban/server/server.py index e3b22c44..ce28e3f9 100644 --- a/fail2ban/server/server.py +++ b/fail2ban/server/server.py @@ -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) diff --git a/files/bash-completion b/files/bash-completion index 36e0cbba..b8887001 100644 --- a/files/bash-completion +++ b/files/bash-completion @@ -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 diff --git a/files/fail2ban.service.in b/files/fail2ban.service.in index 37ae4f07..24dcb51e 100644 --- a/files/fail2ban.service.in +++ b/files/fail2ban.service.in @@ -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 diff --git a/man/fail2ban-client.1 b/man/fail2ban-client.1 index 26e5ee59..6e44b387 100644 --- a/man/fail2ban-client.1 +++ b/man/fail2ban-client.1 @@ -21,8 +21,9 @@ pidfile path .TP \fB\-\-loglevel\fR logging level -.HP -\fB\-\-logtarget\fR |STDOUT|STDERR|SYSLOG +.TP +\fB\-\-logtarget\fR +logging target, use file\-name or stdout, stderr, syslog or sysout. .HP \fB\-\-syslogsocket\fR auto| .TP diff --git a/man/fail2ban-server.1 b/man/fail2ban-server.1 index cb71e288..aca3eeb0 100644 --- a/man/fail2ban-server.1 +++ b/man/fail2ban-server.1 @@ -21,8 +21,9 @@ pidfile path .TP \fB\-\-loglevel\fR logging level -.HP -\fB\-\-logtarget\fR |STDOUT|STDERR|SYSLOG +.TP +\fB\-\-logtarget\fR +logging target, use file\-name or stdout, stderr, syslog or sysout. .HP \fB\-\-syslogsocket\fR auto| .TP