简体   繁体   English

Log4j2自动配置

[英]Log4j2 auto config

I have a problem applying the log4j2.xml auto configuration properly, and I think it has something to do with my folder arrangement. 我在正确应用log4j2.xml自动配置时遇到问题,我认为它与我的文件夹排列有关。
I'm using maven to add log4j2 libs and arrange my projects as follows: 我正在使用maven添加log4j2库并按如下方式安排我的项目:
- 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 ) 两个项目都使用相同的通用包层次结构(如com.foo.specific.package

In the Common project I define a logger wrapper: 在Common项目中,我定义了一个logger包装器:

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

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

Moreover, the Common project contains the log4j2.xml file under META-INF (alongside the persistence.xml file for Hibernate usage). 此外,Common项目包含META-INF下的log4j2.xml文件(以及用于Hibernate用法的persistence.xml文件)。

<?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>

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. 在Core项目中运行示例代码(使用我编写的LogWrapper和一些JPA voodoo)时,我仍然可以看到INFO hibernate日志,并且没有创建日志文件。 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 " 我应该说,在调试代码时,我可以看到获取的记录器被赋予了一些奇怪的值“ com.foo.core.persistence.PersistenceXMLTest:ERROR in sun.misc.Launcher$AppClassLoader@2f600492

The log4j2.xml was placed in a "Folder" which in eclipse terms is "not on classpath". log4j2.xml放在一个“文件夹”中,在eclipse术语中“不在类路径上”。
Changing META-INF to be a "source folder" solved the problem. META-INF更改为“源文件夹”解决了这个问题。
In addition, the log4j2.xml file was not defined properly. 此外,未正确定义log4j2.xml文件。 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>

Still couldn't make the org.hibernate logger to be redirected to my logs, but at least I got the logger to work 仍然无法将org.hibernate记录器重定向到我的日志,但至少我让记录器工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM