gpt4 book ai didi

java - Log4j 2 : How to add references from my classes in my log4j. 属性

转载 作者:行者123 更新时间:2023-12-01 09:57:25 26 4
gpt4 key购买 nike

这是我的第一个问题,如果我问错了,抱歉。

这是我对log4j2的疑问。(我使用spring框架java)

我的目标是将具有 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 接口(interface)的两个类。

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进行分离,但也没有成功。

最佳答案

换行

logger.rolling.name = fnel.bcp.org.main.TestMain

logger.rolling.name = fnel.bcp.org.main

这将导致 fnel.bcp.org.main 包中的任何类被定向到 RollingFile 附加程序。或者,如果您想要文件中的所有内容,您只需删除记录器定义并将根记录器更改为定向到 RollingFile 而不是 STDOUT。

关于java - Log4j 2 : How to add references from my classes in my log4j. 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37082863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com