使用log4j2.xml初始化slf4j [英] Initialize slf4j with log4j2.xml

查看:90
本文介绍了使用log4j2.xml初始化slf4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在log4j上使用slf4j.我在pom.xml中添加了以下依赖关系(对于slf4j使用了1.7.25,对于log4j2使用了2.10.0):

I want to use slf4j over log4j. I added the following dependencies in my pom.xml (I used 1.7.25 for slf4j and 2.10.0 for log4j2):

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>


    <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-slf4j-impl</artifactId>
       <version>${log4j.version}</version>
    </dependency>

    <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-api</artifactId>
       <version>${log4j.version}</version>
    </dependency>

    <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-core</artifactId>
       <version>${log4j.version}</version>
    </dependency>
</dependencies> 

一切正常,没有编译错误或缺少依赖关系,但是我无法在我的类中指定负责Logger初始化的配置(log4j2.xml)文件.在这种情况下,它始终会打印相同的警告

Everything builds just fine, no compilation errors or lack of dependencies, but I failed to specify the configuration (log4j2.xml) file in my class that is responsible for the initialization of the Logger. In this situation it always prints the same warning

log4j:WARN No appenders could be found for logger (com.mypackage.etc).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

我搜索了一种提供配置文件的正确方法,结果是这样:

I searched for a proper way to provide the configuration file and ended up with this:

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
context.setConfigLocation(file.toURI());

问题是在我的情况下,LogManager.getContext(false)将始终返回Slf4JLoggerContext的实例(考虑到我将slf4j用作记录器的外观),并且初始化将因ClassCastException而失败.我试图存储该Slf4JLoggerContext实例,但它不提供上下文的设置方法.同样,我也找不到从log4j检索LoggerContext的方法.

The problem is that in my case LogManager.getContext(false) will always return an instance of Slf4JLoggerContext (considering that I use slf4j as a facade for the logger) and the initialization will fail with ClassCastException. I tried to store that instance of Slf4JLoggerContext, but it doesn't offer a setter for the context. Also I failed to find a way to retrieve a LoggerContext from log4j.

是否可以通过任何方式将配置文件(log4j2.xml)提供给slf4j,以便查看所有附加程序和记录器?

Is there any way to provide the configuration file (log4j2.xml) to slf4j in order to see all the appenders and loggers?

更新考虑一下我的配置文件(我替换了原始的软件包和附加程序名称):

Update Consider this my configuration file (I replaced the original packages and appenders name):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Properties>
        <Property name="def.files.backup.count">10</Property>
        <Property name="log.file.path">${oo.home}/var/logs</Property>
        <Property name="def.file.max.size">10MB</Property>
        <Property name="log.level">WARN</Property>
    </Properties>

    <ThresholdFilter/>

    <Appenders>
        <RollingFile name="Appender1" fileName="${log.file.path}/file1.log" maxFileSize="${def.file.max.size}"
                     maxBackupIndex="${def.files.backup.count}">
            <PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
        </RollingFile>

        <RollingFile name="Appender2" fileName="${log.file.path}/file2.log"
                     maxFileSize="${def.file.max.size}"
                     maxBackupIndex="${def.files.backup.count}">
            <PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
        </RollingFile>

        <RollingFile name="Appender3" fileName="${log.file.path}/file3.log"
                     maxFileSize="${def.file.max.size}"
                     maxBackupIndex="${def.files.backup.count}">
            <PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
        </RollingFile>

    </Appenders>

    <Loggers>
        <AsyncLogger name="com.package1.oo" level="${log.level}" additivity="false">
            <AppenderRef ref="Appender1"/>
        </AsyncLogger>

        <AsyncLogger name="io.package2" level="${log.level}" additivity="false">
            <AppenderRef ref="Appender2"/>
        </AsyncLogger>

        <AsyncLogger name="com.package3.package3" level="${log.level}" additivity="false">
            <AppenderRef ref="Appender3"/>
        </AsyncLogger>

        <AsyncLogger name="org.package4" level="${log.level}">

        </AsyncLogger>

        <AsyncLogger name="com.package5.Class1" level="${log.level}">

        </AsyncLogger>

        <AsyncRoot level="${log.level}">
            <AppenderRef ref="Appender1"/>
            <AppenderRef ref="Appender2"/>
            <AppenderRef ref="Appender3"/>

        </AsyncRoot>
    </Loggers>

</Configuration>

推荐答案

我已经测试了此配置.log4j2文件必须位于您的类路径中.

I have tested this configuration. The log4j2 file has to be in your classpath.

Maven

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>

log4j2.xml

此外,添加xsd可以帮助您创建配置.我已经为Spring框架添加了Logger.请注意,需要使用jcl-over-slf4j来处理spring的内部日志记录

Also, adding xsd helps you with creating configuration. I have added Logger for spring framework. Note, that jcl-over-slf4j is needed to work with spring's internal logging

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error"
               xmlns="http://logging.apache.org/log4j/2.0/config"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config
                https://raw.githubusercontent.com/apache/logging-log4j2/master/log4j-core/src/main/resources/Log4j-config.xsd">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
        <Logger name="org.springframework" level="error">
            <AppenderRef ref="Console"/>
        </Logger>
    </Loggers>
</Configuration>

这篇关于使用log4j2.xml初始化slf4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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