在jfinal_demo中用slf4j+log4j2在jetty中出错:
pom.xml
<!-- log相关 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.11.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.1</version>
<scope>runtime</scope>
</dependency>
<!--需要使用log4j2的AsyncLogger需要包含disruptor -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
<scope>runtime</scope>
</dependency>
BlogController.java
public class BlogController extends Controller {
private static final Logger logger = LoggerFactory.getLogger(BlogController.class);
@Inject
BlogService service;
public void index() {
setAttr("blogPage", service.paginate(getParaToInt(0, 1), 10));
logger.trace("log4j2 Demo");
logger.debug("This is log of level of debug");
logger.info("[GET] /user/all getAllUsers");
logger.warn("this is warn");
logger.error("哎呀,出错啦~");
System.out.println("111111");
Log.getLog(this.getClass()).debug("hello world");
报错:
HTTP ERROR 500
Problem accessing /blog. Reason:
Server Error
Caused by:
java.lang.IncompatibleClassChangeError: Class org.apache.logging.slf4j.Log4jLoggerFactory does not implement the requested interface org.slf4j.ILoggerFactory at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383) at com.demo.blog.BlogController.<clinit>(BlogController.java:22) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at java.lang.Class.newInstance(Class.java:442) at com.jfinal.core.ControllerFactory.getController(ControllerFactory.java:24) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:75) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:86) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1634) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1340) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1242) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) at org.eclipse.jetty.server.Server.handle(Server.java:503) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103) at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683) at java.lang.Thread.run(Thread.java:745)
Powered by Jetty:// 9.4.12.v20180830
1:git clone git@gitee.com:jfinal/jetty-server.git
2: cd jetty-server
3: mvn install
安装成功以后,将项目中的 pom.xml 中对 jetty-server-2018.11 的依赖改下版本号,原来的版本号 2018.11 改成 2018.12-SNAPSHOT
记得解决后回来再回复一下
这个问题的原因是 JettyServer.java 中的 webApp.getSystemClasspathPattern().add("org.apache.") 这行代码将 所有 org.apache. 前缀的类配置成了 “无热加载”,而 org.apache.logging.slf4j.Log4jLoggerFactory 这个在这种情况下不能工作
目前最新的 jetty-server 代码,已经处理过了 "org.apache." 前缀