log4j 无法示例化RollingFileAppender

xxls0lw8  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(125)

我正在尝试通过java代码创建一个RollingFileAppender。下面的代码总是抛出一个空指针异常的行appender.start()声明RollingFileAppender 'null': No name provided。我无法设置名称,因为名称通常是由RollingFileAppender插件设置。
https://logging.apache.org/log4j/2.x/log4j-core/apidocs/src-html/org/apache/logging/log4j/core/appender/RollingFileAppender.html请建议如何示例化RollingFileAppender。

public static RollingFileAppender getAppender() {

        final TimeBasedTriggeringPolicy timeBasedPolicy = new TimeBasedTriggeringPolicy.Builder().withModulate(true).build();
        timeBasedPolicy.start();
        RollingFileAppender appender = new RollingFileAppender.Builder()
                     .withFileName("fileName")
                    .withFilePattern("pattern")
                    .withPolicy(timeBasedPolicy)
                    .build();
        appender.start();
        return appender;
}
5t7ly7z5

5t7ly7z51#

package com.demo.example;

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExampleApplication {

  public static RollingFileAppender getAppender() {

    final TimeBasedTriggeringPolicy timeBasedPolicy = new TimeBasedTriggeringPolicy.Builder().withModulate(true).build();
    timeBasedPolicy.start();
    RollingFileAppender appender = new RollingFileAppender.Builder<>()
        .withFileName("fileName")
        .withFilePattern("pattern")
        .withPolicy(timeBasedPolicy)
        .setName("MyAppender")
        .build();
    appender.start();
    return appender;
  }

  public static void main(String[] args) {
    Appender app = ExampleApplication.getAppender();
  }
}

聚甲醛

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.6.1</version>
        </dependency>
     </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

错误

Exception in thread "main" java.lang.IllegalStateException: Pattern does not contain a date
    at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.getNextTime(PatternProcessor.java:154)
    at org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy.initialize(TimeBasedTriggeringPolicy.java:117)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.initialize(RollingFileManager.java:170)
    at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:153)
    at com.demo.example.ExampleApplication.getAppender(ExampleApplication.java:20)
    at com.demo.example.ExampleApplication.main(ExampleApplication.java:26)

相关问题