【问题标题】:timestamp missing in log(JAVA)日志中缺少时间戳(JAVA)
【发布时间】:2021-08-17 07:33:06
【问题描述】:

完全是我所询问的概念的新手。

我有一个 Maven 项目,在项目 lombok 和 Slf4j 的支持下进行日志记录。 我希望时间戳出现在日志输出中。但我只观察类名和消息。

任何人都可以指导我或建议一些链接吗?

  1. pom.xml
<dependencies>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.0-alpha4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.0-alpha4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
  1. 主类:
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ObjectsAndVariables {

    public static void main(String[] args) {
           log.info("HELLO");

}}
  1. 在 src/main/resources 中有 logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <conversionRule conversionWord="clr"
        converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex"
        converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx"
        converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    <property name="CONSOLE_LOG_PATTERN"
        value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />

    <appender name="STDOUT"
        class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>


    <!-- Console output log level -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>
  1. 输出:
[main] INFO model.ObjectsAndVariables - HELLO

如何获取时间戳?层级有什么问题吗?

【问题讨论】:

  • 这个输出来自slf4j-simple,你目前不使用logback。您需要删除上述依赖项,然后添加 logback logback-to-slf4j 桥依赖项。另请参阅这个很棒的教程:baeldung.com/logback
  • 您好 Lino,感谢您的回复。在删除 slf4j-simple 并将 logback 和 logback 添加到 slf4j 依赖项时,我收到错误:
  • SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details. SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions prior to 1.8. SLF4J: Ignoring binding found at [jar:file:/C:/Users/R.Premsagar/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#ignoredBindings for an explanation.

标签: java spring maven slf4j lombok


【解决方案1】:

根据您在评论中发布的错误,我认为您的问题是版本不兼容我使用以下依赖项和您的 logback.xml 进行了测试

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-core</artifactId>
  <version>1.3.0-alpha9</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>2.0.0-alpha4</version>
</dependency>

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.3.0-alpha9</version>
</dependency>

我的班级有以下日志:

2021-08-17 13:22:05.773  INFO   --- [           main] c.h.s.ch4.SystemCommandApplication       : HELLO

如您所见,它与您的 logback 配置兼容

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多