Log4j2 Slf4j问题 – 日志文件在服务器启动时未被创建

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

Log4j2 Slf4j Issue - logger files not getting created on server startup

问题

ERROR StatusLogger 重新配置失败:在 null 中未找到“75b84c92”的配置

上述错误出现在服务器启动(netty)时。
我正在使用的依赖关系是 log4j-core-2.13.1log4j-api-2.13.1log4j-slf4j-impl-2.13.1slf4j-api-1.7.29

英文:
ERROR StatusLogger Reconfiguration failed: No configuration found for '75b84c92' at 'null' in 'null'

The above is the error I am getting on server startup(netty).
The dependencies I am using are log4j-core-2.13.1, log4j-api-2.13.1, log4j-slf4j-impl-2.13.1 and slf4j-api-1.7.29

Log4j2 Slf4j问题 – 日志文件在服务器启动时未被创建

答案1

得分: 1

日志配置有很多方面,但如果你使用Spring,对于日志你不需要做太多事情。

这是一个基本的 pom.xml 示例:

<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <name>...</name>
    <packaging>...</packaging>
    <version>...</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
    </dependencies>
</project>

使用Lombok插件可以让你的生活更加简单。如果你使用 @Slf4j 注解,你可以立即使用日志记录器:log.debug("say something")

示例代码

@Slf4j
@Component
public class ClassA {
    public void doSomething() {
        log.debug("say something");
    }
}

最后一件需要检查的是你的应用程序配置文件中的日志配置。你可以使用以下内容作为参考:

logging:
    level:
        root: INFO
        org.springframework: INFO
        com.a.b.c: DEBUG
英文:

Log configuration has many aspects, but if you use Spring you do not need to do too many things for logging.

This is a basic pom.xml what you can use:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project ...&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;...&lt;/groupId&gt;
    &lt;artifactId&gt;...&lt;/artifactId&gt;
    &lt;name&gt;...&lt;/name&gt;
    &lt;packaging&gt;...&lt;/packaging&gt;
    &lt;version&gt;...&lt;/version&gt;

    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.1.5.RELEASE&lt;/version&gt;
    &lt;/parent&gt;

    &lt;properties&gt;
        &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

The Lombok plugin makes your life easier. If you use the @Slf4j annotation than you can use logger immediately: log.debug(&quot;say something&quot;).

Example code

@Slf4j
@Component
public class ClassA{
    public void doSomething() {
        log.debug(&quot;say something&quot;);
    }
}

The last thing that you can check is the log configuration in your application config file. You can use the following as a reference:

logging:
    level:
        root: INFO
        org.springframework: INFO
        com.a.b.c: DEBUG

答案2

得分: 1

好的,以下是翻译好的内容:

pom.xml文件内容:

<properties>
	<spring.version>4.1.6.RELEASE</spring.version>
	<log4j.version>1.2.17</log4j.version>
</properties>

<dependencies>

	<!-- Spring -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring.version}</version>
	</dependency>

	<!-- Log4j -->
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>${log4j.version}</version>
	</dependency>

</dependencies>

log4j.properties文件内容:

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
# 输出到Tomcat主目录下的logs文件夹
log4j.appender.file.File=${catalina.home}/logs/myapp.log
log4j.appender.file.MaxFileSize=5MB
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

WelcomeController.java文件内容:

package com.kunal.common.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController {

	private static final Logger logger = Logger.getLogger(WelcomeController.class);

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView getWelcome() {

		// 记录调试信息
		if(logger.isDebugEnabled()){
			logger.debug("getWelcome is executed!");
		}

		// 记录异常
		logger.error("This is Error message", new Exception("Testing"));

		ModelAndView model = new ModelAndView("welcome");
		model.addObject("msg", "Hello Spring MVC + Log4j");
		return model;

	}

}

运行:

运行以下命令:

mvn jetty:run

然后在浏览器中访问URL:http://localhost:8080/spring-mvc-log4j/

所有的日志信息将显示在控制台中。

log4j.properties内容:

# 将日志消息重定向到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Jetty控制台输出:

2015-06-19 14:10:35 DEBUG WelcomeController:19 - getWelcome is executed!
2015-06-19 14:10:35 ERROR WelcomeController:23 - This is Error message
java.lang.Exception: Testing
        at com.kunal.common.controller.WelcomeController.getWelcome(WelcomeController.java:23)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
英文:

Lets do it. There is less info in question so this give opportunity to me to explain in detail. Lets start.

Your pom.xml should look like this least

&lt;properties&gt;
	&lt;spring.version&gt;4.1.6.RELEASE&lt;/spring.version&gt;
	&lt;log4j.version&gt;1.2.17&lt;/log4j.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;

	&lt;!-- Spring --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;!-- Log4j --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;log4j&lt;/groupId&gt;
		&lt;artifactId&gt;log4j&lt;/artifactId&gt;
		&lt;version&gt;${log4j.version}&lt;/version&gt;
	&lt;/dependency&gt;

&lt;/dependencies&gt;

Create a log4j.properties file, and put it in the resources. folder

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/myapp.log
log4j.appender.file.MaxFileSize=5MB
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

A simple controller to return a welcome page. Furthermore, it shows you how to use log4j to do the logging.

package com.kunal.common.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController {

	private static final Logger logger = Logger.getLogger(WelcomeController.class);

	@RequestMapping(value = &quot;/&quot;, method = RequestMethod.GET)
	public ModelAndView getWelcome() {

		//logs debug message
		if(logger.isDebugEnabled()){
			logger.debug(&quot;getWelcome is executed!&quot;);
		}

		//logs exception
		logger.error(&quot;This is Error message&quot;, new Exception(&quot;Testing&quot;));

		ModelAndView model = new ModelAndView(&quot;welcome&quot;);
		model.addObject(&quot;msg&quot;, &quot;Hello Spring MVC + Log4j&quot;);
		return model;

	}

}

Let's run

mvn jetty:run (or whichever server you use)

and run URL http://localhost:8080/spring-mvc-log4j/

All message will appear in console

> log4j.properties

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

> Jetty console

2015-06-19 14:10:35 DEBUG WelcomeController:19 - getWelcome is executed!
2015-06-19 14:10:35 ERROR WelcomeController:23 - This is Error message
java.lang.Exception: Testing
        at com.kunal.common.controller.WelcomeController.getWelcome(WelcomeController.java:23)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)

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

发表评论

匿名网友

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

确定