config: use a section to configure console logging
This commit is contained in:
parent
ef94c6969f
commit
63600416b3
1 changed files with 15 additions and 10 deletions
|
@ -25,6 +25,7 @@ log = logging.getLogger(__name__)
|
|||
# Constants
|
||||
DEFAULT_ENCODING = 'utf-8'
|
||||
DEFAULT_CONFIG_DIRPATH = os.path.expanduser('./')
|
||||
DEFAULT_CONSOLE_LOG_FORMAT = '%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s'
|
||||
|
||||
|
||||
class BaseOption: # pylint: disable=too-many-instance-attributes
|
||||
|
@ -133,7 +134,8 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
|
|||
def parser_help(self):
|
||||
""" Get option help message in arguments parser options """
|
||||
if self.arg_help and self.default is not None:
|
||||
return '{0} (Default: {1})'.format(self.arg_help, self.default)
|
||||
return '{0} (Default: {1})'.format(
|
||||
self.arg_help, re.sub(r'%([^%])', r'%%\1', str(self.default)))
|
||||
if self.arg_help:
|
||||
return self.arg_help
|
||||
return None
|
||||
|
@ -749,12 +751,14 @@ class Config: # pylint: disable=too-many-instance-attributes
|
|||
help='Show verbose messages'
|
||||
)
|
||||
|
||||
self.options_parser.add_argument(
|
||||
'-C',
|
||||
'--console',
|
||||
action='store_true',
|
||||
help='Log on console'
|
||||
)
|
||||
section = self.add_section('console', comment='Console logging')
|
||||
section.add_option(
|
||||
BooleanOption, 'enabled', default=False,
|
||||
arg='--console', short_arg='-C',
|
||||
comment='Enable/disable console log')
|
||||
section.add_option(
|
||||
StringOption, 'log_format', default=DEFAULT_CONSOLE_LOG_FORMAT,
|
||||
arg='--console-log-format', comment='Console log format')
|
||||
|
||||
self.add_options_to_parser(self.options_parser)
|
||||
|
||||
|
@ -813,9 +817,10 @@ class Config: # pylint: disable=too-many-instance-attributes
|
|||
elif options.verbose:
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
|
||||
if options.console:
|
||||
if self.get('console', 'enabled'):
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_formater = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
||||
if self.get('console', 'log_format'):
|
||||
console_formater = logging.Formatter(self.get('console', 'log_format'))
|
||||
console_handler.setFormatter(console_formater)
|
||||
logging.getLogger().addHandler(console_handler)
|
||||
|
||||
|
|
Loading…
Reference in a new issue