Merge branch 'jetty-10.0.x' into jetty-11.0.x

This commit is contained in:
olivier lamy 2020-05-20 11:46:29 +10:00
commit 3cbd8cc5c2
15 changed files with 677 additions and 447 deletions

View File

@ -35,7 +35,7 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<scope>test</scope>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>

View File

@ -61,6 +61,7 @@ import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlets.PushCacheFilter;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.slf4j.LoggerFactory;
public class Http2Server
{
@ -74,6 +75,8 @@ public class Http2Server
ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
server.addBean(LoggerFactory.getILoggerFactory());
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
Path docroot = Paths.get("src/main/resources/docroot");
if (!Files.exists(docroot))

View File

@ -55,7 +55,7 @@ You *must also install the Apache Aries SPI Fly bundles* as many parts of Jetty
[cols=",,",options="header",]
|=======================================================================
|Jar |Bundle Symbolic Name |Location
|org.apache.aries.spifly:org.apache.aries.spifly.dynamic.bundle-1.2.jar |org.apache.aries.spifly.dynamic.bundle
|org.apache.aries.spifly:org.apache.aries.spifly.dynamic.bundle-1.2.4.jar |org.apache.aries.spifly.dynamic.bundle
|https://repo1.maven.org/maven2/org/apache/aries/spifly/org.apache.aries.spifly.dynamic.bundle/[Maven central]
|=======================================================================

View File

@ -4,5 +4,7 @@ org.eclipse.jetty.LEVEL=INFO
#com.example.LEVEL=INFO
## Configure a level for specific logger
#com.example.MyComponent.LEVEL=INFO
## Configure JMX Context Name
# org.eclipse.jetty.logging.jmx.context=JettyServer
## Hide stacks traces in an arbitrary logger tree
#com.example.STACKS=false

View File

@ -30,10 +30,12 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.management.DynamicMBean;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanInfo;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MXBean;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;
@ -151,6 +153,25 @@ public class MBeanContainer implements Container.InheritedListener, Dumpable, De
{
if (o == null)
return null;
if (o instanceof DynamicMBean)
return o;
Class<?> klass = o.getClass();
while (klass != Object.class)
{
MXBean mxbean = klass.getAnnotation(MXBean.class);
if (mxbean != null && mxbean.value())
return o;
String mbeanName = klass.getName() + "MBean";
String mxbeanName = klass.getName() + "MXBean";
Class<?>[] interfaces = klass.getInterfaces();
for (Class<?> type : interfaces)
{
String name = type.getName();
if (name.equals(mbeanName) || name.equals(mxbeanName))
return o;
}
klass = klass.getSuperclass();
}
Object mbean = findMetaData(container, o.getClass()).newInstance(o);
if (mbean instanceof ObjectMBean)
((ObjectMBean)mbean).setMBeanContainer(container);
@ -338,7 +359,9 @@ public class MBeanContainer implements Container.InheritedListener, Dumpable, De
StringBuilder buf = new StringBuilder();
String context = (mbean instanceof ObjectMBean) ? makeName(((ObjectMBean)mbean).getObjectContextBasis()) : null;
String context = (mbean instanceof ObjectMBean)
? makeName(((ObjectMBean)mbean).getObjectContextBasis())
: makeName(reflectContextBasis(mbean));
if (context == null && parentObjectName != null)
context = parentObjectName.getKeyProperty("context");
@ -347,7 +370,9 @@ public class MBeanContainer implements Container.InheritedListener, Dumpable, De
buf.append("type=").append(type);
String name = (mbean instanceof ObjectMBean) ? makeName(((ObjectMBean)mbean).getObjectNameBasis()) : context;
String name = (mbean instanceof ObjectMBean)
? makeName(((ObjectMBean)mbean).getObjectNameBasis())
: makeName(reflectNameBasis(mbean));
if (name != null && name.length() > 1)
buf.append(",").append("name=").append(name);
@ -394,6 +419,28 @@ public class MBeanContainer implements Container.InheritedListener, Dumpable, De
}
}
private String reflectContextBasis(Object mbean)
{
return reflectBasis(mbean, "jmxContext");
}
private String reflectNameBasis(Object mbean)
{
return reflectBasis(mbean, "jmxName");
}
private String reflectBasis(Object mbean, String methodName)
{
try
{
return (String)mbean.getClass().getMethod(methodName).invoke(mbean);
}
catch (Throwable x)
{
return null;
}
}
/**
* @param basis name to strip of special characters.
* @return normalized name

View File

@ -4,15 +4,35 @@
<artifactId>jetty-project</artifactId>
<version>11.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jetty-slf4j-impl</artifactId>
<name>Jetty :: Slf4j Implementation</name>
<description>Slf4j Logging Implementation based on Jetty's older StdErrLog</description>
<properties>
<bundle-symbolic-name>${project.groupId}.logging</bundle-symbolic-name>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-modules</arg>
<arg>java.management</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
@{argLine} ${jetty.surefire.argLine} --add-reads org.eclipse.jetty.logging=java.management
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
@ -28,15 +48,17 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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;
}
}

View File

@ -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,37 +27,130 @@ import org.slf4j.spi.LocationAwareLogger;
public class JettyLogger implements LocationAwareLogger, Logger
{
/**
* The Level to set if you want this logger to be "OFF"
*/
public static final int OFF = 999;
/**
* The Level to set if you want this logger to show all events from all levels.
*/
public static final int ALL = -1;
private final JettyLoggerFactory factory;
private final String name;
private final String condensedName;
private final JettyAppender appender;
private int level;
private boolean hideStacks = false;
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;
this.condensedName = JettyLoggerFactory.condensePackageString(name);
this.condensedName = condensePackageString(name);
this.appender = appender;
this.level = level;
this.hideStacks = hideStacks;
}
/**
* Condenses a classname by stripping down the package name to just the first character of each package name
* segment.Configured
*
* <pre>
* Examples:
* "org.eclipse.jetty.test.FooTest" = "oejt.FooTest"
* "org.eclipse.jetty.server.logging.LogTest" = "orjsl.LogTest"
* </pre>
*
* @param classname the fully qualified class name
* @return the condensed name
*/
private static String condensePackageString(String classname)
{
if (classname == null || classname.isEmpty())
return "";
int rawLen = classname.length();
StringBuilder dense = new StringBuilder(rawLen);
boolean foundStart = false;
boolean hasPackage = false;
int startIdx = -1;
int endIdx = -1;
for (int i = 0; i < rawLen; i++)
{
char c = classname.charAt(i);
if (!foundStart)
{
foundStart = Character.isJavaIdentifierStart(c);
if (foundStart)
{
if (startIdx >= 0)
{
dense.append(classname.charAt(startIdx));
hasPackage = true;
}
startIdx = i;
}
}
if (foundStart)
{
if (Character.isJavaIdentifierPart(c))
endIdx = i;
else
foundStart = false;
}
}
// append remaining from startIdx
if ((startIdx >= 0) && (endIdx >= startIdx))
{
if (hasPackage)
dense.append('.');
dense.append(classname, startIdx, endIdx + 1);
}
return dense.toString();
}
public JettyAppender getAppender()
{
return appender;
}
String getCondensedName()
{
return condensedName;
}
public JettyLevel getLevel()
{
return level;
}
public void setLevel(JettyLevel level)
{
this.level = level;
// apply setLevel to children too.
factory.walkChildrenLoggers(this.getName(), (logger) -> logger.setLevel(level));
}
@Override
public String getName()
{
return name;
}
public boolean isHideStacks()
{
return hideStacks;
}
public void setHideStacks(boolean hideStacks)
{
this.hideStacks = hideStacks;
// apply setHideStacks to children too.
factory.walkChildrenLoggers(this.getName(), (logger) -> logger.setHideStacks(hideStacks));
}
@Override
public void debug(String msg)
{
@ -105,12 +196,6 @@ public class JettyLogger implements LocationAwareLogger, Logger
}
}
@Override
public boolean isDebugEnabled(Marker marker)
{
return isDebugEnabled();
}
@Override
public void debug(Marker marker, String msg)
{
@ -146,6 +231,18 @@ public class JettyLogger implements LocationAwareLogger, Logger
debug(msg, t);
}
@Override
public boolean isDebugEnabled()
{
return level.includes(JettyLevel.DEBUG);
}
@Override
public boolean isDebugEnabled(Marker marker)
{
return isDebugEnabled();
}
@Override
public void error(String msg)
{
@ -191,12 +288,6 @@ public class JettyLogger implements LocationAwareLogger, Logger
}
}
@Override
public boolean isErrorEnabled(Marker marker)
{
return isErrorEnabled();
}
@Override
public void error(Marker marker, String msg)
{
@ -232,66 +323,16 @@ public class JettyLogger implements LocationAwareLogger, Logger
error(msg, t);
}
public JettyAppender getAppender()
{
return appender;
}
/**
* Entry point for {@link LocationAwareLogger}
*/
@Override
public void log(Marker marker, String fqcn, int levelInt, String message, Object[] argArray, Throwable throwable)
public boolean isErrorEnabled()
{
if (this.level <= levelInt)
{
long timestamp = System.currentTimeMillis();
String threadName = Thread.currentThread().getName();
getAppender().emit(this, intToLevel(levelInt), timestamp, threadName, throwable, message, argArray);
}
}
/**
* Dynamic (via Reflection) entry point for {@link SubstituteLogger} usage.
*
* @param event the logging event
*/
@SuppressWarnings("unused")
public void log(LoggingEvent event)
{
// TODO: do we want to support org.sfl4j.Marker?
// TODO: do we want to support org.sfl4j.even.KeyValuePair?
getAppender().emit(this, event.getLevel(), event.getTimeStamp(), event.getThreadName(), event.getThrowable(), event.getMessage(), event.getArgumentArray());
}
public String getCondensedName()
{
return condensedName;
}
public int getLevel()
{
return level;
}
public void setLevel(Level level)
{
Objects.requireNonNull(level, "Level");
setLevel(level.toInt());
}
public void setLevel(int lvlInt)
{
this.level = lvlInt;
// apply setLevel to children too.
factory.walkChildLoggers(this.getName(), (logger) -> logger.setLevel(lvlInt));
return level.includes(JettyLevel.ERROR);
}
@Override
public String getName()
public boolean isErrorEnabled(Marker marker)
{
return name;
return isErrorEnabled();
}
@Override
@ -339,12 +380,6 @@ public class JettyLogger implements LocationAwareLogger, Logger
}
}
@Override
public boolean isInfoEnabled(Marker marker)
{
return isInfoEnabled();
}
@Override
public void info(Marker marker, String msg)
{
@ -380,47 +415,16 @@ public class JettyLogger implements LocationAwareLogger, Logger
info(msg, t);
}
@Override
public boolean isDebugEnabled()
{
return level <= Level.DEBUG.toInt();
}
@Override
public boolean isErrorEnabled()
{
return level <= Level.ERROR.toInt();
}
public boolean isHideStacks()
{
return hideStacks;
}
public void setHideStacks(boolean hideStacks)
{
this.hideStacks = hideStacks;
// apply setHideStacks to children too.
factory.walkChildLoggers(this.getName(), (logger) -> logger.setHideStacks(hideStacks));
}
@Override
public boolean isInfoEnabled()
{
return level <= Level.INFO.toInt();
return level.includes(JettyLevel.INFO);
}
@Override
public boolean isTraceEnabled()
public boolean isInfoEnabled(Marker marker)
{
return level <= Level.TRACE.toInt();
}
@Override
public boolean isWarnEnabled()
{
return level <= Level.WARN.toInt();
return isInfoEnabled();
}
@Override
@ -468,12 +472,6 @@ public class JettyLogger implements LocationAwareLogger, Logger
}
}
@Override
public boolean isTraceEnabled(Marker marker)
{
return isTraceEnabled();
}
@Override
public void trace(Marker marker, String msg)
{
@ -509,6 +507,18 @@ public class JettyLogger implements LocationAwareLogger, Logger
trace(msg, t);
}
@Override
public boolean isTraceEnabled()
{
return level.includes(JettyLevel.TRACE);
}
@Override
public boolean isTraceEnabled(Marker marker)
{
return isTraceEnabled();
}
@Override
public void warn(String msg)
{
@ -554,12 +564,6 @@ public class JettyLogger implements LocationAwareLogger, Logger
}
}
@Override
public boolean isWarnEnabled(Marker marker)
{
return isWarnEnabled();
}
@Override
public void warn(Marker marker, String msg)
{
@ -595,6 +599,18 @@ public class JettyLogger implements LocationAwareLogger, Logger
warn(msg, t);
}
@Override
public boolean isWarnEnabled()
{
return level.includes(JettyLevel.WARN);
}
@Override
public boolean isWarnEnabled(Marker marker)
{
return isWarnEnabled();
}
private void emit(Level level, String msg)
{
long timestamp = System.currentTimeMillis();
@ -636,43 +652,36 @@ public class JettyLogger implements LocationAwareLogger, Logger
getAppender().emit(this, level, timestamp, threadName, throwable, msg);
}
public static Level intToLevel(int level)
/**
* Entry point for {@link LocationAwareLogger}
*/
@Override
public void log(Marker marker, String fqcn, int levelInt, String message, Object[] argArray, Throwable throwable)
{
if (level >= JettyLogger.OFF)
return Level.ERROR;
if (level >= Level.ERROR.toInt())
return Level.ERROR;
if (level >= Level.WARN.toInt())
return Level.WARN;
if (level >= Level.INFO.toInt())
return Level.INFO;
if (level >= Level.DEBUG.toInt())
return Level.DEBUG;
if (level >= Level.TRACE.toInt())
return Level.TRACE;
return Level.TRACE; // everything else
if (this.level.toInt() <= levelInt)
{
long timestamp = System.currentTimeMillis();
String threadName = Thread.currentThread().getName();
getAppender().emit(this, JettyLevel.intToLevel(levelInt).toLevel(), timestamp, threadName, throwable, message, argArray);
}
}
public static String levelToString(int level)
/**
* Dynamic (via Reflection) entry point for {@link SubstituteLogger} usage.
*
* @param event the logging event
*/
@SuppressWarnings("unused")
public void log(LoggingEvent event)
{
if (level >= JettyLogger.OFF)
return "OFF";
if (level >= Level.ERROR.toInt())
return "ERROR";
if (level >= Level.WARN.toInt())
return "WARN";
if (level >= Level.INFO.toInt())
return "INFO";
if (level >= Level.DEBUG.toInt())
return "DEBUG";
if (level >= Level.TRACE.toInt())
return "TRACE";
return "OFF"; // everything else
// TODO: do we want to support org.sfl4j.Marker?
// TODO: do we want to support org.sfl4j.even.KeyValuePair?
getAppender().emit(this, event.getLevel(), event.getTimeStamp(), event.getThreadName(), event.getThrowable(), event.getMessage(), event.getArgumentArray());
}
@Override
public String toString()
{
return String.format("%s:%s:LEVEL=%s", JettyLogger.class.getSimpleName(), name, levelToString(level));
return String.format("%s:%s:LEVEL=%s", JettyLogger.class.getSimpleName(), name, level.name());
}
}

