我可以创造一个“光”吗?我自己的库中是否依赖第三方库(例如 log4j)?

发布于 2024-10-05 06:27:45 字数 1494 浏览 0 评论 0原文

我正在 http://jooq.sourceforge.net 上维护一个用于数据库访问的库,我想使用log4j 用于日志记录。但日志记录确实是我的库中非常可选的一部分。如果 log4j 在客户端代码中不可用,那么我不会进行日志记录。所以我创建了一个像这样的记录器代理:

public final class JooqLogger {

  // Initialise only once
  private static boolean initialisationError = false;

  // The log4j Logger reference
  private Logger logger;

  // Get the logger proxy for a class
  public static JooqLogger getLogger(Class<?> clazz) {
    JooqLogger result = new JooqLogger();

    try {
      result.logger = Logger.getLogger(clazz);
    }

    // Log4j is not found on the classpath, so ignore most of logging
    catch (Throwable t) {
      if (!initialisationError) {
        initialisationError = true;
        result.error("JooqLogger could not initialise log4j logger...");
      }
    }

    return result;
  }

  // All methods from log4j Logger are available as well.
  // They provide redirection and ignore all calls if log4j is not available.
  public boolean isTraceEnabled() {
    if (logger != null) {
      return logger.isTraceEnabled();
    }
    else {
      return false;
    }
  }

  public void trace(Object message) {
    if (logger != null) {
      logger.trace(message);
    }
  }

  // [... etc ...]
}

我的问题是:

  1. 这是一个好主意/一个好的实践吗?或者我应该创建对 log4j 的“硬”依赖?
  2. 使用上述解决方案,日志记录不会像我直接调用 log4j 方法那样精确。例如,日志文件中记录的行号或 .java 文件将始终是 JooqLogger 的行号或 .java 文件,而不是调用者的行号或 .java 文件。有办法绕过这个问题吗?

I am maintaining a library for database access on http://jooq.sourceforge.net, and I would like to use log4j for logging. But logging is really a very optional part of my library. If log4j is not available in client code, then I won't do logging. So I created a logger proxy like this:

public final class JooqLogger {

  // Initialise only once
  private static boolean initialisationError = false;

  // The log4j Logger reference
  private Logger logger;

  // Get the logger proxy for a class
  public static JooqLogger getLogger(Class<?> clazz) {
    JooqLogger result = new JooqLogger();

    try {
      result.logger = Logger.getLogger(clazz);
    }

    // Log4j is not found on the classpath, so ignore most of logging
    catch (Throwable t) {
      if (!initialisationError) {
        initialisationError = true;
        result.error("JooqLogger could not initialise log4j logger...");
      }
    }

    return result;
  }

  // All methods from log4j Logger are available as well.
  // They provide redirection and ignore all calls if log4j is not available.
  public boolean isTraceEnabled() {
    if (logger != null) {
      return logger.isTraceEnabled();
    }
    else {
      return false;
    }
  }

  public void trace(Object message) {
    if (logger != null) {
      logger.trace(message);
    }
  }

  // [... etc ...]
}

My questions are:

  1. Is this a good idea / a good practice? Or should I create a "hard" dependency on log4j?
  2. With the above solution, logging will not be as precise as if I were directly invoking log4j methods. For instance, the logged line number or .java file in the log files will always be the ones of JooqLogger, not of the caller. Is there a way to circumvent that problem?

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

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

发布评论

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

评论(1

萌化 2024-10-12 06:27:45

使用 Null 实现创建接口,然后允许结束开发人员/用户/管理员替换为不同的实现。

Create an interface for it with a Null implementation, then allow the end developer/user/admin to substitute in a different implementation.

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