是我的第一个问题,抱歉,如果我在提问时提出错误。
这是我对log4j2的疑问。(我使用spring framework java)
我的目标是在FILE中写入在具有Logger实例的类中生成的所有日志。
为此,我有以下log4j.properties文件:
name=PropertiesConfig
property.filename = C:/sts-bundle/Tomcat 7.0/logs
appenders = console,rolling
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{dd MMM yyyy HH:mm:ss,SSS} [%t] %c{5} - %msg%n
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}/fnelLog-${date:dd-MM-yyyy}.log
appender.rolling.filePattern = ${filename}/fnelLog-%d{dd-MM-yyyy}-%i.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%-5level] %d{dd MMM yyyy HH:mm:ss,SSS} [%t] %c{5} - %msg%n
appender.rolling.policies.type = Policies
#appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
#appender.rolling.policies.time.interval = 2
#appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=50MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 4
loggers = rolling
############ HERE IS MY PROBLEM, I Only reference to this class, but I want to add another,
############ so I can write all logs in a file described in lines before
logger.rolling.name = fnel.bcp.org.main.TestMain
logger.rolling.level = debug
#logger.rolling.additivity = false
logger.rolling.appenderRefs = rolling
logger.rolling.appenderRef.rolling.ref = RollingFile
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
现在这里是我使用Logger界面的两个部分。
public class FooClass {
private static Logger logger = LogManager.getLogger(FooClass.class);
public void testLog4j() {
// TODO Auto-generated method stub
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
}
}
public class TestMain {
private static Logger logger = LogManager.getLogger(TestMain.class);
public static void main(String[] args) {
FooClass foo = new FooClass();
//Calling foo method
foo.testLog4j();
// TODO Auto-generated method stub
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
}
}
这些是控制台中的输出
所以,我的问题是如何在log4j.properties文件中添加FooClass。因为我在TestMain类旁边添加了这样的“fnel.bcp.org.main.TestMain,fnel.bcp.org.main.FooClass”,但是没有用。
我尝试在不同的logger.rolling.name中分隔,但也没有用。
答案 0 :(得分:1)
更改行
logger.rolling.name = fnel.bcp.org.main.TestMain
到
logger.rolling.name = fnel.bcp.org.main
这将导致fnel.bcp.org.main包中的任何类被定向到RollingFile appender。或者,如果您想要文件中的所有内容,您只需删除记录器定义并将根记录器更改为指向RollingFile而不是STDOUT。