简体   繁体   English

Log4j配置用于调试和测试

[英]Log4j configurations for debug and tests

I want to have two Log4j config files in my web-application. 我想在我的web应用程序中有两个Log4j配置文件。 The first one I placed here: conf/main/log4j.properties 我放在这里的第一个: conf / main / log4j.properties

The second is here: conf/test/resources/log4j-test.properties 第二个是: conf / test / resources / log4j-test.properties

so my pom contains the following: 所以我的pom包含以下内容:

<build>....
    <resources>
        <resource>
            <directory>conf/main</directory>
            <filtering>true</filtering>
            <includes>
                <include>*.properties</include>
            </includes>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>conf/test/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>*.properties</include>
            </includes>
        </testResource>
    </testResources>
    .......
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.8.1</version>
            <configuration>
                <systemPropertyVariables>
                    <log4j.configuration>file:${project.build.testOutputDirectory}/log4j-test.properties</log4j.configuration>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        <plugin>
    .....

log4j.properties: log4j.properties:

log4j.rootLogger=INFO, stdout, mainFile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.mainFile=org.apache.log4j.RollingFileAppender
log4j.appender.mainFile.File=../logs/main.log
log4j.appender.mainFile.MaxFileSize=1MB
log4j.appender.mainFile.MaxBackupIndex=1
log4j.appender.mainFile.layout=org.apache.log4j.PatternLayout
log4j.appender.mainFile.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

and log4j-test.properties: 和log4j-test.properties:

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

My goal - to write logs from tests only to my console. 我的目标 - 将测试日志仅写入我的控制台。 For example I have a simple test method: 例如,我有一个简单的测试方法:

@Test(dataProvider = "invalidCredentials", threadPoolSize = 10, invocationCount = 1, invocationTimeOut = 1000)
public void testGetUserByLoginInvalid(String login, String password) {
    LDAPUser user = LdapUtils.getUserByLogin(login, password);
    assertNull(user);
} 

Class LdapUtils has logger definition: 类LdapUtils具有记录器定义:

private static final Logger logger = Logger.getLogger(LdapUtils.class);

and it can write some info-message: 它可以写一些信息消息:

public static LDAPUser getUserByLogin(String login, String password) {
    ....
    if (user == null) {
        logger.info(INVALID_USER + login);
        return null;
    }
    .....
}

So the problem is: when I execute Maven install(or simply - test) it works fine because it reads log4j config from right place (conf/test/resources/log4j-test.properties) and writes nothing to file, only to console. 所以问题是:当我执行Maven安装(或简单地 - 测试)时,它工作正常,因为它从正确的位置读取log4j配置(conf / test / resources / log4j-test.properties)并且不向文件写入任何内容,仅向控制台写入。

But when I'm trying to run this test method in IntellijIDEA (righ click on testGetUserByLoginInvalid method and choose Run "testGetUserByLoginInvalid()" ) it fails with 但是当我试图在IntellijIDEA中运行这个测试方法时(右键单击testGetUserByLoginInvalid方法并选择Run“testGetUserByLoginInvalid()”)它失败了

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: ..\logs\main.log (The system cannot find the path specified)

In this case it reads wrong config(log4j.properties instead of log4j-test.properties) and fails when trying to find ..\\logs\\main.log file 在这种情况下,它会读取错误的配置(log4j.properties而不是log4j-test.properties),并在尝试查找.. \\ logs \\ main.log文件时失败

log4j.appender.mainFile.File=../logs/main.log log4j.appender.mainFile.File = .. /日志/ main.log

in case of web-application it seems to be just the same as 在网络应用的情况下,它似乎是相同的

log4j.appender.mainFile.File=${catalina.home}/logs/main.log log4j.appender.mainFile.File = $ {}的catalina.home /logs/main.log

But tests do not have to know something about ${catalina.home} at all, isn't it? 但测试根本不需要了解$ {catalina.home},不是吗? Because they are not part of .war. 因为他们不是.war的一部分。 So my idea was to create its own log4j config for tests but I can't avoid this problem with reading wrong config when running test not with Maven but with IDEA as I've described above... 所以我的想法是为测试创建自己的log4j配置,但我不能避免这个问题,因为在运行测试时没有使用Maven而是使用IDEA,如上所述......

Any idea's anyone? 有什么想法吗? Or may be there's another way to do what I want?... Thank you very much in advance! 或者可能有另一种方式来做我想要的事情?...非常感谢你提前!

I resolve issue with ${catalina.home} by: 我通过以下方式解决了$ {catalina.home}的问题:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <systemProperties>
                    <property>
                        <name>catalina.home</name>
                        <value>${project.build.directory}</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>

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

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