mirror of
https://github.com/ghndrx/prowler.git
synced 2026-02-10 14:55:00 +00:00
feat(logger): Logs to file with custom log level (#1217)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
# Logging levels
|
||||
logging_levels = {
|
||||
@@ -10,14 +9,41 @@ logging_levels = {
|
||||
"DEBUG": logging.DEBUG,
|
||||
}
|
||||
|
||||
# Initialize you log configuration using the base class
|
||||
# https://docs.python.org/3/library/logging.html#logrecord-attributes
|
||||
logging.basicConfig(
|
||||
stream=sys.stdout,
|
||||
format="%(asctime)s [File: %(filename)s:%(lineno)d] \t[Module: %(module)s]\t %(levelname)s: %(message)s",
|
||||
datefmt="%m/%d/%Y %I:%M:%S %p",
|
||||
)
|
||||
|
||||
def set_logging_config(log_file: str = None, log_level: str = "ERROR"):
|
||||
# Logs formatter
|
||||
stream_formatter = logging.Formatter(
|
||||
"%(asctime)s [File: %(filename)s:%(lineno)d] \t[Module: %(module)s]\t %(levelname)s: %(message)s"
|
||||
)
|
||||
log_file_formatter = logging.Formatter(
|
||||
'{"timestamp": "%(asctime)s", "filename": "%(filename)s:%(lineno)d", "level": "%(levelname)s", "module": "%(module)s", "message": "%(message)s"}'
|
||||
)
|
||||
|
||||
# Where to put logs
|
||||
logging_handlers = []
|
||||
|
||||
# Include stdout by default
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(stream_formatter)
|
||||
logging_handlers.append(stream_handler)
|
||||
|
||||
# Log to file configuration
|
||||
if log_file:
|
||||
# Set log to file handler
|
||||
log_file_handler = logging.FileHandler(log_file)
|
||||
log_file_handler.setFormatter(log_file_formatter)
|
||||
# Append the log formatter
|
||||
logging_handlers.append(log_file_handler)
|
||||
|
||||
# Configure Logger
|
||||
# Initialize you log configuration using the base class
|
||||
# https://docs.python.org/3/library/logging.html#logrecord-attributes
|
||||
logging.basicConfig(
|
||||
level=logging_levels.get(log_level),
|
||||
handlers=logging_handlers,
|
||||
datefmt="%m/%d/%Y %I:%M:%S %p",
|
||||
)
|
||||
|
||||
|
||||
# Retrieve the logger instance
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.ERROR)
|
||||
|
||||
Reference in New Issue
Block a user