View File

@ -23,12 +23,10 @@ import java.io.InputStream;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Locale;
import java.util.Properties;
import java.util.TimeZone;
import java.util.function.Function;
import org.slf4j.event.Level;
/**
* JettyLogger specific configuration:
@ -39,7 +37,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";
@ -78,76 +76,77 @@ public class JettyLoggerConfiguration
// strip ".STACKS" suffix (if present)
if (startName.endsWith(SUFFIX_STACKS))
{
startName = startName.substring(0, startName.length() - SUFFIX_STACKS.length());
}
Boolean hideStacks = walkParentLoggerNames(startName, (key) ->
Boolean hideStacks = JettyLoggerFactory.walkParentLoggerNames(startName, key ->
{
String stacksBool = properties.getProperty(key + SUFFIX_STACKS);
if (stacksBool != null)
{
return Boolean.parseBoolean(stacksBool);
}
return null;
});
if (hideStacks != null)
return hideStacks;
return DEFAULT_HIDE_STACKS;
return hideStacks != null ? hideStacks : DEFAULT_HIDE_STACKS;
}
/**
* Get the Logging Level for the provided log name. Using the FQCN first, then each package segment from longest to
* shortest.
* <p>Returns the Logging Level for the provided log name.</p>
* <p>Uses the FQCN first, then each package segment from longest to shortest.</p>
*
* @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;
String startName = name != null ? name : "";
// strip trailing dot
// Strip trailing dot.
while (startName.endsWith("."))
{
startName = startName.substring(0, startName.length() - 1);
}
// strip ".LEVEL" suffix (if present)
// Strip ".LEVEL" suffix (if present).
if (startName.endsWith(SUFFIX_LEVEL))
{
startName = startName.substring(0, startName.length() - SUFFIX_LEVEL.length());
}
Integer level = walkParentLoggerNames(startName, (key) ->
JettyLevel level = JettyLoggerFactory.walkParentLoggerNames(startName, key ->
{
String levelStr = properties.getProperty(key + SUFFIX_LEVEL);
if (levelStr != null)
{
return getLevelInt(key, levelStr);
}
return null;
return toJettyLevel(key, levelStr);
});
if (level == null)
{
// try legacy root logging config
String levelStr = properties.getProperty("log" + SUFFIX_LEVEL);
if (levelStr != null)
{
level = getLevelInt("log", levelStr);
}
// Try slf4j root logging config.
String levelStr = properties.getProperty(JettyLogger.ROOT_LOGGER_NAME + SUFFIX_LEVEL);
level = toJettyLevel(JettyLogger.ROOT_LOGGER_NAME, levelStr);
}
if (level != null)
return level;
if (level == null)
{
// Try legacy root logging config.
String levelStr = properties.getProperty("log" + SUFFIX_LEVEL);
level = toJettyLevel("log", levelStr);
}
return DEFAULT_LEVEL;
return level != null ? level : DEFAULT_LEVEL;
}
static JettyLevel toJettyLevel(String loggerName, String levelStr)
{
if (levelStr == null)
return null;
JettyLevel level = JettyLevel.strToLevel(levelStr);
if (level == null)
{
System.err.printf("Unknown JettyLogger/SLF4J Level [%s]=[%s], expecting only %s as values.%n",
loggerName, levelStr, Arrays.toString(JettyLevel.values()));
}
return level;
}
public TimeZone getTimeZone(String key)
@ -155,7 +154,6 @@ public class JettyLoggerConfiguration
String zoneIdStr = properties.getProperty(key);
if (zoneIdStr == null)
return null;
return TimeZone.getTimeZone(zoneIdStr);
}
@ -193,6 +191,11 @@ public class JettyLoggerConfiguration
});
}
public String getString(String key, String defValue)
{
return properties.getProperty(key, defValue);
}
public boolean getBoolean(String key, boolean defValue)
{
String val = properties.getProperty(key, Boolean.toString(defValue));
@ -203,9 +206,7 @@ public class JettyLoggerConfiguration
{
String val = properties.getProperty(key, Integer.toString(defValue));
if (val == null)
{
return defValue;
}
try
{
return Integer.parseInt(val);
@ -216,46 +217,12 @@ public class JettyLoggerConfiguration
}
}
private Integer getLevelInt(String levelSegment, String levelStr)
{
if (levelStr == null)
{
return null;
}
String levelName = levelStr.trim().toUpperCase(Locale.ENGLISH);
switch (levelName)
{
case "ALL":
return JettyLogger.ALL;
case "TRACE":
return Level.TRACE.toInt();
case "DEBUG":
return Level.DEBUG.toInt();
case "INFO":
return Level.INFO.toInt();
case "WARN":
return Level.WARN.toInt();
case "ERROR":
return Level.ERROR.toInt();
case "OFF":
return JettyLogger.OFF;
default:
System.err.println("Unknown JettyLogger/Slf4J Level [" + levelSegment + "]=[" + levelName + "], expecting only [ALL, TRACE, DEBUG, INFO, WARN, ERROR, OFF] as values.");
return null;
}
}
private URL getResource(ClassLoader loader, String resourceName)
{
if (loader == null)
{
return ClassLoader.getSystemResource(resourceName);
}
else
{
return loader.getResource(resourceName);
}
}
/**
@ -286,9 +253,7 @@ public class JettyLoggerConfiguration
{
URL propsUrl = getResource(loader, resourceName);
if (propsUrl == null)
{
return null;
}
try (InputStream in = propsUrl.openStream())
{
@ -303,30 +268,4 @@ public class JettyLoggerConfiguration
}
return null;
}
private <T> T walkParentLoggerNames(String startName, Function<String, T> nameFunction)
{
String nameSegment = startName;
// Checking with FQCN first, then each package segment from longest to shortest.
while ((nameSegment != null) && (nameSegment.length() > 0))
{
T ret = nameFunction.apply(nameSegment);
if (ret != null)
return ret;
// Trim and try again.
int idx = nameSegment.lastIndexOf('.');
if (idx >= 0)
{
nameSegment = nameSegment.substring(0, idx);
}
else
{
nameSegment = null;
}
}
return null;
}
}

View File

@ -19,19 +19,20 @@
package org.eclipse.jetty.logging;
import java.util.Objects;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
import java.util.function.Function;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
public class JettyLoggerFactory implements ILoggerFactory
public class JettyLoggerFactory implements ILoggerFactory, JettyLoggerFactoryMBean
{
private static final String ROOT_LOGGER_NAME = "";
private final JettyLoggerConfiguration configuration;
private final JettyLogger rootLogger;
private ConcurrentMap<String, JettyLogger> loggerMap;
private final ConcurrentMap<String, JettyLogger> loggerMap;
public JettyLoggerFactory(JettyLoggerConfiguration config)
{
@ -41,9 +42,16 @@ public class JettyLoggerFactory implements ILoggerFactory
StdErrAppender appender = new StdErrAppender(configuration);
rootLogger = new JettyLogger(this, ROOT_LOGGER_NAME, appender);
loggerMap.put(ROOT_LOGGER_NAME, rootLogger);
rootLogger.setLevel(configuration.getLevel(ROOT_LOGGER_NAME));
rootLogger = new JettyLogger(this, Logger.ROOT_LOGGER_NAME, appender);
loggerMap.put(Logger.ROOT_LOGGER_NAME, rootLogger);
rootLogger.setLevel(configuration.getLevel(Logger.ROOT_LOGGER_NAME));
}
@SuppressWarnings("unused")
public String jmxContext()
{
// Used to build the ObjectName.
return configuration.getString("org.eclipse.jetty.logging.jmx.context", null);
}
/**
@ -54,11 +62,8 @@ public class JettyLoggerFactory implements ILoggerFactory
*/
public JettyLogger getJettyLogger(String name)
{
if (name.equals(ROOT_LOGGER_NAME))
{
if (name.equals(Logger.ROOT_LOGGER_NAME))
return getRootLogger();
}
return loggerMap.computeIfAbsent(name, this::createLogger);
}
@ -74,109 +79,96 @@ public class JettyLoggerFactory implements ILoggerFactory
return getJettyLogger(name);
}
protected void walkChildLoggers(String parentName, Consumer<JettyLogger> childConsumer)
void walkChildrenLoggers(String parentName, Consumer<JettyLogger> childConsumer)
{
String prefix = parentName;
if (parentName.length() > 0 && !prefix.endsWith("."))
{
prefix += ".";
}
for (JettyLogger logger : loggerMap.values())
{
// Skip self.
if (logger.getName().equals(parentName))
{
// skip self
continue;
}
// is child, and is not itself
// It is a child, and is not itself.
if (logger.getName().startsWith(prefix))
{
childConsumer.accept(logger);
}
}
}
public JettyLogger getRootLogger()
JettyLogger getRootLogger()
{
return rootLogger;
}
private JettyLogger createLogger(String name)
{
// or is that handled by slf4j itself?
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);
}
/**
* Condenses a classname by stripping down the package name to just the first character of each package name
* segment.Configured
*
* <pre>
* Examples:
* "org.eclipse.jetty.test.FooTest" = "oejt.FooTest"
* "org.eclipse.jetty.server.logging.LogTest" = "orjsl.LogTest"
* </pre>
*
* @param classname the fully qualified class name
* @return the condensed name
*/
protected static String condensePackageString(String classname)
static <T> T walkParentLoggerNames(String startName, Function<String, T> nameFunction)
{
if (classname == null || classname.isEmpty())
if (startName == null)
return null;
// Checking with FQCN first, then each package segment from longest to shortest.
String nameSegment = startName;
while (nameSegment.length() > 0)
{
return "";
T ret = nameFunction.apply(nameSegment);
if (ret != null)
return ret;
// Trim and try again.
int idx = nameSegment.lastIndexOf('.');
if (idx >= 0)
nameSegment = nameSegment.substring(0, idx);
else
break;
}
int rawLen = classname.length();
StringBuilder dense = new StringBuilder(rawLen);
boolean foundStart = false;
boolean hasPackage = false;
int startIdx = -1;
int endIdx = -1;
for (int i = 0; i < rawLen; i++)
{
char c = classname.charAt(i);
if (!foundStart)
{
foundStart = Character.isJavaIdentifierStart(c);
if (foundStart)
{
if (startIdx >= 0)
{
dense.append(classname.charAt(startIdx));
hasPackage = true;
}
startIdx = i;
}
}
return nameFunction.apply(Logger.ROOT_LOGGER_NAME);
}
if (foundStart)
{
if (!Character.isJavaIdentifierPart(c))
{
foundStart = false;
}
else
{
endIdx = i;
}
}
}
// append remaining from startIdx
if ((startIdx >= 0) && (endIdx >= startIdx))
{
if (hasPackage)
{
dense.append('.');
}
dense.append(classname, startIdx, endIdx + 1);
}
@Override
public String[] getLoggerNames()
{
TreeSet<String> names = new TreeSet<>(loggerMap.keySet());
return names.toArray(new String[0]);
}
return dense.toString();
@Override
public int getLoggerCount()
{
return loggerMap.size();
}
@Override
public String getLoggerLevel(String loggerName)
{
return walkParentLoggerNames(loggerName, key ->
{
JettyLogger logger = loggerMap.get(key);
if (logger != null)
return logger.getLevel().name();
return null;
});
}
@Override
public boolean setLoggerLevel(String loggerName, String levelName)
{
JettyLevel level = JettyLoggerConfiguration.toJettyLevel(loggerName, levelName);
if (level == null)
{
return false;
}
JettyLogger jettyLogger = getJettyLogger(loggerName);
jettyLogger.setLevel(level);
return true;
}
}

