Skip to content

Commit f099573

Browse files
Added a CLI option (--logging-level) to explicitly specify logging levels per Matthias's request. This is an alternative to the standard gnu/linux-style CLI options of -v, -vv & -vvv. I also updated the help messages for the -v options to describe which logging levels will be set in each case.
1 parent 87519bc commit f099573

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

switch_model/solve.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,32 @@ def define_arguments(argparser):
530530
argparser.add_argument(
531531
'--verbose', '-v', dest='verbose', default=False,
532532
action='store_const', const=logging.WARNING,
533-
help='Show information about model preparation and solution')
533+
help='Show information about model preparation and solution. '
534+
'(print status updates and log messages of "WARNING" or above)')
534535
argparser.add_argument(
535536
'--very-verbose', '-vv', dest='verbose', default=False,
536537
action='store_const', const=logging.INFO,
537-
help='Show more information about model preparation and solution')
538+
help='Show more information about model preparation and solution'
539+
'(print status updates and log messages of "INFO" or above)')
538540
argparser.add_argument(
539541
'--very-very-verbose', '-vvv', dest='verbose', default=False,
540542
action='store_const', const=logging.DEBUG,
541-
help='Show debugging-level information about model preparation and solution')
543+
help='Show debugging-level information about model preparation '
544+
'and solutions (print status updates and log messages of "INFO" '
545+
'or above)')
546+
# The choices for --logging-level must be constrained to standard logging
547+
# levels: https://docs.python.org/3/library/logging.html#levels
548+
argparser.add_argument(
549+
'--logging-level', dest='logging_level',
550+
default=None,
551+
choices = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"],
552+
help='An alternative method of specifying which logging level to '
553+
'display (you may use this instead of -v, -vv, or -vvv).')
542554
argparser.add_argument(
543555
'--quiet', '-q', dest='verbose', action='store_false',
544556
help="Don't show information about model preparation and solution "
545-
"(cancels --verbose setting)")
557+
"(cancels --verbose settings, and disables logging messages "
558+
"for everything short of errors)")
546559
argparser.add_argument(
547560
'--no-post-solve', default=False, action='store_true',
548561
help="Don't run post-solve code on the completed model "

switch_model/utilities.py

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ def create_model(module_list=None, args=sys.argv[1:]):
9191
if hasattr(module, 'define_arguments'):
9292
module.define_arguments(argparser)
9393
model.options = argparser.parse_args(args)
94+
if model.options.logging_level:
95+
# Values of --logging-level are limited to standard logging levels.
96+
# For python >= 3.2, we could store the --logging-level string
97+
# directly into model.options.verbose. For lower versions, we need to
98+
# convert to a constant from the logging package.
99+
level = getattr(logging, model.options.logging_level)
100+
model.options.verbose = level
94101
if model.options.verbose:
95102
logging.basicConfig(level=model.options.verbose)
96103
else:

0 commit comments

Comments
 (0)