Log4j2自动配置 [英] Log4j2 auto config

查看:391
本文介绍了Log4j2自动配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正确应用 log4j2.xml 自动配置时遇到问题,我认为这与我的文件夹排列有关.
我正在使用 maven 添加log4j2库并按如下方式安排我的项目:
-一个包含所有公共"类的项目,供我系统的服务器和客户端使用.
-另一个核心"项目-服务器端应用程序.
两个项目都使用相同的常规包层次结构(例如 com.foo.specific.package )

I have a problem applying the log4j2.xml auto configuration properly, and I think it has something to do with my folder arrangement.
I'm using maven to add log4j2 libs and arrange my projects as follows:
- one project to contain all "common" classes, used by server and client side of my system.
- another "core" project - the server side application.
Both projects use the same general package hierarchy (like com.foo.specific.package)

在Common项目中,我定义了一个记录器包装器:

In the Common project I define a logger wrapper:

public class LogWrapper
{
    static Logger systemParentLogger = LogManager.getLogger("com.foo");

    public static Logger getLogger(Class<?> cls)
    {
        return LogManager.getLogger(cls.getName());
    }
}

此外,Common项目包含 META-INF 下的 log4j2.xml 文件(用于Hibernate的persistence.xml文件).

Moreover, the Common project contains the log4j2.xml file under META-INF (alongside the persistence.xml file for Hibernate usage).

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">

    <appenders>
        <appender type="RollingFile" 
            name="MyFileAppender" 
            fileName="logs/app.log" 
            filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n"/>
        </appender>
    </appenders>

    <loggers>
        <root level="error">
            <appender-ref ref="MyFileAppender"/>
        </root>

        <logger name="com.foo" level="info" additivity="false">
            <appender-ref ref="MyFileAppender"/>
        </logger>

        <logger name="org.hibernate" level="error">
            <appender-ref ref=MyFileAppender"/>
        </logger>
    </loggers>

</configuration>

在Core项目中运行示例代码时(使用我编写的LogWrapper和一些JPA伏都教),我仍然可以看到INFO休眠日志,并且未创建任何日志文件.我应该声明,在调试代码时,我可以看到读取的记录器被赋予了一些怪异的值" com.foo.core.persistence.PersistenceXMLTest:ERROR.sun.misc.Launcher$AppClassLoader@2f600492 "

While running a sample code in the Core project (using the LogWrapper I wrote and some JPA voodoo), I could still see INFO hibernate logs, and no log file was created. I should state that while debugging the code, I could see that the logger fetched was given some weird value "com.foo.core.persistence.PersistenceXMLTest:ERROR in sun.misc.Launcher$AppClassLoader@2f600492"

推荐答案

log4j2.xml 放在文件夹"中,在日文中它是不在类路径上".
META-INF 更改为源文件夹" 解决了该问题.
此外, log4j2.xml 文件未正确定义. 这些是所需的修改:

The log4j2.xml was placed in a "Folder" which in eclipse terms is "not on classpath".
Changing META-INF to be a "source folder" solved the problem.
In addition, the log4j2.xml file was not defined properly. These are the modifications needed:

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="PRODUCTION" status="OFF">

<appenders>
    <RollingFile name="MyFileAppender" 
        fileName="../Logs/app.log" 
        filePattern="../Logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout>
            <pattern>%d %p %C{1.} [%t] %m%n</pattern>
        </PatternLayout>
        <Policies>
            <OnStartupTriggeringPolicy />
            <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
            <SizeBasedTriggeringPolicy size="250 MB"/>
        </Policies>
    </RollingFile>
</appenders>

<loggers>
    <root level="error">
        <appender-ref ref="MyFileAppender"/>
    </root>

    <logger name="com.foo" level="info" additivity="false">
        <appender-ref ref="MyFileAppender"/>
    </logger>
</loggers>

</configuration>

仍然无法将 org.hibernate 记录器重定向到我的日志,但是至少我让记录器可以工作

Still couldn't make the org.hibernate logger to be redirected to my logs, but at least I got the logger to work

这篇关于Log4j2自动配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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