无法在使用Tomcat 9时使log4j写入日志文件。

huangapple go评论63阅读模式
英文:

Unable to get log4j to write to log files when using Tomcat 9

问题

我正在设置一个运行在Tomcat 9上的Web应用程序。

为了与其他地方编写的代码保持一致,我们希望继续在Web应用程序中使用log4j记录日志。

我在我的build.gradle中定义了以下内容:

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
runtimeOnly group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.13.3'

并且log4j-api-2.13.3.jarlog4j-core-2.13.3.jarlog4j-web-2.13.3.jar都位于我的web应用程序文件夹下的WEB-INF/libs文件夹中。

我的log4j配置位于WEB-INF/classes/log4j.properties中:

# 根日志选项
log4j.rootLogger=DEBUG, file

# 将日志消息直接记录到日志文件
log4j.appender.file=org.apache.log4j.RollingFileAppender

# 重定向到Tomcat日志文件夹
log4j.appender.file.File=${catalina.base}/logs/my-application.log

log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

在我的处理程序中,我创建了日志记录器并尝试记录一些信息,但是信息没有显示在任何地方。

@WebServlet(name = "MyServlet", urlPatterns = {"/test"}, loadOnStartup = 1)
public class MyServlet extends HttpServlet {
    private static final Logger logger = LogManager.getLogger(MyServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("Test");
        response.getWriter().print("Service is up and running!");
    }
}

localhost.2020-09-28.log日志文件中,我得到了一条消息,内容如下:

28-Sep-2020 11:38:18.391 FINE [main] org.apache.catalina.core.StandardContext.filterStart  Starting filter 'log4jServletFilter'

因此,我假设log4j已经被正确加载,至少在任何日志文件中我都没有找到任何错误。

我缺少哪些配置,以便能够使用log4j,并将其输出到自己的文件中?

英文:

I am setting up a web app running on Tomcat 9.

To match code written elsewhere, we want to keep using log4j for the code in the web application.

I have the following defined in my build.gradle

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
runtimeOnly group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.13.3'

And both log4j-api-2.13.3.jar, log4j-core-2.13.3.jar and log4j-web-2.13.3.jar are present in WEB-INF/libs under my webapp folder.

My log4j configuration is in WEB-INF/classes/log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

#Redirect to Tomcat logs folder
log4j.appender.file.File=${catalina.base}/logs/my-application.log

log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

In my handler I create the logger and tries to log some info, but it does not show up anywhere.


@WebServlet(name = "MyServlet", urlPatterns = {"/test"}, loadOnStartup = 1)
public class MyServletextends HttpServlet {
    private static final Logger logger = LogManager.getLogger(MyServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("Test");
        response.getWriter().print("Service is up and running!");
    }
}

In the localhost.2020-09-28.log logfile I get a message saying

28-Sep-2020 11:38:18.391 FINE [main] org.apache.catalina.core.StandardContext.filterStart  Starting filter 'log4jServletFilter'

So I assume that log4j is loaded correctly, at least I do not find any errors in any of the log files.

What configuration am I missing for being able to use log4j, and have it output to its own file?

答案1

得分: 1

我发现问题是我将属性设置得像是在使用log4j,但实际上我在使用log4j2。

我是通过在Tomcat服务启动前添加-Dlog4j.debug属性来启用log4j调试,从而发现了这个问题。

这显示它正在寻找类似于log4j2.properties的属性文件。

我还不得不更改属性文件的内容以便与log4j2一起使用。我使用了这个网站上的示例来帮助我创建一个新的属性文件。

英文:

I found out that the problem was that I was setting up the properties as if I was using log4j, but in reality I was using log4j2.

I found this by enabling log4j debug by adding -Dlog4j.debug property to the start of the Tomcat service.

This showed that it was looking for properties files with names similar to log4j2.properties.

I also had to change the content of my properties file to be usable with log4j2. I used the examples from this website to help me create a new properties file.

答案2

得分: 1

在我的情况下,项目有2个SLF4J实现,即"log4j"和"logback"。在应用部署时,catalina.out出现了这个消息:"SLF4J:类路径包含多个SLF4J绑定... "。通过在pom中进行依赖排除来移除了logback,之后文件被日志填充后被创建。

请查看此链接:https://www.baeldung.com/slf4j-classpath-multiple-bindings

英文:

In my case, project had 2 SLF4J implementations, "log4j" and "logback". On application deployment catalina.out had this message "SLF4J: Class path contains multiple SLF4J bindings....". Removed logback by dependency exclusion in pom and after that file was created and populated by logs.

See this link https://www.baeldung.com/slf4j-classpath-multiple-bindings

huangapple
  • 本文由 发表于 2020年9月28日 22:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64104085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定