Skip to content
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

BUG/ENH: ctl run: log level handling #1239

Merged
1 commit merged into from
May 16, 2018
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ CHANGELOG

### Tests

### Tools
- `intelmqctl run` has a new parameter `-l` `--loglevel` to overwrite the log level for the run (#1075).

### Contrib

### Known issues
Expand Down
3 changes: 2 additions & 1 deletion docs/intelmqctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ intelmqctl: file-output was NOT RUNNING.

### run

Run a bot directly for debugging purpose. Command temporarily leverages the logging level to DEBUG so that all the ```self.logger.debug("message")``` in the bot will get displayed.
Run a bot directly for debugging purpose.

If launched with no arguments, the bot will call its init method and start processing messages as usual – but you see everything happens.

Expand Down Expand Up @@ -149,6 +149,7 @@ Note that if another instance of the bot is running, only warning will be displa
intelmqctl: Main instance of the bot is running in the background. You may want to launch: intelmqctl stop file-output
```

You can set the log level with the `-l` flag, e.g. `-l DEBUG`. For the 'console' subcommand, 'DEBUG' is the default.

#### console

Expand Down
23 changes: 14 additions & 9 deletions intelmq/bin/intelmqctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pkg_resources
import psutil

from collections import OrderedDict
from intelmq import (DEFAULTS_CONF_FILE, PIPELINE_CONF_FILE, RUNTIME_CONF_FILE,
VAR_RUN_PATH, BOTS_FILE, HARMONIZATION_CONF_FILE)
from intelmq.lib import utils
Expand Down Expand Up @@ -50,13 +51,13 @@ class Parameters(object):
'access denied': '%s failed to %s because of missing permissions.',
}

LOG_LEVEL = {
'DEBUG': 0,
'INFO': 1,
'WARNING': 2,
'ERROR': 3,
'CRITICAL': 4,
}
LOG_LEVEL = OrderedDict([
('DEBUG', 0),
('INFO', 1),
('WARNING', 2),
('ERROR', 3),
('CRITICAL', 4),
])

RETURN_TYPES = ['text', 'json']
RETURN_TYPE = None
Expand Down Expand Up @@ -121,7 +122,8 @@ def __init__(self, runtime_configuration, logger, controller):
self.logger.error('Directory %s does not exist and cannot be '
'created: %s.', self.PIDDIR, exc)

def bot_run(self, bot_id, run_subcommand=None, console_type=None, message_action_kind=None, dryrun=None, msg=None):
def bot_run(self, bot_id, run_subcommand=None, console_type=None, message_action_kind=None, dryrun=None, msg=None,
loglevel=None):
pid = self.__read_pidfile(bot_id)
module = self.__runtime_configuration[bot_id]['module']
if pid and self.__status_process(pid, module):
Expand All @@ -141,7 +143,7 @@ def bot_run(self, bot_id, run_subcommand=None, console_type=None, message_action

try:
BotDebugger(self.__runtime_configuration[bot_id], bot_id, run_subcommand,
console_type, dryrun, message_action_kind, msg)
console_type, dryrun, message_action_kind, msg, loglevel=loglevel)
retval = 0
except KeyboardInterrupt:
print('Keyboard interrupt.')
Expand Down Expand Up @@ -465,6 +467,9 @@ def __init__(self, interactive: bool = False, return_type: str = "python", quiet
parser_run = subparsers.add_parser('run', help='Run a bot interactively')
parser_run.add_argument('bot_id',
choices=self.runtime_configuration.keys())
parser_run.add_argument('--loglevel', '-l',
nargs='?', default=None,
choices=LOG_LEVEL.keys())
parser_run_subparsers = parser_run.add_subparsers(title='run-subcommands')

parser_run_console = parser_run_subparsers.add_parser('console', help='Get a ipdb live console.')
Expand Down
20 changes: 10 additions & 10 deletions intelmq/lib/bot_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""
import time
import json
import logging
from os.path import exists
from importlib import import_module

Expand All @@ -30,32 +29,33 @@ class BotDebugger:
'{"source.network": "178.72.192.0/18", "time.observation": "2017-05-12T05:23:06+00:00"}' """

load_configuration = utils.load_configuration
logging_level = "DEBUG"
init_log_level = {"console": logging.DEBUG, "message": logging.WARNING, "process": logging.INFO, None: logging.INFO}
logging_level = None

def __init__(self, runtime_configuration, bot_id, run_subcommand=None, console_type=None,
dryrun=None, message_kind=None, msg=None):
dryrun=None, message_kind=None, msg=None, loglevel=None):
self.runtime_configuration = runtime_configuration
self.leverageLogger(level=self.init_log_level[run_subcommand])
module = import_module(self.runtime_configuration['module'])

if loglevel:
self.leverageLogger(loglevel)
elif run_subcommand == "console":
self.leverageLogger("DEBUG")

bot = getattr(module, 'BOT')
if run_subcommand == "message":
bot.init = lambda *args: None
self.instance = bot(bot_id)

if not run_subcommand:
self.leverageLogger(logging.DEBUG)
self.instance.start()
else:
self.instance._Bot__connect_pipelines()
if run_subcommand == "console":
self._console(console_type)
elif run_subcommand == "message":
self.leverageLogger(logging.INFO)
self._message(message_kind, msg)
return
elif run_subcommand == "process":
self.leverageLogger(logging.DEBUG)
self._process(dryrun, msg)
else:
print("Subcommand {} not known.".format(run_subcommand))
Expand All @@ -65,7 +65,7 @@ def _console(self, console_type):
for console in consoles:
try:
module = import_module(console)
except Exception as exc:
except Exception:
pass
else:
if console_type and console != console_type:
Expand Down Expand Up @@ -148,7 +148,7 @@ def leverageLogger(self, level):
@staticmethod
def load_configuration_patch(*args, ** kwargs):
d = BotDebugger.load_configuration(*args, ** kwargs)
if "logging_level" in d:
if "logging_level" in d and BotDebugger.logging_level:
d["logging_level"] = BotDebugger.logging_level
return d

Expand Down