Skip to content

Commit

Permalink
Issue #4572 - Updating to slf4j 2.0.0-alpha1
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Feb 20, 2020
1 parent aecbab4 commit 12706fd
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 18 deletions.
2 changes: 1 addition & 1 deletion jetty-maven-plugin/src/it/javax-annotation-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
<artifactId>slf4j-impl</artifactId>
<version>@log4j2.version@</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.event.Level;
import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.spi.LocationAwareLogger;

public class JettyLogger extends MarkerIgnoringBase implements Logger
public class JettyLogger implements LocationAwareLogger, Logger
{
/**
* The Level to set if you want this logger to be "OFF"
Expand Down Expand Up @@ -92,6 +93,42 @@ public void debug(String msg, Throwable throwable)
}
}

@Override
public boolean isDebugEnabled(Marker marker)
{
return isDebugEnabled();
}

@Override
public void debug(Marker marker, String msg)
{
debug(msg);
}

@Override
public void debug(Marker marker, String format, Object arg)
{
debug(format, arg);
}

@Override
public void debug(Marker marker, String format, Object arg1, Object arg2)
{
debug(format, arg1, arg2);
}

@Override
public void debug(Marker marker, String format, Object... arguments)
{
debug(format, arguments);
}

@Override
public void debug(Marker marker, String msg, Throwable t)
{
debug(msg, t);
}

@Override
public void error(String msg)
{
Expand Down Expand Up @@ -137,11 +174,56 @@ public void error(String msg, Throwable throwable)
}
}

@Override
public boolean isErrorEnabled(Marker marker)
{
return isErrorEnabled();
}

@Override
public void error(Marker marker, String msg)
{
error(msg);
}

@Override
public void error(Marker marker, String format, Object arg)
{
error(format, arg);
}

@Override
public void error(Marker marker, String format, Object arg1, Object arg2)
{
error(format, arg1, arg2);
}

@Override
public void error(Marker marker, String format, Object... arguments)
{
error(format, arguments);
}

@Override
public void error(Marker marker, String msg, Throwable t)
{
error(msg, t);
}

public JettyAppender getAppender()
{
return appender;
}

@Override
public void log(Marker marker, String fqcn, int levelInt, String message, Object[] argArray, Throwable throwable)
{
if (this.level <= levelInt)
{
getAppender().emit(asEvent(intToLevel(levelInt), message, throwable, argArray));
}
}

public void setAppender(JettyAppender appender)
{
this.appender = appender;
Expand Down Expand Up @@ -169,8 +251,7 @@ public void setLevel(int lvlInt)

// apply setLevel to children too.
JettyLoggerFactory jettyLoggerFactory = JettyLoggerFactory.getLoggerFactory();
jettyLoggerFactory.walkChildLoggers(this.getName(),
(logger) -> logger.setLevel(lvlInt));
jettyLoggerFactory.walkChildLoggers(this.getName(), (logger) -> logger.setLevel(lvlInt));
}

@Override
Expand Down Expand Up @@ -224,6 +305,42 @@ public void info(String msg, Throwable throwable)
}
}

@Override
public boolean isInfoEnabled(Marker marker)
{
return isInfoEnabled();
}

@Override
public void info(Marker marker, String msg)
{
info(msg);
}

@Override
public void info(Marker marker, String format, Object arg)
{
info(format, arg);
}

@Override
public void info(Marker marker, String format, Object arg1, Object arg2)
{
info(format, arg1, arg2);
}

@Override
public void info(Marker marker, String format, Object... arguments)
{
info(format, arguments);
}

@Override
public void info(Marker marker, String msg, Throwable t)
{
info(msg, t);
}

@Override
public boolean isDebugEnabled()
{
Expand Down Expand Up @@ -309,6 +426,42 @@ public void trace(String msg, Throwable throwable)
}
}

@Override
public boolean isTraceEnabled(Marker marker)
{
return isTraceEnabled();
}

@Override
public void trace(Marker marker, String msg)
{
trace(msg);
}

@Override
public void trace(Marker marker, String format, Object arg)
{
trace(format, arg);
}

@Override
public void trace(Marker marker, String format, Object arg1, Object arg2)
{
trace(format, arg1, arg2);
}

@Override
public void trace(Marker marker, String format, Object... argArray)
{
trace(format, argArray);
}

@Override
public void trace(Marker marker, String msg, Throwable t)
{
trace(msg, t);
}

@Override
public void warn(String msg)
{
Expand Down Expand Up @@ -354,6 +507,59 @@ public void warn(String msg, Throwable throwable)
}
}

@Override
public boolean isWarnEnabled(Marker marker)
{
return isWarnEnabled();
}

@Override
public void warn(Marker marker, String msg)
{
warn(msg);
}

@Override
public void warn(Marker marker, String format, Object arg)
{
warn(format, arg);
}

@Override
public void warn(Marker marker, String format, Object arg1, Object arg2)
{
warn(format, arg1, arg2);
}

@Override
public void warn(Marker marker, String format, Object... arguments)
{
warn(format, arguments);
}

@Override
public void warn(Marker marker, String msg, Throwable t)
{
warn(msg, t);
}

public static Level intToLevel(int level)
{
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
}

public static String levelToString(int level)
{
if (level >= JettyLogger.OFF)
Expand All @@ -371,6 +577,7 @@ public static String levelToString(int level)
return "OFF"; // everything else
}

@SuppressWarnings("StringBufferReplaceableByString")
@Override
public String toString()
{
Expand Down Expand Up @@ -416,6 +623,13 @@ private JettyLoggingEvent asEvent(Level level, String format, Object arg1, Objec
}
}

private JettyLoggingEvent asEvent(Level level, String format, Throwable cause, Object... args)
{
String threadName = Thread.currentThread().getName();
long timestamp = System.currentTimeMillis();
return new JettyLoggingEvent(this, level, threadName, timestamp, format, cause, args);
}

private JettyLoggingEvent asEvent(Level level, String format, Object... args)
{
String threadName = Thread.currentThread().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public JettyLogger getRootLogger()

private JettyLogger createLogger(String name)
{
// TODO: if SOURCE property is configured, return a org.slf4j.spi.LocationAwareLogger wrapper?
// or is that handled by slf4j itself?
JettyLogger jettyLogger = new JettyLogger(name);
jettyLogger.setLevel(this.configuration.getLevel(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

package org.eclipse.jetty.logging;

import java.util.List;

import org.slf4j.Marker;
import org.slf4j.event.KeyValuePair;
import org.slf4j.event.Level;
import org.slf4j.event.LoggingEvent;

Expand Down Expand Up @@ -63,12 +66,6 @@ public Level getLevel()
return this.level;
}

@Override
public Marker getMarker()
{
return null;
}

@Override
public String getLoggerName()
{
Expand All @@ -81,6 +78,12 @@ public String getMessage()
return this.message;
}

@Override
public List<Object> getArguments()
{
return null;
}

@Override
public String getThreadName()
{
Expand All @@ -93,6 +96,18 @@ public Object[] getArgumentArray()
return this.argumentArray;
}

@Override
public List<Marker> getMarkers()
{
return null;
}

@Override
public List<KeyValuePair> getKeyValuePairs()
{
return null;
}

@Override
public long getTimeStamp()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testStdErrLogFormat()
}

@Test
@Disabled("Needs org.slf4j.spi.LocationAwareLogger impl")
@Disabled("Needs org.slf4j.spi.LocationAwareLogger impl to test")
public void testPrintSource()
{
Properties props = new Properties();
Expand All @@ -67,6 +67,6 @@ public void testPrintSource()
appender.emit(event);

System.err.println(output);
output.assertContains(".StdErrAppenderTest#testPrintSource(StdErrAppenderTest.java:");
output.assertContains(".StdErrxAppenderTest#testPrintSource(StdErrAppenderTest.java:");
}
}
2 changes: 1 addition & 1 deletion jetty-util/src/main/config/modules/log4j2-api.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ Log4j is released under the Apache 2.0 license.
http://www.apache.org/licenses/LICENSE-2.0.html

[ini]
log4j2.version?=2.11.2
log4j2.version?=2.0-alpha2
disruptor.version=3.4.2
jetty.webapp.addServerClasses+=,${jetty.base.uri}/lib/log4j2/
Loading

0 comments on commit 12706fd

Please sign in to comment.