Skip to content

Commit

Permalink
Refactor log arguments and configuration.
Browse files Browse the repository at this point in the history
To make using e3.log possible without e3.main, I've pulled the
command-line arguments for the logging group out of e3.main and put them
in e3.log. I've also created a new activate method that is driven by the
parsed arguments.

To make e3.log a bit more general, I allow the default logging level to
be specified. The default level is taken to be WARNING; in e3.main, this
is overridden to INFO, to match prior behavior.

I also downcased the titles of the argument groups, as this seems more
consistent with the output of argparse.

Finally, I cleaned up the language on one of the help options for the
log.
  • Loading branch information
manthonyaiello committed Aug 24, 2020
1 parent 833b7d7 commit 6a2bd3d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 64 deletions.
91 changes: 91 additions & 0 deletions src/e3/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass

import logging
import os
import re
import sys
import time
Expand All @@ -30,6 +31,7 @@
Mapping,
)
from logging import _ExcInfoType
from argparse import ArgumentParser, _ArgumentGroup, Namespace

T = TypeVar("T")

Expand Down Expand Up @@ -308,6 +310,95 @@ def add_log_handlers(
logging.getLogger("").addHandler(handler)


def add_logging_argument_group(
argument_parser: ArgumentParser, default_level: int = logging.WARNING,
) -> _ArgumentGroup:
"""Add an argument group with logging options to the argument parser.
To be used with `e3.log.activate_with_args`.
:param argument_parser: the parser in which the group will be created
:param default_level: the logging level that will be used by default
"""
log_group = argument_parser.add_argument_group(title="logging arguments")
log_group.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="make the log output to the console more verbose",
)
log_group.add_argument(
"--log-file",
metavar="FILE",
default=None,
help="store all the logs into the specified file",
)
log_group.add_argument(
"--loglevel",
default=default_level,
help="set the console log level",
choices={
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
},
)
log_group.add_argument(
"--nocolor",
default=False,
action="store_true",
help="disable color and progress bars",
)
log_group.add_argument(
"--json-logs",
default="json-logs" in os.environ.get("E3_ENABLE_FEATURE", "").split(","),
action="store_true",
help="enable JSON formatted logs. They can be activated as well by"
" setting the env var E3_ENABLE_FEATURE=json-logs.",
)
log_group.add_argument(
"--console-logs",
metavar="LINE_PREFIX",
help="disable color, progress bars, and redirect as much as"
" possible to stdout, starting lines with the given prefix.",
)

return log_group


def activate_with_args(args: Namespace, default_level: int = logging.WARNING) -> None:
"""Activate the e3 log using argument parsed.
To be used with `e3.log.add_logging_argument_group`.
:param args: the result of parsing arguments
:param default_level: the logging level assumed by default
"""
global console_logs
global pretty_cli

if args.verbose > 0:
level = default_level - 10 * args.verbose
else:
level = args.loglevel

if args.console_logs:
console_logs = args.console_logs

if args.nocolor:
pretty_cli = False

activate(
level=level,
filename=args.log_file,
json_format=args.json_logs,
e3_debug=level == logging.DEBUG,
)


def activate(
stream_format: str = log_config.stream_fmt,
file_format: str = log_config.file_fmt,
Expand Down
67 changes: 3 additions & 64 deletions src/e3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,54 +81,10 @@ def __init__(
if argument_parser is None:
argument_parser = ArgumentParser()

log_group = argument_parser.add_argument_group(title="Logging arguments")
log_group.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="make the log outputted on the console more verbose (this "
"sets the log level to DEBUG)",
)
log_group.add_argument(
"--log-file",
metavar="FILE",
default=None,
help="store all the logs into the specified file",
)
log_group.add_argument(
"--loglevel",
default=logging.INFO,
help="set the console log level",
choices={
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
},
)
log_group.add_argument(
"--nocolor",
default=False,
action="store_true",
help="disable color and progress bars",
)
log_group.add_argument(
"--json-logs",
default="json-logs" in os.environ.get("E3_ENABLE_FEATURE", "").split(","),
action="store_true",
help="enable JSON formatted logs. They can be activated as well by"
" setting the env var E3_ENABLE_FEATURE=json-logs.",
)
log_group.add_argument(
"--console-logs",
metavar="LINE_PREFIX",
help="disable color, progress bars, and redirect as much as"
" possible to stdout, starting lines with the given prefix.",
)
e3.log.add_logging_argument_group(argument_parser, default_level=logging.INFO)

if platform_args:
plat_group = argument_parser.add_argument_group(title="Platform arguments")
plat_group = argument_parser.add_argument_group(title="platform arguments")
plat_group.add_argument(
"--build",
default=None, # to force autodetection
Expand Down Expand Up @@ -193,25 +149,8 @@ def parse_args(
else:
self.args = self.argument_parser.parse_args(args)

if self.args.nocolor:
e3.log.pretty_cli = False

if not self.__log_handlers_set:
# First set level of verbosity
if self.args.verbose:
level = logging.DEBUG
else:
level = self.args.loglevel

if self.args.console_logs:
e3.log.console_logs = self.args.console_logs

e3.log.activate(
level=level,
filename=self.args.log_file,
json_format=self.args.json_logs,
e3_debug=self.args.verbose > 1,
)
e3.log.activate_with_args(self.args, logging.INFO)
self.__log_handlers_set = True

# Export options to env
Expand Down
1 change: 1 addition & 0 deletions tests/tests_e3/main/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_mainprog_with_console_logs():
r"^mymain:.*:.*: DEBUG this is an info line\r?\n"
"mymain:.*:.* DEBUG this is a debug line",
p.out,
re.MULTILINE,
)


Expand Down

0 comments on commit 6a2bd3d

Please sign in to comment.