美文网首页
@Log (and friends)

@Log (and friends)

作者: eagle_king | 来源:发表于2019-01-14 10:32 被阅读0次

@Log (and friends)

船长的日志,标榜24435.7:“又是什么线?”
@Log在lombok v0.10中添加了 各种变体。 lombok 0.10中的新功能:您可以使用日志注释对任何类进行注释,以使lombok生成记录器字段。
记录器已命名log,字段的类型取决于您选择的记录器。

lombok v1.16.24中的新功能:添加了Google的FluentLogger(flogger)。

Overview

你把@Log你的类的变体(适用于你使用的日志系统); 然后,您有一个静态的final log字段,初始化为您的类的名称,然后您可以使用它来编写日志语句。
有几种选择:
@CommonsLog
创建
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger
创建
private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog
创建
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
创建
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
创建
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
创建
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
创建
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
创建
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
默认情况下,记录器的主题(或名称)将是使用@Log注释注释的类的类名。可以通过指定topic参数来自定义。例如:@XSlf4j(topic="reporting")

With Lombok

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

@Log
public class LogExample {
  
  public static void main(String... args) {
    log.severe("Something's wrong here");
  }
}

@Slf4j
public class LogExampleOther {
  
  public static void main(String... args) {
    log.error("Something else is wrong here");
  }
}

@CommonsLog(topic="CounterLog")
public class LogExampleCategory {

  public static void main(String... args) {
    log.error("Calling the 'CounterLog' with a message");
  }
}

Vanilla Java

public class LogExample {
  private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
  
  public static void main(String... args) {
    log.severe("Something's wrong here");
  }
}

public class LogExampleOther {
  private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);
  
  public static void main(String... args) {
    log.error("Something else is wrong here");
  }
}

public class LogExampleCategory {
  private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog");

  public static void main(String... args) {
    log.error("Calling the 'CounterLog' with a message");
  }
}

Supported configuration keys:

lombok.log.fieldName = an identifier (default: log).
lombok.log.fieldIsStatic = [true | false] (default: true)
lombok.log.flagUsage = [warning | error] (default: not set)
lombok.log.xxx.flagUsage = [warning | error] (default: not set)

Small print

如果log已存在已调用的字段,则将发出警告,并且不会生成任何代码。

lombok的各种日志注释的未来特性是找到对logger字段的调用,如果所选的日志记录框架支持它,并且日志级别可以是从日志调用确定的编译时间,则使用if语句来保护它。这样,如果最终忽略了日志语句,则完全避免了可能昂贵的日志字符串计算。这意味着你要不要放在您登录表达任何副作用。

相关文章

网友评论

      本文标题:@Log (and friends)

      本文链接:https://www.haomeiwen.com/subject/iqkddqtx.html