Issue #4830 - Cleaning up Level handling with JettyLevel enum
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
f98e2f8e14
commit
1c4f05d2d8
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,6 @@ import java.util.Locale;
|
|||
import java.util.Properties;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.slf4j.event.Level;
|
||||
|
||||
/**
|
||||
* JettyLogger specific configuration:
|
||||
* <ul>
|
||||
|
@ -38,7 +36,7 @@ import org.slf4j.event.Level;
|
|||
*/
|
||||
public class JettyLoggerConfiguration
|
||||
{
|
||||
private static final int DEFAULT_LEVEL = Level.INFO.toInt();
|
||||
private static final JettyLevel DEFAULT_LEVEL = JettyLevel.INFO;
|
||||
private static final boolean DEFAULT_HIDE_STACKS = false;
|
||||
private static final String SUFFIX_LEVEL = ".LEVEL";
|
||||
private static final String SUFFIX_STACKS = ".STACKS";
|
||||
|
@ -97,7 +95,7 @@ public class JettyLoggerConfiguration
|
|||
* @param name the name to get log for
|
||||
* @return the logging level int
|
||||
*/
|
||||
public int getLevel(String name)
|
||||
public JettyLevel getLevel(String name)
|
||||
{
|
||||
if (properties.isEmpty())
|
||||
return DEFAULT_LEVEL;
|
||||
|
@ -114,17 +112,24 @@ public class JettyLoggerConfiguration
|
|||
if (startName.endsWith(SUFFIX_LEVEL))
|
||||
startName = startName.substring(0, startName.length() - SUFFIX_LEVEL.length());
|
||||
|
||||
Integer level = JettyLoggerFactory.walkParentLoggerNames(startName, key ->
|
||||
JettyLevel level = JettyLoggerFactory.walkParentLoggerNames(startName, key ->
|
||||
{
|
||||
String levelStr = properties.getProperty(key + SUFFIX_LEVEL);
|
||||
return LevelUtils.getLevelInt(levelStr);
|
||||
return JettyLevel.strToLevel(levelStr);
|
||||
});
|
||||
|
||||
if (level == null)
|
||||
{
|
||||
// Try slf4j root logging config.
|
||||
String levelStr = properties.getProperty(JettyLogger.ROOT_LOGGER_NAME + SUFFIX_LEVEL);
|
||||
level = JettyLevel.strToLevel(levelStr);
|
||||
}
|
||||
|
||||
if (level == null)
|
||||
{
|
||||
// Try legacy root logging config.
|
||||
String levelStr = properties.getProperty("log" + SUFFIX_LEVEL);
|
||||
level = LevelUtils.getLevelInt(levelStr);
|
||||
level = JettyLevel.strToLevel(levelStr);
|
||||
}
|
||||
|
||||
return level != null ? level : DEFAULT_LEVEL;
|
||||
|
|
|
@ -24,6 +24,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.ILoggerFactory;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -105,7 +107,7 @@ public class JettyLoggerFactory implements ILoggerFactory, JettyLoggerFactoryMBe
|
|||
private JettyLogger createLogger(String name)
|
||||
{
|
||||
JettyAppender appender = rootLogger.getAppender();
|
||||
int level = this.configuration.getLevel(name);
|
||||
JettyLevel level = this.configuration.getLevel(name);
|
||||
boolean hideStacks = this.configuration.getHideStacks(name);
|
||||
return new JettyLogger(this, name, appender, level, hideStacks);
|
||||
}
|
||||
|
@ -154,7 +156,7 @@ public class JettyLoggerFactory implements ILoggerFactory, JettyLoggerFactoryMBe
|
|||
{
|
||||
JettyLogger logger = loggerMap.get(key);
|
||||
if (logger != null)
|
||||
return LevelUtils.intToLevel(logger.getLevel()).toString();
|
||||
return logger.getLevel().toString();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
@ -162,11 +164,15 @@ public class JettyLoggerFactory implements ILoggerFactory, JettyLoggerFactoryMBe
|
|||
@Override
|
||||
public boolean setLoggerLevel(String loggerName, String levelName)
|
||||
{
|
||||
Integer levelInt = LevelUtils.getLevelInt(levelName);
|
||||
if (levelInt == null)
|
||||
JettyLevel level = JettyLevel.strToLevel(levelName);
|
||||
if (level == null)
|
||||
{
|
||||
System.err.printf("Unknown JettyLogger/Slf4J Level [%s]=[%s], expecting only [%s] as values.",
|
||||
loggerName, levelName, Stream.of(JettyLevel.values()).map(JettyLevel::toString).collect(Collectors.joining(", ")));
|
||||
return false;
|
||||
}
|
||||
JettyLogger jettyLogger = getJettyLogger(loggerName);
|
||||
jettyLogger.setLevel(levelInt);
|
||||
jettyLogger.setLevel(level);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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 class LevelUtils
|
||||
{
|
||||
public static Integer getLevelInt(String levelStr)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (levelStr == null)
|
||||
return null;
|
||||
String levelName = levelStr.trim().toUpperCase(Locale.ENGLISH);
|
||||
if ("ALL".equals(levelName))
|
||||
return Level.TRACE.toInt();
|
||||
return Level.valueOf(levelName).toInt();
|
||||
}
|
||||
catch (Throwable x)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Level intToLevel(int level)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (level < JettyLogger.ALL)
|
||||
return Level.TRACE;
|
||||
return Level.intToLevel(level);
|
||||
}
|
||||
catch (Throwable x)
|
||||
{
|
||||
return Level.ERROR;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,7 +29,6 @@ import javax.management.MBeanServer;
|
|||
import javax.management.ObjectName;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.event.Level;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
@ -64,11 +63,11 @@ public class JMXTest
|
|||
assertEquals(expected, Arrays.asList(loggerNames));
|
||||
|
||||
// Setting the parent level should propagate to the children.
|
||||
parent.setLevel(Level.DEBUG);
|
||||
assertEquals(Level.intToLevel(parent.getLevel()).toString(), mbean.getLoggerLevel(child.getName()));
|
||||
parent.setLevel(JettyLevel.DEBUG);
|
||||
assertEquals(parent.getLevel().toString(), mbean.getLoggerLevel(child.getName()));
|
||||
|
||||
// Setting the level via JMX affects the logger.
|
||||
assertTrue(mbean.setLoggerLevel(child.getName(), "INFO"));
|
||||
assertEquals(Level.INFO.toInt(), child.getLevel());
|
||||
assertEquals(JettyLevel.INFO, child.getLevel());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ package org.eclipse.jetty.logging;
|
|||
import java.util.Properties;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.event.Level;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
@ -46,8 +45,8 @@ public class JettyLoggerConfigurationTest
|
|||
assertFalse(appender.isCondensedNames());
|
||||
assertEquals(appender.getThreadPadding(), 10);
|
||||
|
||||
int level = config.getLevel("com.mortbay");
|
||||
assertEquals(Level.WARN.toInt(), level);
|
||||
JettyLevel level = config.getLevel("com.mortbay");
|
||||
assertEquals(JettyLevel.WARN, level);
|
||||
|
||||
boolean stacks = config.getHideStacks("com.mortbay.Foo");
|
||||
assertFalse(stacks);
|
||||
|
@ -59,8 +58,8 @@ public class JettyLoggerConfigurationTest
|
|||
Properties props = new Properties();
|
||||
props.setProperty("com.mortbay.LEVEL", "WARN");
|
||||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
int level = config.getLevel("com.mortbay");
|
||||
assertEquals(Level.WARN.toInt(), level);
|
||||
JettyLevel level = config.getLevel("com.mortbay");
|
||||
assertEquals(JettyLevel.WARN, level);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -70,8 +69,8 @@ public class JettyLoggerConfigurationTest
|
|||
props.setProperty("com.mortbay.LEVEL", "WARN");
|
||||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
// extra trailing dot "."
|
||||
int level = config.getLevel("com.mortbay.");
|
||||
assertEquals(Level.WARN.toInt(), level);
|
||||
JettyLevel level = config.getLevel("com.mortbay.");
|
||||
assertEquals(JettyLevel.WARN, level);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -81,8 +80,8 @@ public class JettyLoggerConfigurationTest
|
|||
props.setProperty("com.mortbay.LEVEL", "WARN");
|
||||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
// asking for name with ".LEVEL"
|
||||
int level = config.getLevel("com.mortbay.Bar.LEVEL");
|
||||
assertEquals(Level.WARN.toInt(), level);
|
||||
JettyLevel level = config.getLevel("com.mortbay.Bar.LEVEL");
|
||||
assertEquals(JettyLevel.WARN, level);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -91,8 +90,8 @@ public class JettyLoggerConfigurationTest
|
|||
Properties props = new Properties();
|
||||
props.setProperty("com.mortbay.LEVEL", "WARN");
|
||||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
int level = config.getLevel("com.mortbay.Foo");
|
||||
assertEquals(Level.WARN.toInt(), level);
|
||||
JettyLevel level = config.getLevel("com.mortbay.Foo");
|
||||
assertEquals(JettyLevel.WARN, level);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -102,8 +101,8 @@ public class JettyLoggerConfigurationTest
|
|||
props.setProperty("com.mortbay.LEVEL", "WARN");
|
||||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
// asking for name that isn't configured, returns default value
|
||||
int level = config.getLevel("org.eclipse.jetty");
|
||||
assertEquals(Level.INFO.toInt(), level);
|
||||
JettyLevel level = config.getLevel("org.eclipse.jetty");
|
||||
assertEquals(JettyLevel.INFO, level);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -168,7 +167,7 @@ public class JettyLoggerConfigurationTest
|
|||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
|
||||
// Default Level (because of bad level value)
|
||||
assertEquals(Level.WARN.toInt(), config.getLevel("org.eclipse.jetty.bad"));
|
||||
assertEquals(JettyLevel.WARN, config.getLevel("org.eclipse.jetty.bad"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -180,9 +179,9 @@ public class JettyLoggerConfigurationTest
|
|||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
|
||||
// Default Level
|
||||
assertEquals(Level.WARN.toInt(), config.getLevel("org.eclipse.jetty"));
|
||||
assertEquals(JettyLevel.WARN, config.getLevel("org.eclipse.jetty"));
|
||||
// Specific Level
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel("org.eclipse.jetty.util"));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel("org.eclipse.jetty.util"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -193,11 +192,11 @@ public class JettyLoggerConfigurationTest
|
|||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
|
||||
// Default Levels
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel(null));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel(""));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel("org.eclipse.jetty"));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel(null));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel(""));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel("org.eclipse.jetty"));
|
||||
String name = JettyLoggerConfigurationTest.class.getName();
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel(name), "Default Logging Level - " + name + " name");
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel(name), "Default Logging Level - " + name + " name");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -209,12 +208,12 @@ public class JettyLoggerConfigurationTest
|
|||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
|
||||
// Default Levels
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel(null));
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel(""));
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel("org.eclipse.jetty"));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel(null));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel(""));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel("org.eclipse.jetty"));
|
||||
|
||||
// Specified Level
|
||||
assertEquals(JettyLogger.ALL, config.getLevel(name));
|
||||
assertEquals(JettyLevel.ALL, config.getLevel(name));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -225,16 +224,16 @@ public class JettyLoggerConfigurationTest
|
|||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
|
||||
// Default Levels
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel(null));
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel(""));
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel("org.eclipse.jetty"));
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel("org.eclipse.jetty.server.BogusObject"));
|
||||
assertEquals(Level.INFO.toInt(), config.getLevel(JettyLoggerConfigurationTest.class.getName()));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel(null));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel(""));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel("org.eclipse.jetty"));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel("org.eclipse.jetty.server.BogusObject"));
|
||||
assertEquals(JettyLevel.INFO, config.getLevel(JettyLoggerConfigurationTest.class.getName()));
|
||||
|
||||
// Configured Level
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel("org.eclipse.jetty.util.Bogus"));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel("org.eclipse.jetty.util"));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel("org.eclipse.jetty.util.resource.PathResource"));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel("org.eclipse.jetty.util.Bogus"));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel("org.eclipse.jetty.util"));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel("org.eclipse.jetty.util.resource.PathResource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -248,17 +247,17 @@ public class JettyLoggerConfigurationTest
|
|||
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
|
||||
|
||||
// Default Levels
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel(null));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel(""));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel("org.eclipse.jetty"));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel("org.eclipse.jetty.server.BogusObject"));
|
||||
assertEquals(Level.DEBUG.toInt(), config.getLevel(JettyLoggerConfigurationTest.class.getName()));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel(null));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel(""));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel("org.eclipse.jetty"));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel("org.eclipse.jetty.server.BogusObject"));
|
||||
assertEquals(JettyLevel.DEBUG, config.getLevel(JettyLoggerConfigurationTest.class.getName()));
|
||||
|
||||
// Configured Level
|
||||
assertEquals(Level.WARN.toInt(), config.getLevel("org.eclipse.jetty.util.MagicUtil"));
|
||||
assertEquals(Level.WARN.toInt(), config.getLevel("org.eclipse.jetty.util"));
|
||||
assertEquals(Level.WARN.toInt(), config.getLevel("org.eclipse.jetty.util.resource.PathResource"));
|
||||
assertEquals(JettyLevel.WARN, config.getLevel("org.eclipse.jetty.util.MagicUtil"));
|
||||
assertEquals(JettyLevel.WARN, config.getLevel("org.eclipse.jetty.util"));
|
||||
assertEquals(JettyLevel.WARN, config.getLevel("org.eclipse.jetty.util.resource.PathResource"));
|
||||
|
||||
assertEquals(JettyLogger.ALL, config.getLevel("org.eclipse.jetty.util.ConcurrentHashMap"));
|
||||
assertEquals(JettyLevel.ALL, config.getLevel("org.eclipse.jetty.util.ConcurrentHashMap"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,11 +91,11 @@ public class JettyLoggerTest
|
|||
|
||||
JettyLogger log = factory.getJettyLogger("xxx");
|
||||
|
||||
log.setLevel(Level.DEBUG);
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
log.debug("testing {} {}", "test", "debug");
|
||||
log.info("testing {} {}", "test", "info");
|
||||
log.warn("testing {} {}", "test", "warn");
|
||||
log.setLevel(Level.INFO);
|
||||
log.setLevel(JettyLevel.INFO);
|
||||
log.debug("YOU SHOULD NOT SEE THIS!");
|
||||
|
||||
output.assertContains("DEBUG:xxx:tname: testing test debug");
|
||||
|
@ -177,7 +177,7 @@ public class JettyLoggerTest
|
|||
appender.setStream(output);
|
||||
|
||||
JettyLogger log = factory.getJettyLogger(JettyLoggerTest.class.getName());
|
||||
log.setLevel(Level.DEBUG);
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
|
||||
String nullMsg = null;
|
||||
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||
|
@ -234,11 +234,11 @@ public class JettyLoggerTest
|
|||
log.warn("See Me");
|
||||
|
||||
// Set to debug level
|
||||
log.setLevel(Level.DEBUG);
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
log.warn("Hear Me");
|
||||
|
||||
// Set to warn level
|
||||
log.setLevel(Level.WARN);
|
||||
log.setLevel(JettyLevel.WARN);
|
||||
log.warn("Cheer Me");
|
||||
|
||||
log.warn("<zoom>", new Throwable("out of focus"));
|
||||
|
@ -279,18 +279,18 @@ public class JettyLoggerTest
|
|||
log.info("I will not buy");
|
||||
|
||||
// Level Debug
|
||||
log.setLevel(Level.DEBUG);
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
log.info("this record");
|
||||
|
||||
// Level All
|
||||
log.setLevel(Level.TRACE);
|
||||
log.setLevel(JettyLevel.TRACE);
|
||||
log.info("it is scratched.");
|
||||
|
||||
log.info("<zoom>", new Throwable("out of focus"));
|
||||
log.info("shot issue", new Throwable("scene lost"));
|
||||
|
||||
// Level Warn
|
||||
log.setLevel(Level.WARN);
|
||||
log.setLevel(JettyLevel.WARN);
|
||||
log.info("sorry?");
|
||||
log.info("<spoken line>", new Throwable("on editing room floor"));
|
||||
|
||||
|
@ -326,7 +326,7 @@ public class JettyLoggerTest
|
|||
JettyLogger log = factory.getJettyLogger(JettyLoggerTest.class.getName());
|
||||
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||
{
|
||||
log.setLevel(Level.ERROR);
|
||||
log.setLevel(JettyLevel.ERROR);
|
||||
|
||||
// Various logging events
|
||||
log.debug("Squelch");
|
||||
|
@ -359,7 +359,7 @@ public class JettyLoggerTest
|
|||
|
||||
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||
{
|
||||
log.setLevel(JettyLogger.OFF);
|
||||
log.setLevel(JettyLevel.OFF);
|
||||
|
||||
// Various logging events
|
||||
log.debug("Squelch");
|
||||
|
@ -399,18 +399,18 @@ public class JettyLoggerTest
|
|||
log.debug("<spoken line>", new Throwable("on editing room floor"));
|
||||
|
||||
// Level Debug
|
||||
log.setLevel(Level.DEBUG);
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
log.debug("my hovercraft is");
|
||||
|
||||
log.debug("<zoom>", new Throwable("out of focus"));
|
||||
log.debug("shot issue", new Throwable("scene lost"));
|
||||
|
||||
// Level All
|
||||
log.setLevel(Level.TRACE);
|
||||
log.setLevel(JettyLevel.TRACE);
|
||||
log.debug("full of eels.");
|
||||
|
||||
// Level Warn
|
||||
log.setLevel(Level.WARN);
|
||||
log.setLevel(JettyLevel.WARN);
|
||||
log.debug("what?");
|
||||
|
||||
// Validate Output
|
||||
|
@ -444,22 +444,22 @@ public class JettyLoggerTest
|
|||
|
||||
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||
{
|
||||
log.setLevel(Level.TRACE);
|
||||
log.setLevel(JettyLevel.TRACE);
|
||||
assertThat("log.level(trace).isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||
|
||||
log.setLevel(Level.DEBUG);
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
assertThat("log.level(debug).isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||
|
||||
log.setLevel(Level.INFO);
|
||||
log.setLevel(JettyLevel.INFO);
|
||||
assertThat("log.level(info).isDebugEnabled", log.isDebugEnabled(), is(false));
|
||||
|
||||
log.setLevel(Level.WARN);
|
||||
log.setLevel(JettyLevel.WARN);
|
||||
assertThat("log.level(warn).isDebugEnabled", log.isDebugEnabled(), is(false));
|
||||
|
||||
log.setLevel(Level.ERROR);
|
||||
log.setLevel(JettyLevel.ERROR);
|
||||
assertThat("log.level(error).isDebugEnabled", log.isDebugEnabled(), is(false));
|
||||
|
||||
log.setLevel(JettyLogger.OFF);
|
||||
log.setLevel(JettyLevel.OFF);
|
||||
assertThat("log.level(null).isDebugEnabled", log.isDebugEnabled(), is(false));
|
||||
}
|
||||
}
|
||||
|
@ -478,23 +478,23 @@ public class JettyLoggerTest
|
|||
|
||||
try (StacklessLogging ignored = new StacklessLogging(log))
|
||||
{
|
||||
log.setLevel(Level.TRACE);
|
||||
assertThat("log.level(trace).getLevel()", log.getLevel(), is(Level.TRACE.toInt()));
|
||||
log.setLevel(JettyLevel.TRACE);
|
||||
assertThat("log.level(trace).getLevel()", log.getLevel(), is(JettyLevel.TRACE));
|
||||
|
||||
log.setLevel(Level.DEBUG);
|
||||
assertThat("log.level(debug).getLevel()", log.getLevel(), is(Level.DEBUG.toInt()));
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
assertThat("log.level(debug).getLevel()", log.getLevel(), is(JettyLevel.DEBUG));
|
||||
|
||||
log.setLevel(Level.INFO);
|
||||
assertThat("log.level(info).getLevel()", log.getLevel(), is(Level.INFO.toInt()));
|
||||
log.setLevel(JettyLevel.INFO);
|
||||
assertThat("log.level(info).getLevel()", log.getLevel(), is(JettyLevel.INFO));
|
||||
|
||||
log.setLevel(Level.WARN);
|
||||
assertThat("log.level(warn).getLevel()", log.getLevel(), is(Level.WARN.toInt()));
|
||||
log.setLevel(JettyLevel.WARN);
|
||||
assertThat("log.level(warn).getLevel()", log.getLevel(), is(JettyLevel.WARN));
|
||||
|
||||
log.setLevel(Level.ERROR);
|
||||
assertThat("log.level(error).getLevel()", log.getLevel(), is(Level.ERROR.toInt()));
|
||||
log.setLevel(JettyLevel.ERROR);
|
||||
assertThat("log.level(error).getLevel()", log.getLevel(), is(JettyLevel.ERROR));
|
||||
|
||||
log.setLevel(888);
|
||||
assertThat("log.level(888).getLevel()", log.getLevel(), is(888));
|
||||
log.setLevel(JettyLevel.OFF);
|
||||
assertThat("log.level(off).getLevel()", log.getLevel(), is(JettyLevel.OFF));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -510,23 +510,23 @@ public class JettyLoggerTest
|
|||
|
||||
JettyLogger log = factory.getJettyLogger("xxx");
|
||||
|
||||
log.setLevel(Level.TRACE);
|
||||
log.setLevel(JettyLevel.TRACE);
|
||||
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=TRACE"));
|
||||
|
||||
log.setLevel(Level.DEBUG);
|
||||
log.setLevel(JettyLevel.DEBUG);
|
||||
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=DEBUG"));
|
||||
|
||||
log.setLevel(Level.INFO);
|
||||
log.setLevel(JettyLevel.INFO);
|
||||
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=INFO"));
|
||||
|
||||
log.setLevel(Level.WARN);
|
||||
log.setLevel(JettyLevel.WARN);
|
||||
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=WARN"));
|
||||
|
||||
log.setLevel(Level.ERROR);
|
||||
log.setLevel(JettyLevel.ERROR);
|
||||
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=ERROR"));
|
||||
|
||||
log.setLevel(JettyLogger.OFF);
|
||||
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=ERROR"));
|
||||
log.setLevel(JettyLevel.OFF);
|
||||
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=OFF"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -544,25 +544,25 @@ public class JettyLoggerTest
|
|||
appender.setStream(output);
|
||||
|
||||
JettyLogger root = factory.getJettyLogger("");
|
||||
assertLevel(root, Level.INFO); // default
|
||||
assertLevel(root, JettyLevel.INFO); // default
|
||||
|
||||
JettyLogger log = factory.getJettyLogger("org.eclipse.jetty.util.Foo");
|
||||
assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
|
||||
assertLevel(log, Level.WARN); // as configured
|
||||
assertLevel(log, JettyLevel.WARN); // as configured
|
||||
|
||||
// Boot stomp it all to debug
|
||||
root.setLevel(Level.DEBUG);
|
||||
root.setLevel(JettyLevel.DEBUG);
|
||||
assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(true));
|
||||
assertLevel(log, Level.DEBUG); // as stomped
|
||||
assertLevel(log, JettyLevel.DEBUG); // as stomped
|
||||
|
||||
// Restore configured
|
||||
factory.walkChildrenLoggers(root.getName(), (logger) ->
|
||||
{
|
||||
int configuredLevel = config.getLevel(logger.getName());
|
||||
JettyLevel configuredLevel = config.getLevel(logger.getName());
|
||||
logger.setLevel(configuredLevel);
|
||||
});
|
||||
assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
|
||||
assertLevel(log, Level.WARN); // as configured
|
||||
assertLevel(log, JettyLevel.WARN); // as configured
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -606,9 +606,9 @@ public class JettyLoggerTest
|
|||
output.assertContains("\t|\t|java.lang.Exception: branch0");
|
||||
}
|
||||
|
||||
private void assertLevel(JettyLogger log, Level expectedLevel)
|
||||
private void assertLevel(JettyLogger log, JettyLevel expectedLevel)
|
||||
{
|
||||
assertThat("Log[" + log.getName() + "].level",
|
||||
LevelUtils.intToLevel(log.getLevel()), is(expectedLevel));
|
||||
log.getLevel(), is(expectedLevel));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ import java.util.Map;
|
|||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.eclipse.jetty.logging.JettyLevel;
|
||||
import org.eclipse.jetty.logging.JettyLogger;
|
||||
import org.eclipse.jetty.logging.StdErrAppender;
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
|
@ -1595,7 +1596,7 @@ public class XmlConfigurationTest
|
|||
JettyLogger jettyLogger = (JettyLogger)slf4jLogger;
|
||||
StdErrAppender appender = (StdErrAppender)jettyLogger.getAppender();
|
||||
PrintStream oldStream = appender.getStream();
|
||||
int oldLevel = jettyLogger.getLevel();
|
||||
JettyLevel oldLevel = jettyLogger.getLevel();
|
||||
try
|
||||
{
|
||||
// capture events
|
||||
|
|
Loading…
Reference in New Issue