Skip to content

ctl run: fix overwriting logging level #2603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Please refer to the [NEWS](NEWS.md) for a list of changes which have an affect o
- `.github/workflows/codespell.yml`, `debian-package.yml`, `regexploit.yml`: Upgrade to `ubuntu-latest` runners (PR#2602 by Sebastian Wagner).

### Tools
- `intelmq/lib/bot_debugger.py`: Fix overwriting the runtime logging level by command line parameter (PR#2603 by Sebastian Wagner, fixes #2563).

### Contrib

Expand Down
39 changes: 21 additions & 18 deletions intelmq/lib/bot_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import sys
from importlib import import_module
from os.path import exists
from typing import Optional, Callable
from unittest import mock

import time

Expand All @@ -35,7 +37,6 @@ class BotDebugger:
EXAMPLE = """\nThe message may look like:
'{"source.network": "178.72.192.0/18", "time.observation": "2017-05-12T05:23:06+00:00"}' """

load_configuration = utils.load_configuration
logging_level = None
output = []
instance = None
Expand Down Expand Up @@ -65,13 +66,15 @@ def __init__(self, runtime_configuration, bot_id, run_subcommand=None, console_t
# Set's the bot's default and initial value for the logging_level to the value we want
bot.logging_level = self.logging_level

self.instance = bot(bot_id, disable_multithreading=True,
standalone=True, # instruct the bot to call SystemExit exception at the end or in case of errors
)
with mock.patch.object(utils, 'get_runtime', self.generate_get_runtime(self.logging_level)):
self.instance = bot(bot_id, disable_multithreading=True,
standalone=True, # instruct the bot to call SystemExit exception at the end or in case of errors
)

def run(self) -> str:
if not self.run_subcommand:
self.instance.start()
with mock.patch.object(utils, 'get_runtime', self.generate_get_runtime(self.logging_level)):
self.instance.start()
else:
self.instance._Bot__connect_pipelines()
if self.run_subcommand == "console":
Expand Down Expand Up @@ -181,33 +184,33 @@ def arg2msg(self, msg):
return msg

def leverageLogger(self, level):
utils.load_configuration = BotDebugger.load_configuration_patch
self.logging_level = level
if self.instance:
self.instance.logger.setLevel(level)
for h in self.instance.logger.handlers:
if isinstance(h, StreamHandler):
h.setLevel(level)

@staticmethod
def load_configuration_patch(configuration_filepath: str, *args, **kwargs) -> dict:
def generate_get_runtime(self, logging_level: Optional[str] = None) -> Callable:
"""
Mock function for utils.load_configuration which ensures the logging level parameter is set to the value we want.
If Runtime configuration is detected, the logging_level parameter is
- inserted in all bot's parameters. bot_id is not accessible here, hence we add it everywhere
- inserted in the global parameters (ex-defaults).
Maybe not everything is necessary, but we can make sure the logging_level is just everywhere where it might be relevant, also in the future.
"""
config = BotDebugger.load_configuration(configuration_filepath=configuration_filepath, *args, **kwargs)
if BotDebugger.logging_level and configuration_filepath == RUNTIME_CONF_FILE:
for bot_id in config.keys():
if bot_id == "global":
config[bot_id]["logging_level"] = BotDebugger.logging_level
else:
config[bot_id]['parameters']["logging_level"] = BotDebugger.logging_level
if "global" not in config:
config["global"] = {"logging_level": BotDebugger.logging_level}
return config
def new_get_runtime(*args, **kwargs):
config = utils.load_configuration(RUNTIME_CONF_FILE, *args, **kwargs)
if logging_level:
for bot_id in config.keys():
if bot_id == "global":
config[bot_id]["logging_level"] = logging_level
else:
config[bot_id]['parameters']["logging_level"] = logging_level
if "global" not in config:
config["global"] = {"logging_level": logging_level}
return config
return new_get_runtime

def messageWizzard(self, msg):
self.instance.logger.error(msg)
Expand Down
Loading