简体   繁体   English

log4j:每个Appender的属性列表?

[英]log4j: List of properties for each Appender?

The task: 任务:

I am trying to configure an Appender to output to a JTextArea. 我正在尝试将Appender配置为输出到JTextArea。 I believe a WriterAppender is capable of writing to an OutputStream. 我相信WriterAppender能够写入OutputStream。 I have already subclassed OutputStream to produce a JTextAreaOutputStream class, which currently is populated by hijacking the output from System.out and System.err. 我已经将OutputStream子类化以生成JTextAreaOutputStream类,当前该类是通过劫持System.out和System.err的输出来填充的。

The configuraton file: 配置文件:

    # File appender for the GUI screen
    log4j.appender.gui = org.apache.log4j.WriterAppender
    log4j.appender.gui.Target=project.gui.GUIView.logWindow   //logWindow is the name of my JTextArea

    # Root logger option
    log4j.rootLogger=INFO, gui

The error: 错误:

log4j:WARN No such property [target] in org.apache.log4j.WriterAppender.

The question: 问题:

Anyone know where I can identify the valid set of properties per Appender? 有人知道我可以在哪里识别每个Appender的有效属性集吗?

Here's how I configured WriterAppender: 这是我配置WriterAppender的方法:

In log4j.properties : log4j.properties中

log4j.rootLogger=INFO, ConsoleAppenderInstance,FileAppenderInstance, WriterAppenderInstance
...
log4j.appender.WriterAppenderInstance=org.apache.log4j.WriterAppender
log4j.appender.WriterAppenderInstance.layout=org.apache.log4j.PatternLayout
log4j.appender.WriterAppenderInstance.layout.ConversionPattern=%m%n

In java code: 在Java代码中:

StringWriter writer = new StringWriter();
Logger root = Logger.getRootLogger();
WriterAppender app = (WriterAppender)root.getAppender("WriterAppenderInstance");
app.setWriter(writer);
...
writer.toString()

And as for the available properties, I guess everything starting with set here: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/WriterAppender.html (and in subclasses) 至于可用的属性,我猜一切都从这里的set开始: http : //logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/WriterAppender.html (以及子类)

Why do you think that WriterAppender has such a property? 您为什么认为WriterAppender具有这样的属性? As far as I can see from JavaDocs , it does not have such a property. 据我从JavaDocs可以看到,它没有这样的属性。 Maybe you are confusing it with ConsoleAppender ? 也许您将其与ConsoleAppender混淆了?

You can get a list of properties per appender if you open relevant JavaDoc/source code and look up all JavaBean-style setter methods. 如果打开相关的JavaDoc /源代码并查找所有JavaBean样式的setter方法,则可以获取每个附加程序的属性列表 This means that if WriterAppender would have target property, it would need to have setTarget(...) setter method. 这意味着,如果WriterAppender具有target属性,则需要具有setTarget(...) setter方法。

Anyway, I recommend you to subclass WriterAppender and create your own JTextAreaAppender that would pass in your custom OutputStream to superclass. 无论如何,我建议您WriterAppender子类,创建自己的JTextAreaAppender ,并将您的自定义OutputStream传递给超类。 See ConsoleAppender and FileAppender for samples of such subclasses. 有关此类子类的示例,请参见ConsoleAppenderFileAppender

EDIT : by the way, as you most probably need to pass in a reference to JTextArea to your JTextAreaAppender , I would recommend you to configure log4j programmatically. 编辑 :顺便说一下,因为你很可能需要在引用传递JTextAreaJTextAreaAppender ,我建议你以编程方式配置log4j。 Or at least add your custom appender programmatically, after you have a reference to the JTextArea . 或至少在引用JTextArea之后以编程方式添加自定义追加程序。

Or, even better, you could configure it via properties file but leave the initial JTextArea reference null - after your application has started up and you have your jTextArea reference, you can programmatically look trough all the log4j appenders and pass in the reference to your custom JTextAreaAppender . 或者,甚至更好的是,您可以通过属性文件对其进行配置,但将初始JTextArea引用保留为空-在您的应用程序启动并拥有jTextArea引用之后,您可以通过编程方式查看所有log4j附加程序并将引用传递给自定义JTextAreaAppender

I know this is pretty old, but I just wanted to follow up on this question as I just spent all morning trying to find the same information. 我知道这已经很老了,但是我只是想跟进这个问题,因为我整个上午都在努力寻找相同的信息。

From what I can tell the log4j WriterAppender cannot be configured in an external configuration file as it has no configurable options. 据我所知,无法在外部配置文件中配置log4j WriterAppender,因为它没有可配置的选项。 The class is designed to write to a Writer or OutputStream and there is just no way to specify that object in the string-based configuration file. 该类旨在写入WriterOutputStream并且无法在基于字符串的配置文件中指定该对象。

If this is incorrect please correct me and point me to the place where the correct information can be found. 如果这不正确,请纠正我,然后将我指向可以找到正确信息的地方。 I'm kind of surprised that this answer isn't more easily and obviously found as it is. 我很惊讶这个答案不是那么容易,而且显然是这样找到的。

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

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