View File

@ -0,0 +1,30 @@
//
// ========================================================================
// 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;
public interface JettyLoggerFactoryMBean
{
int getLoggerCount();
String[] getLoggerNames();
boolean setLoggerLevel(String loggerName, String levelName);
String getLoggerLevel(String loggerName);
}

View File

@ -0,0 +1,73 @@
//
// ========================================================================
// 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.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class JMXTest
{
@Test
public void testJMX() throws Exception
{
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
Properties props = new Properties();
JettyLoggerConfiguration config = new JettyLoggerConfiguration(props);
JettyLoggerFactory loggerFactory = new JettyLoggerFactory(config);
ObjectName objectName = ObjectName.getInstance("org.eclipse.jetty.logging", "type", JettyLoggerFactory.class.getSimpleName().toLowerCase(Locale.ENGLISH));
mbeanServer.registerMBean(loggerFactory, objectName);
JettyLoggerFactoryMBean mbean = JMX.newMBeanProxy(mbeanServer, objectName, JettyLoggerFactoryMBean.class);
// Only the root logger.
assertEquals(1, mbean.getLoggerCount());
JettyLogger child = loggerFactory.getJettyLogger("org.eclipse.jetty.logging");
JettyLogger parent = loggerFactory.getJettyLogger("org.eclipse.jetty");
assertEquals(3, mbean.getLoggerCount());
// Names are sorted.
List<String> expected = new ArrayList<>(Arrays.asList(JettyLogger.ROOT_LOGGER_NAME, parent.getName(), child.getName()));
expected.sort(String::compareTo);
String[] loggerNames = mbean.getLoggerNames();
assertEquals(expected, Arrays.asList(loggerNames));
// Setting the parent level should propagate to the children.
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(JettyLevel.INFO, child.getLevel());
}
}

