日志记录包装器 - log4j

发布于 2024-09-08 05:01:00 字数 294 浏览 1 评论 0 原文

我需要做一个包装器接口,以便您可以在不同类型的日志记录机制之间切换而无需更改代码。所以我在包装类中有自己的 info(String message) 方法,例如:

public void info( Object message ) { if ( isInfoEnabled() ) { logger.info( 消息 ); } 它

按预期工作,只是在记录时它不显示我正在记录的位置的类和行,而是显示来自 Wrapper 类的类和行。 你能帮忙吗...

I have a requirement to do a wrapper interface so that you can switch between different kind of logging mechanisms without changing the code. So I have my own info(String message) method inside a Wrapper class, like:

public void info( Object message )
{
if ( isInfoEnabled() )
{
logger.info( message );
}
}

It works as expected except that when logging it does not show the class and line of the from where I am logging, but those from the Wrapper class.
Can you please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜雨飘雪 2024-09-15 05:01:09

这就是包装 log4j 的方式。您向 log() 传递包装类的名称。

class YourLog4jWrapper {

  Logger _base = Logger.getLogger(/*....*/);

  private void log(Object msg) {
    _base.log(YourLog4jWrapper.class.getName(), Priority.INFO, msg, null);
  }

}

This is how you wrap log4j. You pass log() the name of your wrapper class.

class YourLog4jWrapper {

  Logger _base = Logger.getLogger(/*....*/);

  private void log(Object msg) {
    _base.log(YourLog4jWrapper.class.getName(), Priority.INFO, msg, null);
  }

}
云胡 2024-09-15 05:01:07

因此,为了解决我的具体要求,应该这样做...

public class Log4jWrapper implements ILogger

{
私人记录器记录器;

/**
 * The name of this wrapper class
 */
static final String FQCN = Log4jWrapper.class.getName();

/**
 * Constructor which returns the root content
 */
public Log4jWrapper()
{
    logger = Logger.getRootLogger();
}

/**
 * Creates a {@link Logger} object
 * 
 * @param clazz name reference for this category to create
 */
public Log4jWrapper( Class<?> clazz )
{
    logger = Logger.getLogger( clazz );
}

/**
 * Log an debug message
 * 
 * @param msg the message
 */
public void debug( Object msg )
{
    if ( isDebugEnabled() )
    {
        logger.log( FQCN, Level.DEBUG, msg, null );
    }
}

So, to solve my specific requirement, here is it what should be done...

public class Log4jWrapper implements ILogger

{
private Logger logger;

/**
 * The name of this wrapper class
 */
static final String FQCN = Log4jWrapper.class.getName();

/**
 * Constructor which returns the root content
 */
public Log4jWrapper()
{
    logger = Logger.getRootLogger();
}

/**
 * Creates a {@link Logger} object
 * 
 * @param clazz name reference for this category to create
 */
public Log4jWrapper( Class<?> clazz )
{
    logger = Logger.getLogger( clazz );
}

/**
 * Log an debug message
 * 
 * @param msg the message
 */
public void debug( Object msg )
{
    if ( isDebugEnabled() )
    {
        logger.log( FQCN, Level.DEBUG, msg, null );
    }
}
煮茶煮酒煮时光 2024-09-15 05:01:06

已经有一个围绕各种日志记录实现的包装器接口:Apache Commons Logging。你尝试过用它来代替吗?

There's already a wrapper interface around various logging implementations: Apache Commons Logging. Have you tried using that instead?

暗藏城府 2024-09-15 05:01:04

我的建议是使用 Simple Logging Facade for Java (SLF4J) 而不是您自己的包装器。从其网站:

Simple Logging Facade for Java 或 (SLF4J) 用作各种日志框架(例如 java.util.logging、log4j 和 logback)的简单外观或抽象,允许最终用户在部署时插入所需的日志框架。

这是 Jakarta Commons Logging (JCL) 的更现代且首选的1替代方案。我建议阅读 这篇文章 进行详细分析,但简短的版本是 SLF4J 不会遇到使用 JCL 观察到的类加载器问题或内存泄漏问题。 Commons Logging 的作者本人承认了这一点

如果您想知道,SLF4J 是由 Log4J 的作者 Ceki Gülcü 编写的(现在致力于 Logback,Log4J 的后继者)。因此,这个人对日志记录略知一二,并且“他的”包装器有可能比我的更好。

换句话说,恕我直言,当一个好的包装器已经存在时,在 2010 年创建一个包装器是没有意义的。

1 请不要使用 Spring 作为反例,Spring 是一个不好的反例(请参阅 SPR-5327)。

My suggestion would be to use Simple Logging Facade for Java (SLF4J) instead of your own wrapper. From its website:

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

This is a moderner and preferred1 alternative to Jakarta Commons Logging (JCL). I recommend to read this article for a detailed analysis but the short version is that SLF4J doesn't suffer from the class loader problems or memory leaks observed with JCL. The author of Commons Logging himself admits it.

In case you wonder, SLF4J has been written by Ceki Gülcü which is the author of Log4J (and now works on Logback, the successor of Log4J). So this guy know a thing or two about logging and there are some chances "his" wrapper is better than mine.

In other words, there is IMHO just no point at creating a wrapper in 2010 when a nice and good one already exists.

1 Please, don't use Spring as counter example, Spring is a bad counter example (see SPR-5327).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文