Skip to content

Commit

Permalink
Merge pull request #2613 from OSInside/full_log_info_with_logfile_option
Browse files Browse the repository at this point in the history
Fixed wrong log level on --logfile
  • Loading branch information
Conan-Kudo authored Aug 16, 2024
2 parents e866bc5 + 3c07603 commit a919bdf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion kiwi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
up at ~/.config/kiwi/config.yml or /etc/kiwi.yml
--logfile=<filename>
create a log file containing all log information including
debug information even if this is was not requested by the
debug information even if this was not requested by the
debug switch. The special call: '--logfile stdout' sends all
information to standard out instead of writing to a file
--logsocket=<socketfile>
Expand Down
16 changes: 15 additions & 1 deletion kiwi/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,27 @@ def getLogFlags(self) -> Dict[str, bool]:
"""
return self.log_flags

def setLogLevel(self, level: int, except_for: List[str] = []) -> None:
def setLogLevel(
self, level: int, except_for: List[str] = [], only_for: List[str] = []
) -> None:
"""
Set custom log level for all console handlers
:param int level: log level number
:param list except_for:
set log level to all handlers except for the given list
:param list only_for:
set log level to the given handlers only
if both except_for and only_for handlers are specified,
the except_for list will be ignored
"""
self.log_level = level
if only_for:
for handler_type in self.log_handlers:
if handler_type in only_for:
self.log_handlers[handler_type].setLevel(level)
return
for handler_type in self.log_handlers:
if handler_type not in except_for:
self.log_handlers[handler_type].setLevel(level)
Expand Down
4 changes: 4 additions & 0 deletions kiwi/tasks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ def __init__(self, should_perform_task_setup: bool = True) -> None:
log.setLogLevel(logging.DEBUG)
else:
log.setLogLevel(logging.INFO)

if self.global_args['--logfile'] == 'stdout':
# deactivate standard console logger by setting
# the highest possible log entry level
log.setLogLevel(logging.CRITICAL, except_for=['file', 'socket'])
elif self.global_args['--logfile']:
# set debug level for the file and socket logger
log.setLogLevel(logging.DEBUG, only_for=['file', 'socket'])

# set log flags
if self.global_args['--debug-run-scripts-in-screen']:
Expand Down
31 changes: 30 additions & 1 deletion test/unit/logger_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from unittest.mock import (
patch, call
patch, call, Mock
)
from pytest import raises

Expand Down Expand Up @@ -87,6 +87,35 @@ def test_getLogLevel(self):
self.log.setLogLevel(42)
assert self.log.getLogLevel() == 42

def test_setLogLevel(self):
handler_one = Mock()
handler_two = Mock()
self.log.log_handlers = {
'handler_one': handler_one,
'handler_two': handler_two
}
self.log.setLogLevel(
10, except_for=['handler_one']
)
assert not handler_one.setLevel.called
handler_two.setLevel.assert_called_once_with(10)

handler_one.reset_mock()
handler_two.reset_mock()
self.log.setLogLevel(
10, only_for=['handler_one']
)
assert not handler_two.setLevel.called
handler_one.setLevel.assert_called_once_with(10)

handler_one.reset_mock()
handler_two.reset_mock()
self.log.setLogLevel(
10, except_for=['handler_one'], only_for=['handler_one']
)
assert not handler_two.setLevel.called
handler_one.setLevel.assert_called_once_with(10)

def test_getLogFlags(self):
assert self.log.getLogFlags().get('run-scripts-in-screen') is None
self.log.setLogFlag('run-scripts-in-screen')
Expand Down
20 changes: 19 additions & 1 deletion test/unit/tasks/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,31 @@ def setup(
mock_command_args.assert_called_once_with()
mock_global_args.assert_called_once_with()
assert mock_setLogLevel.call_args_list == [
call(logging.DEBUG), call(logging.CRITICAL, except_for=['file', 'socket'])
call(logging.DEBUG),
call(logging.CRITICAL, except_for=['file', 'socket'])
]
mock_setLogFlag.assert_called_once_with('run-scripts-in-screen')
mock_setlog.assert_called_once_with('stdout')
mock_color.assert_called_once_with()
mock_runtime_config.assert_called_once_with()

mock_setLogLevel.reset_mock()
mock_global_args.return_value = {
'--debug': True,
'--debug-run-scripts-in-screen': True,
'--logfile': 'some',
'--logsocket': 'log_socket',
'--loglevel': None,
'--color-output': True,
'--profile': ['vmxFlavour'],
'--type': None
}
self.task = CliTask()
assert mock_setLogLevel.call_args_list == [
call(logging.DEBUG),
call(logging.DEBUG, only_for=['file', 'socket'])
]

@patch('kiwi.logger.Logger.setLogLevel')
@patch('kiwi.logger.Logger.setLogFlag')
@patch('kiwi.logger.Logger.set_logfile')
Expand Down

0 comments on commit a919bdf

Please sign in to comment.