【问题标题】:How to delegate embedded tomcat logging to log4j如何将嵌入式 tomcat 日志记录委托给 log4j
【发布时间】:2021-12-27 18:37:54
【问题描述】:

我正在尝试禁用嵌入式 tomcat 的默认 java.util.logging。它可以工作,但由于某种原因,我仍然在标准日志中找到了一些消息。

之前:

2021-12-27 21:24:33 INFO   org.apache.coyote.AbstractProtocol init Initializing ProtocolHandler ["http-nio-8081"]
2021-12-27 21:24:33 INFO   org.apache.catalina.core.StandardService startInternal Starting service [Tomcat]
2021-12-27 21:24:33 INFO   org.apache.catalina.core.StandardEngine startInternal Starting Servlet engine: [Apache Tomcat/10.1.0-M7]
2021-12-27 21:24:33 INFO   org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment No global web.xml found
2021-12-27 21:24:35 INFO   org.apache.jasper.servlet.TldScanner scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2021-12-27 21:24:35 INFO   org.apache.coyote.AbstractProtocol start Starting ProtocolHandler ["http-nio-8081"]

之后:

2021-12-27 21:33:21 INFO   org.apache.coyote.AbstractProtocol init Initializing ProtocolHandler ["http-nio-8081"]
2021-12-27 21:33:23 INFO   org.apache.jasper.servlet.TldScanner scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2021-12-27 21:33:23 INFO   org.apache.coyote.AbstractProtocol start Starting ProtocolHandler ["http-nio-8081"]

我如何尝试委派日志记录:

import org.apache.logging.log4j.jul.Log4jBridgeHandler;

import java.util.logging.Level;

public class Application {
    public static void main(String[] args) throws Exception {
        System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n");

        fixLogger("org.apache.tomcat");
        fixLogger("org.apache.catalina");
        fixLogger("org.apache.jasper");
        fixLogger("org.apache.coyote");
        fixLogger("org.apache.juli");

        TomcatServer.INSTANCE.run();

    }

    private static void fixLogger(String name) {
        java.util.logging.Logger logger = java.util.logging.Logger.getLogger(name);
        logger.setLevel(Level.ALL);
        logger.setUseParentHandlers(false);
        logger.addHandler(new Log4jBridgeHandler(false, "", false));
    }
}

【问题讨论】:

    标签: java tomcat logging log4j log4j2


    【解决方案1】:

    解决了这个问题。我只需要在第一次初始化 LogManager 之前使用它:

    System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
    

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      相关资源
      最近更新 更多