打印“来源”带有 log4j 包装器的日志语句中的类
我的应用程序有一个自制日志记录类,我正在将其迁移到幕后使用 log4j。但是,由于我使用自制类将应用程序的其余日志记录语句传递给 log4j,因此输出语句被记录为来自包装器类而不是源类。
除了为每个日志语句创建新的 org.apache.log4j.Logger 实例之外,还有其他方法可以确保显示“正确”的源吗?我也尝试过使用 Logger.log(String callerFQCN, Priority level, Object message, Throwable t) 方法,但它似乎不起作用,例如:
public class Logger2 {
public static org.apache.log4j.Logger log4JLogger = org.apache.log4j.Logger.getLogger(Logger2.class);
public static void warning(Object source, String message) {
log(source, message, Level.WARN, null)
}
private static void log(Object source, String message, Level level, Throwable t) {
String className = source.getClass().getName();
System.out.println("Logging class should be " + className);
log4JLogger.log(className, loggingLevel, message, t);
}
}
当调用时:
public void testWarning() {
Logger2.warning(new Integer(3), "This should warn");
}
打印:
Logging class should be java.lang.Integer
2010-05-25 10:49:57,152 WARN test.Logger2 - This should warn
My application has a homebrew logging class that I'm migrating to using log4j under the covers. However, since I'm using the homebrew class to pass the rest of the application's logging statements to log4j, the output statements are logged as coming from the wrapper class instead of the source class.
Is there a way to ensure that the "correct" source is being shown besides creating new org.apache.log4j.Logger instances for every log statement? I've also tried using the Logger.log(String callerFQCN, Priority level, Object message, Throwable t) method, but it doesnt seem to be working, for example:
public class Logger2 {
public static org.apache.log4j.Logger log4JLogger = org.apache.log4j.Logger.getLogger(Logger2.class);
public static void warning(Object source, String message) {
log(source, message, Level.WARN, null)
}
private static void log(Object source, String message, Level level, Throwable t) {
String className = source.getClass().getName();
System.out.println("Logging class should be " + className);
log4JLogger.log(className, loggingLevel, message, t);
}
}
When called by:
public void testWarning() {
Logger2.warning(new Integer(3), "This should warn");
}
Prints:
Logging class should be java.lang.Integer
2010-05-25 10:49:57,152 WARN test.Logger2 - This should warn
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自制的日志记录解决方案使用了 log4j 的 LocationInfo 类查找源代码信息。
通过此解决方案,
locationInfo
对象包含来自使用loggerName
调用我的记录器的对象的信息。这是我的记录器的简化版本,使用 log4j 进行记录:
顺便说一句: 级别,MDC和 Logger 类都是 log4j 类。
评论回复:
MDC 对象存储在
ThreadLocal
对象上,可供 log4j 记录器访问。来自 MDC Java 文档:
My home brewed logging solution used log4j's LocationInfo class to find the source code information.
With this solution, the
locationInfo
object contains information from the object that calls my logger with theloggerName
.Here's a simplified version of my logger that logs with log4j:
Btw: The Level, MDC and Logger class are all log4j classes.
Replies to comments:
The MDC object is stored on the
ThreadLocal
object and is accessible for the log4j logger.From the MDC Java documentation:
在 Log4J API 中,关于 Logger.log(String callerFQCN, Priority level, Object message, Throwable t):
参数:
请参阅:Log4J API。
我知道很奇怪,参数被称为“callerFQCN”,它似乎是调用者对象类,但(muajaja!)它实际上是“包装器”类(例如 MyClass.class.getName())。
实际上,我认为调用者是通过 Throwable 参数得到的。不确定,尚未检查实施情况。
当您将 Throwable 参数保留为空时,我认为下面一定会发生类似的事情:
如果您想创建一个 Logger 包装器,您需要执行如下操作:
作为包装器,这项工作没有问题。
或者,如果您绝对需要维护下面显示的界面,请执行以下操作:
希望这会有所帮助。
In Log4J API about Logger.log(String callerFQCN, Priority level, Object message, Throwable t):
Parameters:
See: Log4J API.
Quite weird I know, argument is called "callerFQCN" which seams to be the caller object class, BUT (muajaja!) it's actually the "wrapper" class (ex MyClass.class.getName()).
Actually, the caller is get by the Throwable argument I think. Not sure, haven't checked the implementation.
As you leave Throwable argument null, I think something like this must be happening below:
If you want to create a Logger wrapper you need to do something like this:
This work without problem as a wrapper.
Or, if you absolutely need to maintain the interface you show below, do something like this:
Hope this helps.
这应该可以做到:
This should do it: