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

Level.toLevel throws IllegalArgumentException instead of returning default Level

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 0.1
    • Fix Version/s: None
    • Component/s: API
    • Labels:
      None
    • Environment:

      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.

        Activity

        Hide
        ralph.goers@dslextreme.com Ralph Goers added a comment -

        Thanks. I created a unit test to verify the problem and then resolved it using option b. Please verify and close.

        Show
        ralph.goers@dslextreme.com Ralph Goers added a comment - Thanks. I created a unit test to verify the problem and then resolved it using option b. Please verify and close.
        Hide
        johnowenatala John Owen Atala added a comment -

        Verified at r1240306

        Show
        johnowenatala John Owen Atala added a comment - Verified at r1240306

          People

          • Assignee:
            ralph.goers@dslextreme.com Ralph Goers
            Reporter:
            johnowenatala John Owen Atala
          • Votes:
            0 Vote for this issue
            Watchers:
            0 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

                Development