我想知道如何获取自定义记录器 通过log4j的LogManger.getLogger()方法。
我几乎可以肯定我的项目不需要自定义记录器并使用默认的log4j记录器。
但是,如果有必要,我想用自定义记录器更改源代码中的所有记录器。
我可以只更改设置文件或LoggerFactory源代码吗?
如果这篇文章重复,请点击链接引导我。
感谢。
答案 0 :(得分:1)
请查看https://issues.apache.org/jira/browse/LOG4J2-519
请注意,附加到该故障单的代码生成器是实验性的,使用此工具生成的自定义/扩展记录器可能会在将来的log4j-2.0版本中中断。
答案 1 :(得分:1)
建议的方法是修改配置文件并将watch标志设置为true。如果你想推进前进的源代码方法,请参阅下文。
这是一个非常复杂的过程,包括转换为log4j.core.logger类型以公开Logger接口隐藏的受保护方法。我还应该给出免责声明,这涉及core.logger中受保护的方法,这些方法随时都可能发生变化。
幸运的是,我刚刚解决了这个问题,并提供了示例代码。
/**
* Dynamically creates an instance of a Logger.
*
* @param appenderName
* most often will be the name of the class this logger will
* belong to.
* @param logFileName
* the file name to save the logs to
* @return
*/
public static org.apache.logging.log4j.Logger createInstanceLogger(String appenderName,
String logFileName) {
//call getLogger() which will create and load a logger with a default configuration
org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager
.getLogger(String.format("DynamicLogger%s",
String.valueOf(DynamicLogManager.loggerCount++)));
org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) logger; // cast logger to core logger to access protected methods
org.apache.logging.log4j.core.LoggerContext context = coreLogger.getContext();
org.apache.logging.log4j.core.config.BaseConfiguration configuration = (org.apache.logging.log4j.core.config.BaseConfiguration) context
.getConfiguration();
TriggeringPolicy compositePolicy = CompositeTriggeringPolicy.createPolicy(
SizeBasedTriggeringPolicy.createPolicy("5mb"),
TimeBasedTriggeringPolicy.createPolicy("1", "true"));
String fileName = String.format("%1$s/%2$s", appenderName, logFileName);
String filePattern = String.format("%1$s/%2$s.%%d{yyyy-MM-dd}",
appenderName, logFileName);
Layout<String> layout = PatternLayout.createLayout(DynamicLogManager._conversionPattern,
configuration, RegexReplacement.createRegexReplacement("a^", "."), //empty regex replacement
"utf-8", "false");
Filter f = ThresholdFilter.createFilter("trace", "accept", "accept");
Appender a = RollingFileAppender.createAppender(fileName, //fileName
filePattern, //filePattern
"true", //append
appenderName, //appender name
"true", //bufferred io
"true", //immediate flush
compositePolicy, //policy
null, //strategy default
layout, //layout
f, //filter none
"true", //ignore exceptions
"false", //advertise
"null", //advertise uri
configuration //configuration
);
a.start();
coreLogger.addAppender(a);
//this is where we would figure out how to configure the logger to listen on a configuration file
coreLogger.setLevel(Level.ALL); //set the log level of the logger that we are returning to Level.ALL
return coreLogger; //cast logger back to Logger interface
按照您的评论进行修改:
看起来你只想将你的记录器添加到你的xml配置中并从java代码中获取它,这很容易做到......
<Loggers>
<Logger name="com.foo.Bar" level="trace" additivity="false">
<AppenderRef ref="whateverAppenderName"/>
</Logger>
<Root level="error">
<AppenderRef ref="whateverOtherAppenderName"/>
</Root>
</Loggers>
然后在java代码中:
package com.foo;
class Bar{
private static final Logger log = LogManager.getLogger(); //qualified class name is com.foo.Bar
}
登录log4j也是分层的。这意味着如果您定义一个名为“com.foo”的记录器,它将被该包的所有子项抓取,如果它是最可访问的记录器(意味着您没有该特定类的更具体的记录器名称) )
阅读本文... http://logging.apache.org/log4j/2.x/manual/configuration.html#XML