Level.toLevel throws IllegalArgumentException instead of returning default Level
--------------------------------------------------------------------------------
Key: LOG4J2-56
URL: https://issues.apache.org/jira/browse/LOG4J2-56
Project: Log4j 2
Issue Type: Bug
Components: API
Affects Versions: 0.1
Environment: Java 6
Reporter: John Owen Atala
Priority: Minor
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.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]