包装slf4j API [英] Wrapping the slf4j API

查看:109
本文介绍了包装slf4j API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Logback将slf4j改造成旧版应用程序.好消息是,旧版应用程序具有自己的日志记录框架.因此,我要做的就是更改日志记录框架以将日志记录到slf4j而不是log4j.

I want to retrofit slf4j with Logback into a legacy application. Good thing is, the legacy application has its own logging framework. So all I had to do is alter the logging framework to log to slf4j instead of log4j.

它像梦一样运作.我很高兴,直到我注意到每个日志事件记录的Logback位置:

It worked like a dream. I was happy, until I noticed the location Logback logged for each and every log event:

Logger.java:...

赞!当试图找出日志事件的来源时,这对我的开发人员没有多大帮助.

Yikes! That wasn't going to help my fellow developers much when trying to figure out where a log event came from.

如何告诉Logback在堆栈中向上查找几层以记录实际位置?

How can I tell Logback to look a few levels up in the stack for the actual location to log?

logger类是具有以下方法的实用程序类:

The logger class is a utility class with methods like this:

public static void debug(String clazz, String message) {
    org.slf4j.Logger logger = LoggerFactory.getLogger(clazz);
    logger.debug(message);
}

推荐答案

找到了查看jcl-over-slf4j来源的解决方案. slf4j的大多数实现(包括登录)都使用实现LocationAwareLogger的记录器,该记录器具有一个log方法,该方法期望包装记录器类的全限定类名称作为其参数之一:

Found the solution looking at the source of jcl-over-slf4j. Most implementations of slf4j (including logback) use loggers that implement LocationAwareLogger, which has a log method that expects the fully qualified class name of the wrapping logger class as one of it's arguments:

private static final String FQCN = Logger.class.getName();


public static void debug(String clazz, String message) {
    org.slf4j.Logger logger = LoggerFactory.getLogger(clazz);
    if (logger instanceof LocationAwareLogger) {
        ((LocationAwareLogger) logger).log(null, FQCN, LocationAwareLogger.DEBUG_INT, message, null, null);
    } else {
        logger.debug(message);
    }
}

这篇关于包装slf4j API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