日志中缺少时间戳(JAVA)

timestamp missing in log(JAVA)

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

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

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

  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. 主要class:
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

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

根据您在评论中发布的错误,我认为您的问题是版本不兼容我用以下依赖项和您的 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>

我的 class 得到以下日志:

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

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