garydgregory commented on a change in pull request #789:
URL: https://github.com/apache/logging-log4j2/pull/789#discussion_r824936085
##########
File path:
log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java
##########
@@ -75,55 +100,82 @@ public CharMap(final char key, final char replacement) {
return a;
}
- public static org.apache.logging.log4j.Level convertLevel(final Level
level) {
- if (level == null) {
- return org.apache.logging.log4j.Level.ERROR;
- }
- if (level.isGreaterOrEqual(Level.FATAL)) {
- return org.apache.logging.log4j.Level.FATAL;
- } else if (level.isGreaterOrEqual(Level.ERROR)) {
- return org.apache.logging.log4j.Level.ERROR;
- } else if (level.isGreaterOrEqual(Level.WARN)) {
- return org.apache.logging.log4j.Level.WARN;
- } else if (level.isGreaterOrEqual(Level.INFO)) {
- return org.apache.logging.log4j.Level.INFO;
- } else if (level.isGreaterOrEqual(Level.DEBUG)) {
- return org.apache.logging.log4j.Level.DEBUG;
- } else if (level.isGreaterOrEqual(Level.TRACE)) {
- return org.apache.logging.log4j.Level.TRACE;
- }
- return org.apache.logging.log4j.Level.ALL;
+ static int toLog4j2Level(final int v1Level) {
+ // I don't believe anyone uses values much bigger than FATAL
+ if (v1Level >= MAX_CUTOFF_LEVEL) {
+ return StandardLevel.OFF.intLevel();
+ }
+ // Linear transformation up to debug: CUTOFF_LEVEL -> OFF, DEBUG ->
DEBUG
+ if (v1Level > Priority.DEBUG_INT) {
+ final int offset = Math.round((v1Level - Priority.DEBUG_INT) /
100.0f);
+ return StandardLevel.DEBUG.intLevel() - offset;
+ }
+ // Steeper linear transformation
+ if (v1Level > Level.TRACE_INT) {
+ final int offset = Math.round((v1Level - Level.TRACE_INT) / 50.0f);
+ return StandardLevel.TRACE.intLevel() - offset;
+ }
+ if (v1Level > MIN_CUTOFF_LEVEL) {
+ final int offset = Level.TRACE_INT - v1Level;
+ return StandardLevel.TRACE.intLevel() + offset;
+ }
+ return StandardLevel.ALL.intLevel();
}
+ static int toLog4j1Level(int v2Level) {
+ if (v2Level == StandardLevel.ALL.intLevel()) {
+ return Priority.ALL_INT;
+ }
+ if (v2Level > StandardLevel.TRACE.intLevel()) {
+ return MIN_CUTOFF_LEVEL + (StandardLevel.ALL.intLevel() - v2Level);
+ }
+ // Inflating by 50
+ if (v2Level > StandardLevel.DEBUG.intLevel()) {
+ return Level.TRACE_INT + 50 * (StandardLevel.TRACE.intLevel() -
v2Level);
+ }
+ // Inflating by 100
+ if (v2Level > StandardLevel.OFF.intLevel()) {
+ return Priority.DEBUG_INT + 100 * (StandardLevel.DEBUG.intLevel()
- v2Level);
+ }
+ return Priority.OFF_INT;
+ }
- public static Level convertLevel(final org.apache.logging.log4j.Level
level) {
- if (level == null) {
- return Level.ERROR;
- }
- switch (level.getStandardLevel()) {
- case FATAL:
- return Level.FATAL;
- case WARN:
- return Level.WARN;
- case INFO:
- return Level.INFO;
- case DEBUG:
- return Level.DEBUG;
- case TRACE:
- return Level.TRACE;
- case ALL:
- return Level.ALL;
- case OFF:
- return Level.OFF;
- default:
- return Level.ERROR;
+ static int toSyslogLevel(int v2Level) {
+ if (v2Level <= StandardLevel.FATAL.intLevel()) {
+ return 0;
}
+ if (v2Level <= StandardLevel.ERROR.intLevel()) {
+ return 3 - (3 * (StandardLevel.ERROR.intLevel() - v2Level))
+ / (StandardLevel.ERROR.intLevel() -
StandardLevel.FATAL.intLevel());
+ }
+ if (v2Level <= StandardLevel.WARN.intLevel()) {
+ return 4;
+ }
+ if (v2Level <= StandardLevel.INFO.intLevel()) {
+ return 6 - (2 * (StandardLevel.INFO.intLevel() - v2Level))
+ / (StandardLevel.INFO.intLevel() -
StandardLevel.WARN.intLevel());
+ }
+ return 7;
+ }
+
+ public static org.apache.logging.log4j.Level createLevel(final Priority
level) {
+ final String name = level.toString() + "#" +
level.getClass().getName();
+ return org.apache.logging.log4j.Level.forName(name,
toLog4j2Level(level.toInt()));
+ }
+
+ public static org.apache.logging.log4j.Level convertLevel(final Priority
level) {
+ return level != null ? level.getVersion2Level() :
org.apache.logging.log4j.Level.ERROR;
+ }
+
+ public static Level convertLevel(final org.apache.logging.log4j.Level
level) {
+ final Level l = toLevel(level.name(), null);
+ return l != null ? l : Level.ERROR;
}
public static org.apache.logging.log4j.Level convertLevel(final String
level,
final org.apache.logging.log4j.Level defaultLevel) {
final Level l = toLevel(level, null);
- return l != null ? convertLevel(l) : defaultLevel;
+ return l != null ? l.getVersion2Level() : defaultLevel;
Review comment:
I am really not a fan of a variable called "l" (lower-case L) which can
look like a one "1" depending on your font. I prefer to make names obvious.
Here maybe "actualLevel"? Or something nor "l" ;-)
##########
File path:
log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java
##########
@@ -75,55 +100,82 @@ public CharMap(final char key, final char replacement) {
return a;
}
- public static org.apache.logging.log4j.Level convertLevel(final Level
level) {
- if (level == null) {
- return org.apache.logging.log4j.Level.ERROR;
- }
- if (level.isGreaterOrEqual(Level.FATAL)) {
- return org.apache.logging.log4j.Level.FATAL;
- } else if (level.isGreaterOrEqual(Level.ERROR)) {
- return org.apache.logging.log4j.Level.ERROR;
- } else if (level.isGreaterOrEqual(Level.WARN)) {
- return org.apache.logging.log4j.Level.WARN;
- } else if (level.isGreaterOrEqual(Level.INFO)) {
- return org.apache.logging.log4j.Level.INFO;
- } else if (level.isGreaterOrEqual(Level.DEBUG)) {
- return org.apache.logging.log4j.Level.DEBUG;
- } else if (level.isGreaterOrEqual(Level.TRACE)) {
- return org.apache.logging.log4j.Level.TRACE;
- }
- return org.apache.logging.log4j.Level.ALL;
+ static int toLog4j2Level(final int v1Level) {
+ // I don't believe anyone uses values much bigger than FATAL
+ if (v1Level >= MAX_CUTOFF_LEVEL) {
+ return StandardLevel.OFF.intLevel();
+ }
+ // Linear transformation up to debug: CUTOFF_LEVEL -> OFF, DEBUG ->
DEBUG
+ if (v1Level > Priority.DEBUG_INT) {
+ final int offset = Math.round((v1Level - Priority.DEBUG_INT) /
100.0f);
+ return StandardLevel.DEBUG.intLevel() - offset;
+ }
+ // Steeper linear transformation
+ if (v1Level > Level.TRACE_INT) {
+ final int offset = Math.round((v1Level - Level.TRACE_INT) / 50.0f);
+ return StandardLevel.TRACE.intLevel() - offset;
+ }
+ if (v1Level > MIN_CUTOFF_LEVEL) {
+ final int offset = Level.TRACE_INT - v1Level;
+ return StandardLevel.TRACE.intLevel() + offset;
+ }
+ return StandardLevel.ALL.intLevel();
}
+ static int toLog4j1Level(int v2Level) {
+ if (v2Level == StandardLevel.ALL.intLevel()) {
+ return Priority.ALL_INT;
+ }
+ if (v2Level > StandardLevel.TRACE.intLevel()) {
+ return MIN_CUTOFF_LEVEL + (StandardLevel.ALL.intLevel() - v2Level);
+ }
+ // Inflating by 50
+ if (v2Level > StandardLevel.DEBUG.intLevel()) {
+ return Level.TRACE_INT + 50 * (StandardLevel.TRACE.intLevel() -
v2Level);
+ }
+ // Inflating by 100
+ if (v2Level > StandardLevel.OFF.intLevel()) {
+ return Priority.DEBUG_INT + 100 * (StandardLevel.DEBUG.intLevel()
- v2Level);
+ }
+ return Priority.OFF_INT;
+ }
- public static Level convertLevel(final org.apache.logging.log4j.Level
level) {
- if (level == null) {
- return Level.ERROR;
- }
- switch (level.getStandardLevel()) {
- case FATAL:
- return Level.FATAL;
- case WARN:
- return Level.WARN;
- case INFO:
- return Level.INFO;
- case DEBUG:
- return Level.DEBUG;
- case TRACE:
- return Level.TRACE;
- case ALL:
- return Level.ALL;
- case OFF:
- return Level.OFF;
- default:
- return Level.ERROR;
+ static int toSyslogLevel(int v2Level) {
+ if (v2Level <= StandardLevel.FATAL.intLevel()) {
+ return 0;
}
+ if (v2Level <= StandardLevel.ERROR.intLevel()) {
+ return 3 - (3 * (StandardLevel.ERROR.intLevel() - v2Level))
+ / (StandardLevel.ERROR.intLevel() -
StandardLevel.FATAL.intLevel());
+ }
+ if (v2Level <= StandardLevel.WARN.intLevel()) {
+ return 4;
+ }
+ if (v2Level <= StandardLevel.INFO.intLevel()) {
+ return 6 - (2 * (StandardLevel.INFO.intLevel() - v2Level))
+ / (StandardLevel.INFO.intLevel() -
StandardLevel.WARN.intLevel());
+ }
+ return 7;
+ }
+
+ public static org.apache.logging.log4j.Level createLevel(final Priority
level) {
+ final String name = level.toString() + "#" +
level.getClass().getName();
+ return org.apache.logging.log4j.Level.forName(name,
toLog4j2Level(level.toInt()));
+ }
+
+ public static org.apache.logging.log4j.Level convertLevel(final Priority
level) {
+ return level != null ? level.getVersion2Level() :
org.apache.logging.log4j.Level.ERROR;
+ }
+
+ public static Level convertLevel(final org.apache.logging.log4j.Level
level) {
+ final Level l = toLevel(level.name(), null);
+ return l != null ? l : Level.ERROR;
}
public static org.apache.logging.log4j.Level convertLevel(final String
level,
final org.apache.logging.log4j.Level defaultLevel) {
final Level l = toLevel(level, null);
- return l != null ? convertLevel(l) : defaultLevel;
+ return l != null ? l.getVersion2Level() : defaultLevel;
Review comment:
I am really not a fan of a variable called "l" (lower-case L) which can
look like a one "1" depending on your font. I prefer to make names obvious.
Here maybe "actualLevel"? Or something nor "l" ;-)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]