简体   繁体   English

使用log4j2以编程方式创建FileAppender时出错

[英]Error in programmatically creating FileAppender using log4j2

I wanted to try and test the FileAppender on my local machine after reading the documentation online. 在线阅读文档后,我想尝试在本地计算机上测试FileAppender。 When i create the object by calling the build method i get errors. 当我通过调用build方法创建对象时,出现错误。

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. 我将在应用程序中升级log4j版本,当我无法在本地创建和测试一个文件附加器时,我正在学习文件附加器。我尝试在Internet上查找并找到了一些有关如何创建它的代码。 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. 我正在使用Windows计算机,并在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. 在调用build()方法期间出现错误。

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. 我不明白为什么我会得到null异常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM