Repository navigation
fix(logger): handle case-insensitive and integer log levels - #8836
roonakyadav wants to merge 1 commit into
Conversation
Lowercase log levels crashed with TypeError when AWS_LAMBDA_LOG_LEVEL was configured due to comparing int with str returned by logging.getLevelName(). Custom integer log levels crashed with ValueError when logging.getLevelName(int) returned "Level <int>" which failed stdlib setLevel(). Normalize string level names case-insensitively, preserve integer levels, and compare numeric logging levels. Add regression test matrix. Closes aws-powertools#8823
|
Manishram-ai
left a comment
There was a problem hiding this comment.
Thanks for working on this. I tried a few level inputs on develop and on this branch. Lowercase names with AWS_LAMBDA_LOG_LEVEL set and level=15 both work here now. The one difference I found is Logger(level=""), which falls back to POWERTOOLS_LOG_LEVEL or INFO on develop and raises ValueError: Unknown level: '' here. With the two suggestions below, tests/functional/logger and tests/unit/logger pass (228 tests), and ruff and mypy are clean.
| # If level is an int (logging.INFO), return its respective string ("INFO") | ||
| if isinstance(level, int): | ||
| return logging.getLevelName(level) | ||
| if isinstance(level, str): |
There was a problem hiding this comment.
The old level or log_level_env sent an empty string on to the env var or INFO. Code like Logger(level=os.getenv("LOG_LEVEL", "")) would start failing on cold start, and checking and level here keeps the old fallback.
| if isinstance(level, str): | |
| if isinstance(level, str) and level: |
| ("error", logging.ERROR), | ||
| ("critical", logging.CRITICAL), | ||
| (15, 15), | ||
| (logging.INFO, logging.INFO), |
There was a problem hiding this comment.
A row for the empty string. It fails on this branch without the change above and passes with it.
| (logging.INFO, logging.INFO), | |
| (logging.INFO, logging.INFO), | |
| ("", logging.INFO), |



Closes #8823
Summary
Changes
aws_lambda_powertools/logging/logger.py:_to_numeric_log_level()helper method to resolve string log levels (standard or custom names registered vialogging.addLevelName) and integer levels to numeric integers across Python 3.10–3.14 (usinglogging.getLevelNamesMapping()on Python 3.11+ and falling back tologging._nameToLevelon Python 3.10)._get_powertools_log_level()to normalize string log levels case-insensitively (includingPOWERTOOLS_LOG_LEVELandLOG_LEVELenvironment variables) to uppercase, and preserve valid integer log levels as integers instead of converting them vialogging.getLevelName()._determine_log_level()to compare numeric levels via_to_numeric_log_level(), avoidingTypeErrorcomparisons betweenintandstr, and return the normalized log level (int or uppercase string) without string conversion.tests/functional/logger/required_dependencies/test_logger.py:"debug","info","warning","error","critical"), custom integer levels (15,25), and standard environment variables (POWERTOOLS_LOG_LEVEL,LOG_LEVEL), both with and without Advanced Logging Controls (AWS_LAMBDA_LOG_LEVEL).Root cause
AWS_LAMBDA_LOG_LEVELwas set, passing a lowercase level likelevel="debug"causedlogging.getLevelName("debug")to return the string"Level debug". Line 1056 then executedlogging.getLevelName("INFO") > "Level debug", which attempted to compareintwithstr, raisingTypeError: '>' not supported between instances of 'int' and 'str'.level=15was passed,_get_powertools_log_level()calledlogging.getLevelName(15), returning"Level 15"._determine_log_level()then called.upper(), yielding"LEVEL 15". Calling stdlibsetLevel("LEVEL 15")failed withValueError: Unknown level: 'LEVEL 15'.User experience
Logger(level="debug")crashed withTypeErrorwheneverAWS_LAMBDA_LOG_LEVELwas set.Logger(level=15)crashed withValueError: Unknown level: 'LEVEL 15'.Logger(level="debug")works seamlessly with or withoutAWS_LAMBDA_LOG_LEVEL, respecting ALC precedence and case-insensitivity.Logger(level=15)works seamlessly, preserving integer levels as accepted by stdliblogging.Verification
I used an AI assistant to prepare this fix and ran all validations locally:
developforLogger(level="debug")with ALC (TypeError: '>' not supported between instances of 'int' and 'str') andLogger(level=15)(ValueError: Unknown level: 'LEVEL 15').uv run pytest tests/functional/logger/required_dependencies/test_logger.py -k "test_logger_level_case_insensitive_and_integer or test_logger_env_var_case_insensitive" -v->
23 passed, 89 deselected in 0.14suv run pytest tests/functional/logger/ tests/unit/logger/->
227 passed, 7 warnings in 0.78smake lint(ruff format + ruff check) ->1417 files left unchanged,All checks passed!make mypy->Success: no issues found in 963 source filesgit diff --check-> Clean, 0 errors.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.