2015-11-25 627 views
0

我想实现MyTest类的log4j日志记录。代码的包结构如下所述使用代码。目前我的log4j.properties被放在trial包的资源下。log4j构建一致的日志记录

enter image description here

我有时真的得到了日志,但在时间调试器显示“的log4j:警告没有附加目的地可以找到记录。”所以我最终得到:

  1. 我应该在哪里放置log4j.properties在MyTest中一致地获取日志?
  2. 相同的项目可以在不同的包中包含多个log4j.properties吗?
  3. 调试器/编译器与调用它的类关联的log4j属性如何?

BasicPage.class

package com.trial.pages; 
    public class BasicPage{ 
     protected static final Logger pageLogger = Logger.getLogger(BasicPage.class); 
     <some code goes here> 
    } 

FirstPage.class

package com.trial.pages.mobile; 
import package com.trial.pages; 
public class FirstPage extends BasicPage { 
    public void pageMethod() { 
     pageLogger.info("These are logs from the Pages."); 
    } 
} 

BasicTest.class

package com.core.data; 
public class BasicTest{ 
    protected static final Logger testLogger = Logger.getLogger(BasicTest.class); 
    <some code goes here> 
} 

MyTest.class

package com.trial.tests.mobile; 
import com.trial.pages.mobile.FirstPage; 
import com.core.data.BasicTest; 
public class MyTest extends BasicTest{ 
    public void someMethod(){ 
     testLogger.info("These are the logs from the test activities."); 
     new FirstPage.pageMethod(); 
    } 
} 
+0

你想要一个或多个日志文件吗? – Patrick

+0

宁愿使用单个文件 – nullpointer

回答

1
  1. 我应该在哪里放置log4j.prope在MyTest中始终如一地获取日志?

一般只是把文件的log4j.xml到src/main/resourcessrc/test/resources让log4j的本身找到它:无需任何代码,默认的log4j初始化将它捡起来。

2.同一个项目可以在不同的包中包含多个log4j.properties吗?

我不会做,因为类加载器只需要被发现的第一个。您可以在单个属性文件中为不同的记录器提供多种配置。

log4j-properties-file-multiple-loggers-in-same-class

how-can-i-create-2-separate-log-files-with-one-log4j-config-file

log4j-multiple-loggers-levels-and-appenders


EDIT1:

你能帮助我的信息何时和如何是 log4j.pr operties移动到目标/班[propertyconfigurator看起来 里面]

,当你建立你的项目的log4j.property文件将会被自动移动。但该文件应该在src/main/resource/中进行自动移动。

如果是这种情况,您不需要以编程方式配置属性配置器。 Log4j可以直接使用。

+0

log4j.properties已经在'trial'包的资源文件夹中,在执行过程中仍然存在不一致的情况,即该文件被找到,而不是。 – nullpointer

+0

@nullpointer为什么它应该是一个包?对于整个应用程序的全局访问,只需将其放入src/main/resources文件夹即可。还是Iam错了? – Patrick

+0

也试过这样做..你能帮我了解什么时候以及如何将log4j.properties移动到目标/类[propertyconfigurator看起来里面] – nullpointer