standardize and normalize verbosity parameters for fail2ban-regex / fail2ban-testcases (-v ... -vvvv, or --verbosity=0..4)

pull/1562/head
sebres 2016-09-30 10:01:21 +02:00
parent 2cfaf845ca
commit 77ec9df678
2 changed files with 16 additions and 15 deletions

View File

@ -51,7 +51,7 @@ from .filterreader import FilterReader
from ..server.filter import Filter, FileContainer from ..server.filter import Filter, FileContainer
from ..server.failregex import RegexException from ..server.failregex import RegexException
from ..helpers import FormatterWithTraceBack, getLogger, PREFER_ENC from ..helpers import str2LogLevel, getVerbosityFormat, FormatterWithTraceBack, getLogger, PREFER_ENC
# Gets the instance of the logger. # Gets the instance of the logger.
logSys = getLogger("fail2ban") logSys = getLogger("fail2ban")
@ -134,13 +134,16 @@ Report bugs to https://github.com/fail2ban/fail2ban/issues
Option("-m", "--journalmatch", Option("-m", "--journalmatch",
help="journalctl style matches overriding filter file. " help="journalctl style matches overriding filter file. "
"\"systemd-journal\" only"), "\"systemd-journal\" only"),
Option('-l', "--log-level", type="choice", Option('-l', "--log-level",
dest="log_level", dest="log_level",
choices=('heavydebug', 'debug', 'info', 'notice', 'warning', 'error', 'critical'),
default=None, default=None,
help="Log level for the Fail2Ban logger to use"), help="Log level for the Fail2Ban logger to use"),
Option("-v", "--verbose", action='store_true', Option('-v', '--verbose', action="count", dest="verbose",
help="Be verbose in output"), default=None,
help="Increase verbosity"),
Option("--verbosity", action="store", dest="verbose", type=int,
default=None,
help="Set numerical level of verbosity (0..4)"),
Option("-D", "--debuggex", action='store_true', Option("-D", "--debuggex", action='store_true',
help="Produce debuggex.com urls for debugging there"), help="Produce debuggex.com urls for debugging there"),
Option("--print-no-missed", action='store_true', Option("--print-no-missed", action='store_true',
@ -579,17 +582,17 @@ def exec_command_line():
# TODO: taken from -testcases -- move common functionality somewhere # TODO: taken from -testcases -- move common functionality somewhere
if opts.log_level is not None: if opts.log_level is not None:
# so we had explicit settings # so we had explicit settings
logSys.setLevel(getattr(logging, opts.log_level.upper())) logSys.setLevel(str2LogLevel(opts.log_level))
else: else:
# suppress the logging but it would leave unittests' progress dots # suppress the logging but it would leave unittests' progress dots
# ticking, unless like with '-l critical' which would be silent # ticking, unless like with '-l critical' which would be silent
# unless error occurs # unless error occurs
logSys.setLevel(getattr(logging, 'CRITICAL')) logSys.setLevel(logging.CRITICAL)
# Add the default logging handler # Add the default logging handler
stdout = logging.StreamHandler(sys.stdout) stdout = logging.StreamHandler(sys.stdout)
fmt = 'D: %(message)s' fmt = '%(levelname)-1.1s: %(message)s' if opts.verbose <= 1 else '%(message)s'
if opts.log_traceback: if opts.log_traceback:
Formatter = FormatterWithTraceBack Formatter = FormatterWithTraceBack
@ -598,11 +601,7 @@ def exec_command_line():
Formatter = logging.Formatter Formatter = logging.Formatter
# Custom log format for the verbose tests runs # Custom log format for the verbose tests runs
if opts.verbose: stdout.setFormatter(Formatter(getVerbosityFormat(opts.verbose, fmt)))
stdout.setFormatter(Formatter(' %(asctime)-15s %(thread)s' + fmt))
else:
# just prefix with the space
stdout.setFormatter(Formatter(fmt))
logSys.addHandler(stdout) logSys.addHandler(stdout)
fail2banRegex = Fail2banRegex(opts) fail2banRegex = Fail2banRegex(opts)

View File

@ -86,8 +86,10 @@ def getOptParser(doc=""):
dest="log_level", dest="log_level",
default=None, default=None,
help="Log level for the logger to use during running tests"), help="Log level for the logger to use during running tests"),
Option('-v', "--verbosity", action="store", Option('-v', action="count", dest="verbosity",
dest="verbosity", type=int, default=None,
help="Increase verbosity"),
Option("--verbosity", action="store", dest="verbosity", type=int,
default=None, default=None,
help="Set numerical level of verbosity (0..4)"), help="Set numerical level of verbosity (0..4)"),
Option("--log-direct", action="store_false", Option("--log-direct", action="store_false",