1

I wanted to try and test the FileAppender on my local machine after reading the documentation online. When i create the object by calling the build method i get errors.

I will be upgrading the log4j version in an application and was learning about file appenders when i failed to create and test one locally.I tried looking on the internet and found some code on how to create it. When i try the same i get errors show below in the stack trace. I am using a windows machine and running the code on netbeans.


package logtest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.appender.FileAppender;
class Test
{ 
    public void appendLogs(String logEvent)
    {
        LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
        Configuration conf = ctx.getConfiguration();
        FileAppender.Builder b = FileAppender.newBuilder();
        b.withFileName("TestFile");
        b.withAppend(true);
        b.build();
        FileAppender fa = b.build();
        System.out.println(fa.toString());
        fa.start();
        fa.error("Error message");
    }
}
public class LogTest {

    private static final Logger LOG = LogManager.getLogger(LogTest.class);

    public static void main(String[] args) {

        Test t = new Test();
        t.appendLogs("Test log");
        System.out.println(t.toString());
        t.appendLogs("This is an error in a file");
        LOG.debug("This Will Be Printed On Debug");
        LOG.info("This Will Be Printed On Info");
        LOG.warn("This Will Be Printed On Warn");
        LOG.error("This Will Be Printed On Error");
        LOG.fatal("This Will Be Printed On Fatal");

        LOG.info("Appending string: {}.", "Hello, World");
    }
}

I get the error during the call to build() method.

Exception in thread "main" java.lang.NullPointerException: name
    at java.util.Objects.requireNonNull(Objects.java:228)
    at org.apache.logging.log4j.core.appender.AbstractAppender.<init>(AbstractAppender.java:205)
    at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.<init>(AbstractOutputStreamAppender.java:120)
    at org.apache.logging.log4j.core.appender.FileAppender.<init>(FileAppender.java:259)
    at org.apache.logging.log4j.core.appender.FileAppender.<init>(FileAppender.java:42)
    at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:104)
    at logtest.Test.appendLogs(LogTest.java:17)
    at logtest.LogTest.main(LogTest.java:31)
Java Result: 1

I dont understand why i get the null exception.

Shivam...
  • 409
  • 1
  • 8
  • 21
  • This feels like an [XY Problem](https://meta.stackexchange.com/a/66378) to me. Can you please tell us why you are creating the appender programmatically rather than using a configuration file? – D.B. Sep 16 '19 at 15:20
  • Actually the application uses a daily rolling file appender to create log files. I have a class in my code which extends fileappender and creates the missing files on the log file path and passes the file name to setfile method of fileappender. I dont know how to rewrite this in log4j 2.12 version. So i was trying to programmatically create a FileAppender. – Shivam... Sep 16 '19 at 16:22
  • I would highly recommend that you use a configuration file. If you create the appender with code your code becomes dependent on the log4j2 implementation, which means you may have problems upgrading to newer versions of log4j2 in the future. It's best to depend on the public interface only. – D.B. Sep 17 '19 at 01:38

1 Answers1

1

You need to set a name for the appender.

FileAppender.Builder b = FileAppender.newBuilder();
b.withFileName("TestFile");
b.withAppend(true);
b.setName("my-appender");
b.build();
FileAppender fa = b.build();
Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17