Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-56

Level.toLevel throws IllegalArgumentException instead of returning default Level

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 0.1
    • None
    • API
    • None
    • Java 6

    Description

      org.apache.logging.log4j.Level.toLevel(String, Level) ( Level.java line 100) uses enum static method valueOf(String) which throws IllegalArgumentException instead of returning null when enum const doesnt exists. This makes the methods Level.toLevel throw the exception instead of return default value.

      Solution:

      You can:
      a) sorround it with a try-catch statement, like:
      try

      { return valueOf(sArg); }

      catch (Exception e)

      { //exception doesnt matter return defaultLevel; }

      b) translate manually de String to a enum constant, like:
      for (Level level : values()) {
      if (level.name().equals(sArg))

      { return level; }

      }
      return defaultLevel;

      I prefer b) because it saves the try-catch context and the for is nearly the same that the valueOf should do.

      Attachments

        Activity

          People

            rgoers Ralph Goers
            johnowenatala John Owen Atala
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 10m
                10m
                Remaining:
                Remaining Estimate - 10m
                10m
                Logged:
                Time Spent - Not Specified
                Not Specified