已创建Log4j2文件,但未写入日志 [英] Log4j2 file created but not log written

查看:251
本文介绍了已创建Log4j2文件,但未写入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used log4j2 with springboot, log file has been created but logs are not written in file.

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";
    }

上面我提到了我使用的代码.仍然找不到解决方法.日志甚至没有出现在控制台中.

Above I have mention the codes I have used. Still can not find a way to solve. Logs are even not appearing in the console.

推荐答案

在spring-boot中使用"log4j2.properties"进行配置有一定的局限性. Log4J 2最初发布时不支持通过属性文件进行配置.如文档

Usage of "log4j2.properties" for configuration in spring-boot has certain limitations. Log4J 2 did not support configuration through the properties file when it was initially released. It was from Log4J 2.4 that support for the properties file was again added, but with a completely different syntax.As mentioned in the documentation

从2.4版开始,Log4j现在支持通过属性文件进行配置.请注意,属性语法与Log4j 1中使用的语法不同.

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.

从2.6版开始,不再需要此标识符列表,因为会在首次使用时推断出名称,但是,如果您希望使用更复杂的标识符,则仍必须使用该列表.如果存在该列表,则将使用它.

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支持多个基础Logging Framework.将基于属性的配置用于log4j2时,似乎没有问题,而无法解决slf4j绑定的类路径依赖项.

As of spring boot release 1.4.0, the log4j2 api version used is 2.6.2. Note that spring-boot uses the slf4j api to support multiple underlying Logging Framework. There seems to be an issue when using properties based configuration for log4j2 without juggling with classpath dependencies for slf4j bindings.

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

It would make sense to use the XML (or yaml/json) based configuration to achieve the same and to enable the usage of all the features log4j2 is capable of.

这是具有相同属性的基于xml的配置.

Here is an xml based configuration for the same properties.

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>

这篇关于已创建Log4j2文件,但未写入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