View File

@ -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
@ -163,41 +162,41 @@ public class JettyLoggerConfigurationTest
public void testGetLoggingLevelBad()
{
Properties props = new Properties();
props.setProperty("log.LEVEL", "WARN");
props.setProperty("ROOT.LEVEL", "WARN");
props.setProperty("org.eclipse.jetty.bad.LEVEL", "EXPECTED_BAD_LEVEL");
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
public void testGetLoggingLevelLowercase()
{
Properties props = new Properties();
props.setProperty("log.LEVEL", "warn");
props.setProperty("ROOT.LEVEL", "warn");
props.setProperty("org.eclipse.jetty.util.LEVEL", "info");
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
public void testGetLoggingLevelRoot()
{
Properties props = new Properties();
props.setProperty("log.LEVEL", "DEBUG");
props.setProperty("ROOT.LEVEL", "DEBUG");
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,40 +224,40 @@ 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
public void testGetLoggingLevelMixedLevels()
{
Properties props = new Properties();
props.setProperty("log.LEVEL", "DEBUG");
props.setProperty("ROOT.LEVEL", "DEBUG");
props.setProperty("org.eclipse.jetty.util.LEVEL", "WARN");
props.setProperty("org.eclipse.jetty.util.ConcurrentHashMap.LEVEL", "ALL");
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"));
}
}

View File

@ -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,22 +510,22 @@ 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);
log.setLevel(JettyLevel.OFF);
assertThat("Logger.toString", log.toString(), is("JettyLogger:xxx:LEVEL=OFF"));
}
@ -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.walkChildLoggers(root.getName(), (logger) ->
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",
JettyLogger.levelToString(log.getLevel()), is(expectedLevel.toString()));
log.getLevel(), is(expectedLevel));
}
}

View File

@ -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;
@ -54,7 +55,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import org.xml.sax.SAXException;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -1595,13 +1595,13 @@ 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
appender.setStream(new PrintStream(logBytes, true));
// make sure we are seeing WARN level events
jettyLogger.setLevel(Level.WARN);
jettyLogger.setLevel(JettyLevel.WARN);
action.run();
}