javalog4jlog4j2appenderrollingfileappender

Unable to instantiate RollingFileAppender


I am trying to create a RollingFileAppender through java code. The below code always throws a null pointer Exception for the line appender.start() stating RollingFileAppender 'null': No name provided. I am unable to set the name because the name is usually set by the RollingFileAppender plugin.

https://logging.apache.org/log4j/2.x/log4j-core/apidocs/src-html/org/apache/logging/log4j/core/appender/RollingFileAppender.html Please advise how to instantiate the 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;
}

Solution

  • 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();
      }
    }
    

    POM

    <?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>
    

    Error

    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)