英文:
Log4j2 Slf4j Issue - logger files not getting created on server startup
问题
ERROR StatusLogger 重新配置失败:在 null 中未找到“75b84c92”的配置
上述错误出现在服务器启动(netty)时。
我正在使用的依赖关系是 log4j-core-2.13.1
、log4j-api-2.13.1
、log4j-slf4j-impl-2.13.1
和 slf4j-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
答案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:
<?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>
The Lombok plugin makes your life easier. If you use the @Slf4j
annotation than you can use logger immediately: log.debug("say something")
.
Example code
@Slf4j
@Component
public class ClassA{
public void doSomething() {
log.debug("say something");
}
}
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
<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>
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 = "/", method = RequestMethod.GET)
public ModelAndView getWelcome() {
//logs debug message
if(logger.isDebugEnabled()){
logger.debug("getWelcome is executed!");
}
//logs exception
logger.error("This is Error message", new Exception("Testing"));
ModelAndView model = new ModelAndView("welcome");
model.addObject("msg", "Hello Spring MVC + Log4j");
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论