diff --git a/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLevel.java b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLevel.java new file mode 100644 index 00000000000..038ca466a99 --- /dev/null +++ b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLevel.java @@ -0,0 +1,114 @@ +// +// ======================================================================== +// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under +// the terms of the Eclipse Public License 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0 +// +// This Source Code may also be made available under the following +// Secondary Licenses when the conditions for such availability set +// forth in the Eclipse Public License, v. 2.0 are satisfied: +// the Apache License v2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.logging; + +import java.util.Locale; + +import org.slf4j.event.Level; + +public enum JettyLevel +{ + // Intentionally sorted incrementally by level int + ALL(Level.TRACE.toInt() - 10), + TRACE(Level.TRACE), + DEBUG(Level.DEBUG), + INFO(Level.INFO), + WARN(Level.WARN), + ERROR(Level.ERROR), + OFF(Level.ERROR.toInt() + 1); + + private final Level level; + private final int levelInt; + + JettyLevel(Level level) + { + this.level = level; + this.levelInt = level.toInt(); + } + + JettyLevel(int i) + { + this.level = null; + this.levelInt = i; + } + + public static JettyLevel fromLevel(Level slf4jLevel) + { + for (JettyLevel level : JettyLevel.values()) + { + if (slf4jLevel.toInt() == level.levelInt) + return level; + } + return OFF; + } + + public int toInt() + { + return levelInt; + } + + public Level toLevel() + { + return level; + } + + /** + * Tests that a provided level is included by the level value of this level. + * + * @param testLevel the level to test against. + * @return true if includes this includes the test level. + */ + public boolean includes(JettyLevel testLevel) + { + return (this.levelInt <= testLevel.levelInt); + } + + @Override + public String toString() + { + return name(); + } + + public static JettyLevel intToLevel(int levelInt) + { + for (JettyLevel level : JettyLevel.values()) + { + if (levelInt <= level.levelInt) + return level; + } + return OFF; + } + + public static JettyLevel strToLevel(String levelStr) + { + if (levelStr == null) + { + return null; + } + + String levelName = levelStr.trim().toUpperCase(Locale.ENGLISH); + for (JettyLevel level : JettyLevel.values()) + { + if (level.name().equals(levelName)) + return level; + } + + return null; + } +} diff --git a/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLogger.java b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLogger.java index e7eaff98155..7367a215911 100644 --- a/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLogger.java +++ b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLogger.java @@ -18,8 +18,6 @@ package org.eclipse.jetty.logging; -import java.util.Objects; - import org.slf4j.Logger; import org.slf4j.Marker; import org.slf4j.event.Level; @@ -29,28 +27,19 @@ import org.slf4j.spi.LocationAwareLogger; public class JettyLogger implements LocationAwareLogger, Logger { - /** - * The Level to set if you want this logger to be "OFF" - */ - static final int OFF = 999; - /** - * The Level to set if you want this logger to show all events from all levels. - */ - static final int ALL = Level.TRACE.toInt(); - private final JettyLoggerFactory factory; private final String name; private final String condensedName; private final JettyAppender appender; - private int level; + private JettyLevel level; private boolean hideStacks; public JettyLogger(JettyLoggerFactory factory, String name, JettyAppender appender) { - this(factory, name, appender, Level.INFO.toInt(), false); + this(factory, name, appender, JettyLevel.INFO, false); } - public JettyLogger(JettyLoggerFactory factory, String name, JettyAppender appender, int level, boolean hideStacks) + public JettyLogger(JettyLoggerFactory factory, String name, JettyAppender appender, JettyLevel level, boolean hideStacks) { this.factory = factory; this.name = name; @@ -303,11 +292,11 @@ public class JettyLogger implements LocationAwareLogger, Logger @Override public void log(Marker marker, String fqcn, int levelInt, String message, Object[] argArray, Throwable throwable) { - if (this.level <= levelInt) + if (this.level.toInt() <= levelInt) { long timestamp = System.currentTimeMillis(); String threadName = Thread.currentThread().getName(); - getAppender().emit(this, LevelUtils.intToLevel(levelInt), timestamp, threadName, throwable, message, argArray); + getAppender().emit(this, JettyLevel.intToLevel(levelInt).toLevel(), timestamp, threadName, throwable, message, argArray); } } @@ -329,23 +318,22 @@ public class JettyLogger implements LocationAwareLogger, Logger return condensedName; } - public int getLevel() + public JettyLevel getLevel() { return level; } public void setLevel(Level level) { - Objects.requireNonNull(level, "Level"); - setLevel(level.toInt()); + setLevel(JettyLevel.fromLevel(level)); } - public void setLevel(int lvlInt) + public void setLevel(JettyLevel level) { - this.level = lvlInt; + this.level = level; // apply setLevel to children too. - factory.walkChildrenLoggers(this.getName(), (logger) -> logger.setLevel(lvlInt)); + factory.walkChildrenLoggers(this.getName(), (logger) -> logger.setLevel(level)); } @Override @@ -443,13 +431,13 @@ public class JettyLogger implements LocationAwareLogger, Logger @Override public boolean isDebugEnabled() { - return level <= Level.DEBUG.toInt(); + return level.includes(JettyLevel.DEBUG); } @Override public boolean isErrorEnabled() { - return level <= Level.ERROR.toInt(); + return level.includes(JettyLevel.ERROR); } public boolean isHideStacks() @@ -468,19 +456,19 @@ public class JettyLogger implements LocationAwareLogger, Logger @Override public boolean isInfoEnabled() { - return level <= Level.INFO.toInt(); + return level.includes(JettyLevel.INFO); } @Override public boolean isTraceEnabled() { - return level <= Level.TRACE.toInt(); + return level.includes(JettyLevel.TRACE); } @Override public boolean isWarnEnabled() { - return level <= Level.WARN.toInt(); + return level.includes(JettyLevel.WARN); } @Override @@ -699,6 +687,6 @@ public class JettyLogger implements LocationAwareLogger, Logger @Override public String toString() { - return String.format("%s:%s:LEVEL=%s", JettyLogger.class.getSimpleName(), name, LevelUtils.intToLevel(level)); + return String.format("%s:%s:LEVEL=%s", JettyLogger.class.getSimpleName(), name, level.name()); } } diff --git a/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLoggerConfiguration.java b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLoggerConfiguration.java index ba00a0abadf..4096ab4f70e 100644 --- a/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLoggerConfiguration.java +++ b/jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLoggerConfiguration.java @@ -27,8 +27,6 @@ import java.util.Locale; import java.util.Properties; import java.util.TimeZone; -import org.slf4j.event.Level; - /** * JettyLogger specific configuration: *