symfony/http-kernel · error · InvalidArgumentException

Invalid log level " ".

Error message

Invalid log level "%s".

What it means

The #[WithLogLevel] attribute restricts its level parameter to valid PSR-3 LogLevel constants, checked at construction by testing that Psr\Log\LogLevel::<UPPERCASED> is defined. Any other string throws InvalidArgumentException at attribute-resolution time.

Solutions

  1. Use a valid PSR-3 level string: 'debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'
  2. Fix case: use lowercase 'warning', not 'WARNING' or 'WARN'
  3. Check defined('Psr\Log\LogLevel::'.strtoupper($level)) before wiring dynamic levels
  4. Reference LogLevel:: class constants to get IDE validation

Example fix

// before
#[WithLogLevel('warn')]
class MyException extends \RuntimeException {}

// after
#[WithLogLevel('warning')]
class MyException extends \RuntimeException {}
Defensive patterns

Strategy: validation

Validate before calling

const LEVELS = ['debug','info','notice','warning','error','critical','alert','emergency'];
if (!LEVELS.includes(level)) throw new Error(`Invalid log level: ${level}`);

Type guard

function isLogLevel(string $level): bool {
  return \defined('Psr\Log\LogLevel::'.strtoupper($level));
}

Try / catch

try { $attr = new \Symfony\Component\HttpKernel\Attribute\WithLogLevel($level); } catch (\InvalidArgumentException $e) { $attr = new \Symfony\Component\HttpKernel\Attribute\WithLogLevel('critical'); }

Prevention

When it happens

Trigger: Declaring #[WithLogLevel('warn')], uppercase/misspelled levels ('ERROR', 'verbose'), or any string that is not debug/info/notice/warning/error/critical/alert/emergency on an exception class or method.

Common situations: Typos in exception logging attributes; using a library-specific level name that isn't PSR-3; config-driven level strings interpolated into the attribute; upgrading code from a logger with non-PSR levels.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13). Data as JSON: /api/errors/c04f5d9be6fd8b5c. Report an issue: GitHub.

Appendix: source

Thrown at Attribute/WithLogLevel.php:30

namespace Symfony\Component\HttpKernel\Attribute;

use Psr\Log\LogLevel;

/**
 * Defines the log level applied to an exception.
 *
 * @author Dejan Angelov <angelovdejan@protonmail.com>
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
final class WithLogLevel
{
    /**
     * @param LogLevel::* $level The level to use to log the exception
     */
    public function __construct(public readonly string $level)
    {
        if (!\defined('Psr\Log\LogLevel::'.strtoupper($this->level))) {
            throw new \InvalidArgumentException(\sprintf('Invalid log level "%s".', $this->level));
        }
    }
}

View on GitHub (pinned to aa3a39d728)