Skip to content

Commit 8dee2db

Browse files
committed
Set the minimum logging level via --debug option
Without --debug, both cbi.log and the terminal are restricted to displaying only info, warning and error messages. With --debug, cbi.log includes debug messages by default, but the terminal displays debug messages only with sufficient --verbose flags. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent de40109 commit 8dee2db

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

codebasin/__main__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ def _main():
8989
default=0,
9090
help=_help_string("Decrease verbosity level."),
9191
)
92+
parser.add_argument(
93+
"--debug",
94+
dest="debug",
95+
action="store_const",
96+
default=False,
97+
const=True,
98+
help=_help_string("Enable debug output."),
99+
)
92100
parser.add_argument(
93101
"-R",
94102
"--report",
@@ -152,13 +160,17 @@ def _main():
152160
# Configure logging such that:
153161
# - All messages are written to a log file
154162
# - Only errors are written to the terminal by default
155-
# - Messages written to terminal are based on -q and -v flags
163+
# - Messages written to terminal are based on -q, -v and --debug flags
156164
# - Meta-warnings and statistics are generated by a WarningAggregator
157165
aggregator = WarningAggregator()
158166
log.setLevel(logging.DEBUG)
167+
if args.debug:
168+
min_log_level = logging.DEBUG
169+
else:
170+
min_log_level = logging.INFO
159171

160172
file_handler = logging.FileHandler("cbi.log", mode="w")
161-
file_handler.setLevel(logging.INFO)
173+
file_handler.setLevel(min_log_level)
162174
file_handler.setFormatter(Formatter())
163175
file_handler.addFilter(aggregator)
164176
log.addHandler(file_handler)
@@ -169,7 +181,7 @@ def _main():
169181
print(f"Log file created at {log_path}")
170182

171183
log_level = max(
172-
logging.INFO,
184+
min_log_level,
173185
logging.ERROR - 10 * (args.verbose - args.quiet),
174186
)
175187
stdout_handler = logging.StreamHandler(sys.stdout)

0 commit comments

Comments
 (0)