Skip to content

fix(logger): handle case-insensitive and integer log levels - #8836

Open
roonakyadav wants to merge 1 commit into
aws-powertools:developfrom
roonakyadav:fix/8823-log-level-normalization
Open

roonakyadav wants to merge 1 commit into
aws-powertools:developfrom
roonakyadav:fix/8823-log-level-normalization

Conversation

@roonakyadav

Copy link
Copy Markdown

Closes #8823

Summary

Changes

  • In aws_lambda_powertools/logging/logger.py:
    • Add _to_numeric_log_level() helper method to resolve string log levels (standard or custom names registered via logging.addLevelName) and integer levels to numeric integers across Python 3.10–3.14 (using logging.getLevelNamesMapping() on Python 3.11+ and falling back to logging._nameToLevel on Python 3.10).
    • Update _get_powertools_log_level() to normalize string log levels case-insensitively (including POWERTOOLS_LOG_LEVEL and LOG_LEVEL environment variables) to uppercase, and preserve valid integer log levels as integers instead of converting them via logging.getLevelName().
    • Update _determine_log_level() to compare numeric levels via _to_numeric_log_level(), avoiding TypeError comparisons between int and str, and return the normalized log level (int or uppercase string) without string conversion.
  • In tests/functional/logger/required_dependencies/test_logger.py:
    • Add parameterized regression test matrix covering lowercase string levels ("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

  1. When AWS_LAMBDA_LOG_LEVEL was set, passing a lowercase level like level="debug" caused logging.getLevelName("debug") to return the string "Level debug". Line 1056 then executed logging.getLevelName("INFO") > "Level debug", which attempted to compare int with str, raising TypeError: '>' not supported between instances of 'int' and 'str'.
  2. When a custom integer level like level=15 was passed, _get_powertools_log_level() called logging.getLevelName(15), returning "Level 15". _determine_log_level() then called .upper(), yielding "LEVEL 15". Calling stdlib setLevel("LEVEL 15") failed with ValueError: Unknown level: 'LEVEL 15'.

User experience

  • Before:
    • Logger(level="debug") crashed with TypeError whenever AWS_LAMBDA_LOG_LEVEL was set.
    • Logger(level=15) crashed with ValueError: Unknown level: 'LEVEL 15'.
  • After:
    • Logger(level="debug") works seamlessly with or without AWS_LAMBDA_LOG_LEVEL, respecting ALC precedence and case-insensitivity.
    • Logger(level=15) works seamlessly, preserving integer levels as accepted by stdlib logging.

Verification

I used an AI assistant to prepare this fix and ran all validations locally:

  1. Reproduction & regression verification:
    • Confirmed failure on develop for Logger(level="debug") with ALC (TypeError: '>' not supported between instances of 'int' and 'str') and Logger(level=15) (ValueError: Unknown level: 'LEVEL 15').
    • Ran newly added regression test matrix:
      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.14s
  2. Full logger test suite:
    • uv run pytest tests/functional/logger/ tests/unit/logger/
      -> 227 passed, 7 warnings in 0.78s
  3. Linting and formatting:
    • make lint (ruff format + ruff check) -> 1417 files left unchanged, All checks passed!
  4. Static type checking:
    • make mypy -> Success: no issues found in 963 source files
  5. Diff hygiene:
    • git 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.

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
@roonakyadav
roonakyadav requested a review from a team as a code owner November 15, 2026 16:10
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 15, 2026
@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 15, 2026

@Manishram-ai Manishram-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if isinstance(level, str):
if isinstance(level, str) and level:

("error", logging.ERROR),
("critical", logging.CRITICAL),
(15, 15),
(logging.INFO, logging.INFO),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A row for the empty string. It fails on this branch without the change above and passes with it.

Suggested change
(logging.INFO, logging.INFO),
(logging.INFO, logging.INFO),
("", logging.INFO),

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logger size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: lowercase log level with Advanced Logging Controls crashes, and custom int levels crash

3 participants