Added ability to set log file logging level

This commit is contained in:
lepz0r 2022-10-28 10:37:35 +07:00
parent 0cc829642c
commit 8a5a49e2c8
2 changed files with 11 additions and 3 deletions

View File

@ -107,12 +107,14 @@ def parse(requested_trees, logger):
def get_logger(basepath, log_dir, logger_name, log_name, environment_id, log_level="DEBUG", demo_mode=False): def get_logger(basepath, log_dir, logger_name, log_name, environment_id, log_level="DEBUG", file_log_level="DEBUG", demo_mode=False):
""" """
Configures and returns a logger. Configures and returns a logger.
""" """
if type(log_level) == str: if type(log_level) == str:
log_level = log_level.upper() log_level = log_level.upper()
if type(file_log_level) == str:
file_log_level = file_log_level.upper()
global CONSOLE_LOG_LEVEL global CONSOLE_LOG_LEVEL
full_path = os.path.join(basepath, log_dir, "logs") full_path = os.path.join(basepath, log_dir, "logs")
if not os.path.exists(full_path): if not os.path.exists(full_path):
@ -130,7 +132,6 @@ def get_logger(basepath, log_dir, logger_name, log_name, environment_id, log_lev
if logger.handlers: if logger.handlers:
return logger return logger
fh = logging.FileHandler(os.path.join(basepath, log_dir, "logs", "%s.%s.log" % (environment_id, log_name))) fh = logging.FileHandler(os.path.join(basepath, log_dir, "logs", "%s.%s.log" % (environment_id, log_name)))
fh.setLevel("DEBUG")
log_prefix = "[%s] " % log_name.upper() log_prefix = "[%s] " % log_name.upper()
formatter = logging.Formatter("%(asctime)s %(levelname)s:" + log_prefix + "%(message)s", datefmt="%Y-%m-%d %H:%M:%S") formatter = logging.Formatter("%(asctime)s %(levelname)s:" + log_prefix + "%(message)s", datefmt="%Y-%m-%d %H:%M:%S")
@ -141,6 +142,7 @@ def get_logger(basepath, log_dir, logger_name, log_name, environment_id, log_lev
ch = logging.StreamHandler() ch = logging.StreamHandler()
ch.setFormatter(formatter) ch.setFormatter(formatter)
ch.setLevel(log_level) ch.setLevel(log_level)
fh.setLevel(file_log_level)
CONSOLE_LOG_LEVEL = log_level.lower() CONSOLE_LOG_LEVEL = log_level.lower()
logger.addHandler(ch) logger.addHandler(ch)
return CustomAdapter(logger, {}) if demo_mode else logger return CustomAdapter(logger, {}) if demo_mode else logger

View File

@ -39,6 +39,7 @@ class Engine():
server_side=False, server_side=False,
output_directory="trials", output_directory="trials",
log_level="info", log_level="info",
file_log_level="info",
enabled=True, enabled=True,
in_queue_num=None, in_queue_num=None,
out_queue_num=None, out_queue_num=None,
@ -89,6 +90,7 @@ class Engine():
"engine", "engine",
self.environment_id, self.environment_id,
log_level=log_level, log_level=log_level,
file_log_level=file_log_level,
demo_mode=demo_mode) demo_mode=demo_mode)
# Warn if these are not provided # Warn if these are not provided
if not environment_id: if not environment_id:
@ -431,7 +433,10 @@ def get_args():
parser.add_argument('--server-side', action='store_true', help='Enable if this is running on the server side') parser.add_argument('--server-side', action='store_true', help='Enable if this is running on the server side')
parser.add_argument('--log', action='store', default="debug", parser.add_argument('--log', action='store', default="debug",
choices=("debug", "info", "warning", "critical", "error"), choices=("debug", "info", "warning", "critical", "error"),
help="Sets the log level") help="Sets the log level for the console")
parser.add_argument('--file-log', action='store', default="debug",
choices=("debug", "info", "warning", "critical", "error"),
help="Sets the log level for the log file")
parser.add_argument('--no-save-packets', action='store_false', help='Disables recording captured packets') parser.add_argument('--no-save-packets', action='store_false', help='Disables recording captured packets')
parser.add_argument("--in-queue-num", action="store", help="NfQueue number for incoming packets", default=1, type=int) parser.add_argument("--in-queue-num", action="store", help="NfQueue number for incoming packets", default=1, type=int)
parser.add_argument("--out-queue-num", action="store", help="NfQueue number for outgoing packets", default=None, type=int) parser.add_argument("--out-queue-num", action="store", help="NfQueue number for outgoing packets", default=None, type=int)
@ -458,6 +463,7 @@ def main(args):
output_directory=args["output_directory"], output_directory=args["output_directory"],
forwarder=nat_config, forwarder=nat_config,
log_level=args["log"], log_level=args["log"],
file_log_level=args["file_log"],
in_queue_num=args["in_queue_num"], in_queue_num=args["in_queue_num"],
out_queue_num=args["out_queue_num"], out_queue_num=args["out_queue_num"],
save_seen_packets=args["no_save_packets"], save_seen_packets=args["no_save_packets"],