Issue #4830 - Add JMX to new Jetty 10 jetty-slf4j-impl.

Updates after review.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2020-05-08 22:03:03 +02:00
parent 69e1d119fe
commit d6d994db5c
4 changed files with 114 additions and 120 deletions

View File

@ -109,6 +109,48 @@ public class JettyLogger implements LocationAwareLogger, Logger
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)
{
@ -154,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)
{
@ -195,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)
{
@ -240,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)
{
@ -281,65 +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.toInt() <= levelInt)
{
long timestamp = System.currentTimeMillis();
String threadName = Thread.currentThread().getName();
getAppender().emit(this, JettyLevel.intToLevel(levelInt).toLevel(), 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 JettyLevel getLevel()
{
return level;
}
public void setLevel(Level level)
{
setLevel(JettyLevel.fromLevel(level));
}
public void setLevel(JettyLevel level)
{
this.level = level;
// apply setLevel to children too.
factory.walkChildrenLoggers(this.getName(), (logger) -> logger.setLevel(level));
return level.includes(JettyLevel.ERROR);
}
@Override
public String getName()
public boolean isErrorEnabled(Marker marker)
{
return name;
return isErrorEnabled();
}
@Override
@ -387,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)
{
@ -428,31 +415,6 @@ public class JettyLogger implements LocationAwareLogger, Logger
info(msg, t);
}
@Override
public boolean isDebugEnabled()
{
return level.includes(JettyLevel.DEBUG);
}
@Override
public boolean isErrorEnabled()
{
return level.includes(JettyLevel.ERROR);
}
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 boolean isInfoEnabled()
{
@ -460,15 +422,9 @@ public class JettyLogger implements LocationAwareLogger, Logger
}
@Override
public boolean isTraceEnabled()
public boolean isInfoEnabled(Marker marker)
{
return level.includes(JettyLevel.TRACE);
}
@Override
public boolean isWarnEnabled()
{
return level.includes(JettyLevel.WARN);
return isInfoEnabled();
}
@Override
@ -516,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)
{
@ -557,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)
{
@ -602,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)
{
@ -643,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();
@ -684,6 +652,33 @@ public class JettyLogger implements LocationAwareLogger, Logger
getAppender().emit(this, level, timestamp, threadName, throwable, msg);
}
/**
* Entry point for {@link LocationAwareLogger}
*/
@Override
public void log(Marker marker, String fqcn, int levelInt, String message, Object[] argArray, Throwable throwable)
{
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);
}
}
/**
* 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());
}
@Override
public String toString()
{

View File

@ -145,7 +145,7 @@ public class JettyLoggerConfiguration
if (level == null)
{
System.err.printf("Unknown JettyLogger/Slf4J Level [%s]=[%s], expecting only [%s] as values.",
loggerName, levelStr, Stream.of(JettyLevel.values()).map(JettyLevel::toString).collect(Collectors.joining(", ")));
loggerName, levelStr, Stream.of(JettyLevel.values()).map(JettyLevel::name).collect(Collectors.joining(", ")));
}
return level;
}

View File

@ -154,7 +154,7 @@ public class JettyLoggerFactory implements ILoggerFactory, JettyLoggerFactoryMBe
{
JettyLogger logger = loggerMap.get(key);
if (logger != null)
return logger.getLevel().toString();
return logger.getLevel().name();
return null;
});
}

View File

@ -55,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;
@ -1602,7 +1601,7 @@ public class XmlConfigurationTest
// 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();
}