用于一致日志记录的 log4j 结构
log4j structuring for consistent logging
我正在尝试为 MyTest
class 实现 log4j 日志记录。代码的包结构如使用下面的代码所述。目前我的 log4j.properties 放在 trial
包的资源下。
我有时会收到日志,但有时调试器会显示 "log4j:WARN No appenders could be found for logger." 所以我最终得到:
- 我应该把 log4j.properties 放在哪里才能在 MyTest 中获得一致的日志?
- 同一个项目可以在不同的包中有多个log4j.properties吗?
- debugger/compiler 的 log4j 属性如何与 class 调用关联?
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();
}
}
- Where should I place the log4j.properties to get the logs consistently in MyTest?
一般只要把一个log4j.xml文件放到src/main/resources
和src/test/resources
里面,让log4j自己找:不需要代码,默认的log4j初始化会把它捡起来。
2.Can the same project have more than one log4j.properties in different packages?
我不会那样做,因为类加载器只接受找到的第一个。您可以在单个 属性 文件中为不同的记录器设置多个配置。
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
编辑 1:
could you help me with the info on when and how is the
log4j.properties moved to target/classes [propertyconfigurator looks
inside it]
log4j.property 文件会在您构建项目时自动移动。但是文件应该在 src/main/resource/
以便自动移动。
如果是这种情况,您不需要以编程方式配置 属性 配置器。 Log4j可直接使用
我正在尝试为 MyTest
class 实现 log4j 日志记录。代码的包结构如使用下面的代码所述。目前我的 log4j.properties 放在 trial
包的资源下。
我有时会收到日志,但有时调试器会显示 "log4j:WARN No appenders could be found for logger." 所以我最终得到:
- 我应该把 log4j.properties 放在哪里才能在 MyTest 中获得一致的日志?
- 同一个项目可以在不同的包中有多个log4j.properties吗?
- debugger/compiler 的 log4j 属性如何与 class 调用关联?
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();
}
}
- Where should I place the log4j.properties to get the logs consistently in MyTest?
一般只要把一个log4j.xml文件放到src/main/resources
和src/test/resources
里面,让log4j自己找:不需要代码,默认的log4j初始化会把它捡起来。
2.Can the same project have more than one log4j.properties in different packages?
我不会那样做,因为类加载器只接受找到的第一个。您可以在单个 属性 文件中为不同的记录器设置多个配置。
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
编辑 1:
could you help me with the info on when and how is the log4j.properties moved to target/classes [propertyconfigurator looks inside it]
log4j.property 文件会在您构建项目时自动移动。但是文件应该在 src/main/resource/
以便自动移动。
如果是这种情况,您不需要以编程方式配置 属性 配置器。 Log4j可直接使用