You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "ASF subversion and git services (Jira)" <ji...@apache.org> on 2021/12/23 16:32:00 UTC

[jira] [Commented] (LOG4J2-56) Level.toLevel throws IllegalArgumentException instead of returning default Level

    [ https://issues.apache.org/jira/browse/LOG4J2-56?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17464688#comment-17464688 ] 

ASF subversion and git services commented on LOG4J2-56:
-------------------------------------------------------

Commit 1508e2cd0e1798eb4e7ab41463d0b1bed87a5320 in logging-log4j1's branch refs/heads/BRANCH_2_0_EXPERIMENTAL from Ralph Goers
[ https://gitbox.apache.org/repos/asf?p=logging-log4j1.git;h=1508e2c ]

Fix LOG4J2-56

git-svn-id: https://svn.apache.org/repos/asf/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL@1240019 13f79535-47bb-0310-9956-ffa450edef68


> 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
>            Assignee: Ralph Goers
>            Priority: Minor
>   Original Estimate: 10m
>  Remaining Estimate: 10m
>
> 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 was sent by Atlassian Jira
(v8.20.1#820001)