Skip to content

Commit

Permalink
add logging.getLevelName() method to be used instead of creating a di…
Browse files Browse the repository at this point in the history
…ct with all available logging level to validate user input from the config file
  • Loading branch information
vlerkin committed Nov 20, 2024
1 parent 29a9175 commit d58776b
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions scrapyd_k8s/logging_config.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import logging
import sys

VALID_LOG_LEVELS = {
'CRITICAL': logging.CRITICAL,
'ERROR': logging.ERROR,
'WARNING': logging.WARNING,
'INFO': logging.INFO,
'DEBUG': logging.DEBUG,
'NOTSET': logging.NOTSET,
}

def setup_logging(logging_level):
if not logging_level:
logging_level = 'INFO' # Default to INFO if logging_level is None

level_name = str(logging_level).upper()

if level_name not in VALID_LOG_LEVELS:
valid_levels_str = ', '.join(VALID_LOG_LEVELS.keys())
numeric_level = logging.getLevelName(level_name)
if not isinstance(numeric_level, int):
raise ValueError(
f"Invalid logging level '{logging_level}'. Valid levels are: {valid_levels_str}"
f"Invalid logging level '{logging_level}'."
)

logging.basicConfig(
level=VALID_LOG_LEVELS[level_name],
level=numeric_level,
format='%(asctime)s %(name)s [%(levelname)s]: %(message)s',
handlers=[logging.StreamHandler(sys.stdout)]
)

0 comments on commit d58776b

Please sign in to comment.