gpt4 book ai didi

logging - Log4j2 文件已创建但未写入日志

转载 作者:行者123 更新时间:2023-12-02 21:07:46 25 4
gpt4 key购买 nike

我在springboot中使用了log4j2,日志文件已创建,但日志未写入文件中。

log4j2.properties

name=PropertiesConfig
property.filename = /export/home/apps/logs
appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/app-frontend.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

loggers=file
logger.file.name=guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>

使用记录器的方法

private static Logger logger = LogManager.getLogger();

@RequestMapping(value="/check", method = RequestMethod.GET)
public String healthCheck() {

logger.debug("Health-check invoked");

return "Hey, I am fine";
}

上面我提到了我使用过的代码。还是找不到解决的办法。日志甚至没有出现在控制台中。

最佳答案

在 spring-boot 中使用“log4j2.properties”进行配置有一定的限制。 Log4J 2 最初发布时不支持通过属性文件进行配置。从 Log4J 2.4 开始,再次添加了对属性文件的支持,但语法完全不同。如 the documentation 中所述。

As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1.

As of version 2.6, this list of identifiers is no longer required as names are inferred upon first usage, however if you wish to use more complex identifies you must still use the list. If the list is present it will be used.

从 spring boot 版本 1.4.0 开始,使用的 log4j2 api 版本是 2.6.2。请注意,spring-boot 使用 slf4j api 来支持多个底层日志框架。在为 log4j2 使用基于属性的配置而不考虑 slf4j 绑定(bind)的类路径依赖项时,似乎存在问题。

使用基于 XML(或 yaml/json)的配置来实现相同的目的并启用 log4j2 能够使用的所有功能是有意义的。

这是相同属性的基于 xml 的配置。

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
<Properties>
<Property name="filename">export/home/apps/logs</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="LOGFILE"
fileName="${filename}/app-frontend.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
</Appenders>
<Loggers>

<Logger name="guru.springframework.blog.log4j2properties" level="debug">
<AppenderRef ref="LOGFILE"
level="debug" />
</Logger>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>

关于logging - Log4j2 文件已创建但未写入日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39404461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com