Skip to content

Commit 46b8d79

Browse files
committed
Remove unnecessary FILE log level
Having a custom log level was unnecessarily complex and the routing of debug messages to the stdout was not intuitive. Using a Filter to decide whether a debug level message should be printed to stdout based on the use of the --debug option is cleaner.
1 parent ea63b78 commit 46b8d79

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

convert2rhel/logger.py

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
INFO (20) Prints info message (no date/time, just plain message)
2424
TASK (15) CUSTOM LABEL - Prints a task header message (using asterisks)
2525
DEBUG (10) Prints debug message (using date/time)
26-
FILE (5) CUSTOM LABEL - Outputs with the DEBUG label but only to a file
27-
handle (using date/time)
2826
"""
2927
import logging
3028
import os
@@ -81,52 +79,46 @@ class LogLevelTask(object):
8179
label = "TASK"
8280

8381

84-
class LogLevelFile(object):
85-
level = 5
86-
# Label messages DEBUG as it is contains the same messages as debug, just that they always go
87-
# to the log file.
88-
label = "DEBUG"
89-
90-
9182
def setup_logger_handler(log_name, log_dir):
9283
"""Setup custom logging levels, handlers, and so on. Call this method
9384
from your application's main start point.
9485
log_name = the name for the log file
9586
log_dir = path to the dir where log file will be presented
9687
"""
97-
# set custom labels
88+
# set the TASK custom label
9889
logging.addLevelName(LogLevelTask.level, LogLevelTask.label)
99-
logging.addLevelName(LogLevelFile.level, LogLevelFile.label)
10090
logging.Logger.task = _task
101-
logging.Logger.file = _file
102-
logging.Logger.debug = _debug
10391
logging.Logger.critical = _critical
10492

10593
# enable raising exceptions
10694
logging.raiseExceptions = True
107-
# get root logger
95+
# get the highest level app logger
10896
logger = logging.getLogger("convert2rhel")
109-
# propagate
97+
# propagate log messages to the root logger to be able to capture them in unit tests
98+
# refence: https://github.com/oamg/convert2rhel/pull/179
11099
logger.propagate = True
111-
# set default logging level
112-
logger.setLevel(LogLevelFile.level)
100+
# set the debug level as the default logging level
101+
logger.setLevel(logging.DEBUG)
113102

114103
# create sys.stdout handler for info/debug
115104
stdout_handler = logging.StreamHandler(sys.stdout)
116105
formatter = CustomFormatter("%(message)s")
117106
formatter.disable_colors(should_disable_color_output())
118107
stdout_handler.setFormatter(formatter)
108+
debug_flag_filter = DebugFlagFilter()
109+
stdout_handler.addFilter(debug_flag_filter)
110+
# Set the debug level as the default level. Whether a debug message is printed is decided in DebugFlagFilter.
119111
stdout_handler.setLevel(logging.DEBUG)
120112
logger.addHandler(stdout_handler)
121113

122-
# create file handler
114+
# create a log file handler
123115
if not os.path.isdir(log_dir):
124116
os.makedirs(log_dir) # pragma: no cover
125117
handler = logging.FileHandler(os.path.join(log_dir, log_name), "a")
126118
formatter = CustomFormatter("%(message)s")
127119
formatter.disable_colors(True)
128120
handler.setFormatter(formatter)
129-
handler.setLevel(LogLevelFile.level)
121+
handler.setLevel(logging.DEBUG)
130122
logger.addHandler(handler)
131123

132124

@@ -143,6 +135,18 @@ def should_disable_color_output():
143135
return False
144136

145137

138+
class DebugFlagFilter(logging.Filter):
139+
"""Print debug messages to the stdout only when --debug is used."""
140+
141+
def filter(self, record):
142+
from convert2rhel.toolopts import tool_opts
143+
144+
if record.levelno == logging.DEBUG and not tool_opts.debug:
145+
# not logging a debug level message if the --debug option hasn't been used
146+
return False
147+
return True
148+
149+
146150
def archive_old_logger_files(log_name, log_dir):
147151
"""Archive the old log files to not mess with multiple runs outputs.
148152
Every time a new run begins, this method will be called to archive the previous logs
@@ -187,27 +191,12 @@ def _task(self, msg, *args, **kwargs):
187191
self._log(LogLevelTask.level, msg, args, **kwargs)
188192

189193

190-
def _file(self, msg, *args, **kwargs):
191-
if self.isEnabledFor(LogLevelFile.level):
192-
self._log(LogLevelFile.level, msg, args, **kwargs)
193-
194-
195194
def _critical(self, msg, *args, **kwargs):
196195
if self.isEnabledFor(logging.CRITICAL):
197196
self._log(logging.CRITICAL, msg, args, **kwargs)
198197
sys.exit(msg)
199198

200199

201-
def _debug(self, msg, *args, **kwargs):
202-
if self.isEnabledFor(logging.DEBUG):
203-
from convert2rhel.toolopts import tool_opts
204-
205-
if tool_opts.debug:
206-
self._log(logging.DEBUG, msg, args, **kwargs)
207-
else:
208-
self._log(LogLevelFile.level, msg, args, **kwargs)
209-
210-
211200
class bcolors:
212201
OKGREEN = "\033[92m"
213202
WARNING = "\033[93m"

convert2rhel/unit_tests/logger_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_logger_custom_logger(tmpdir, caplog, clear_loggers):
7979
logger_module.setup_logger_handler(log_name=log_fname, log_dir=str(tmpdir))
8080
logger = logging.getLogger(__name__)
8181
logger.task("Some task: %s", "data")
82-
logger.file("Some task write to file: %s", "data")
82+
logger.debug("Some task write to file: %s", "data")
8383
with pytest.raises(SystemExit):
8484
logger.critical("Critical error: %s", "data")
8585

convert2rhel/utils.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,7 @@ def log_traceback(debug):
367367
on the debug parameter.
368368
"""
369369
traceback_str = get_traceback_str()
370-
if debug:
371-
# Print the traceback to the user when debug option used
372-
loggerinst.debug(traceback_str)
373-
else:
374-
# Print the traceback to the log file in any way
375-
loggerinst.file(traceback_str)
370+
loggerinst.debug(traceback_str)
376371

377372

378373
def get_traceback_str():

0 commit comments

Comments
 (0)