Skip to content

Commit

Permalink
Fix foreground mode and related unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Rivas committed Feb 14, 2022
1 parent 349826b commit e14b911
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
6 changes: 2 additions & 4 deletions api/scripts/wazuh-apid.py
Expand Up @@ -201,12 +201,10 @@ def set_logging(log_path='logs/api.log', foreground_mode=False, debug_mode='info
plain_log = 'plain' in api_conf['logs']['format']
json_log = 'json' in api_conf['logs']['format']
if plain_log:
api_log_file_path = f'{API_LOG_PATH}.log'
set_logging(log_path=api_log_file_path, debug_mode=api_conf['logs']['level'],
set_logging(log_path=f'{API_LOG_PATH}.log', debug_mode=api_conf['logs']['level'],
foreground_mode=args.foreground)
if json_log:
api_log_file_path = f'{API_LOG_PATH}.json'
set_logging(log_path=api_log_file_path, debug_mode=api_conf['logs']['level'],
set_logging(log_path=f'{API_LOG_PATH}.json', debug_mode=api_conf['logs']['level'],
foreground_mode=not plain_log and args.foreground)

logger = logging.getLogger('wazuh-api')
Expand Down
3 changes: 1 addition & 2 deletions framework/wazuh/core/tests/test_wlogging.py
Expand Up @@ -96,8 +96,7 @@ def test_wazuh_logger_setup_logger(mock_fh, mock_add_handler, mock_add_level_nam
@patch.object(wlogging.WazuhLogger, 'debug_level', create=True, new_callable=PropertyMock)
@patch.object(wlogging.WazuhLogger, 'logger_name', create=True, new_callable=PropertyMock)
@patch.object(wlogging.WazuhLogger, 'custom_formatter', create=True, new_callable=PropertyMock)
@patch('logging.Formatter')
def test_wazuh_logger__init__(mock_lformatter, mock_formatter, mock_logger_name, mock_debug_level, mock_foreground_mode,
def test_wazuh_logger__init__(mock_formatter, mock_logger_name, mock_debug_level, mock_foreground_mode,
mock_logger, mock_tag, mock_log_path):
"""Test if WazuhLogger __init__ method initialize all attributes properly.
Expand Down
10 changes: 5 additions & 5 deletions framework/wazuh/core/wlogging.py
Expand Up @@ -90,9 +90,8 @@ def __init__(self, foreground_mode: bool, log_path: str, tag: str, debug_level:
self.foreground_mode = foreground_mode
self.debug_level = debug_level
self.logger_name = logger_name
self.default_formatter = logging.Formatter(self.tag, style='%', datefmt="%Y/%m/%d %H:%M:%S")
if custom_formatter is None:
self.custom_formatter = self.default_formatter
self.custom_formatter = logging.Formatter(self.tag, style='%', datefmt="%Y/%m/%d %H:%M:%S")
else:
self.custom_formatter = custom_formatter(self.tag, style='%', datefmt="%Y/%m/%d %H:%M:%S")

Expand All @@ -104,17 +103,18 @@ def setup_logger(self):
* An additional debug level.
"""
logger = logging.getLogger(self.logger_name)
cf = CustomFilter('log') if self.log_path.endswith('.log') else CustomFilter('json')
logger.propagate = False
# configure logger
fh = CustomFileRotatingHandler(filename=self.log_path, when='midnight')
fh.setFormatter(self.custom_formatter)
if self.logger_name != 'wazuh':
fh.addFilter(CustomFilter('log')) if self.log_path.endswith('.log') else fh.addFilter(CustomFilter('json'))
fh.addFilter(cf)
logger.addHandler(fh)

if self.foreground_mode:
ch = logging.StreamHandler()
ch.setFormatter(self.default_formatter)
ch.setFormatter(self.custom_formatter)
ch.addFilter(cf)
logger.addHandler(ch)

# add a new debug level
Expand Down

0 comments on commit e14b911

Please sign in to comment.