log4j:每个 Appender 的属性列表?

发布于 2024-10-04 18:12:55 字数 641 浏览 8 评论 0原文

任务:

我正在尝试配置 Appender 以输出到 JTextArea。我相信 WriterAppender 能够写入 OutputStream。我已经对 OutputStream 进行了子类化以生成 JTextAreaOutputStream 类,该类当前是通过劫持 System.out 和 System.err 的输出来填充的。

配置文件:

    # 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

错误:

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

问题:

有人知道我可以在哪里识别每个 Appender 的有效属性集吗?

The task:

I am trying to configure an Appender to output to a JTextArea. I believe a WriterAppender is capable of writing to an 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撑一把青伞 2024-10-11 18:12:55

为什么你认为 WriterAppender 有这样一个财产?据我所知,JavaDocs,它没有这样的属性。也许您将它与 ConsoleAppender 混淆了?

如果您打开相关的 JavaDoc/源代码并查找所有 JavaBean 样式的 setter 方法,则可以获得每个附加程序的属性列表。这意味着如果 WriterAppender 具有 target 属性,则需要具有 setTarget(...) setter 方法。

无论如何,我建议您子类WriterAppender创建您自己的JTextAreaAppender,它将传入您的自定义OutputStream 到超类。请参阅 ConsoleAppenderFileAppender 此类子类的示例。

编辑:顺便说一句,由于您很可能需要将对 JTextArea 的引用传递给您的 JTextAreaAppender,我建议您配置 log4j以编程方式。或者至少在引用 JTextArea 后以编程方式添加自定义附加程序。

或者,更好的是,您可以通过属性文件对其进行配置,但将初始的 JTextArea 引用保留为 null - 在应用程序启动并且拥有 jTextArea 引用后,您可以通过编程方式查看所有 log4j 附加程序并将引用传递给您的自定义 JTextAreaAppender

Why do you think that WriterAppender has such a property? As far as I can see from JavaDocs, it does not have such a property. Maybe you are confusing it with 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. This means that if WriterAppender would have target property, it would need to have setTarget(...) setter method.

Anyway, I recommend you to subclass WriterAppender and create your own JTextAreaAppender that would pass in your custom OutputStream to superclass. See ConsoleAppender and FileAppender for samples of such subclasses.

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. Or at least add your custom appender programmatically, after you have a reference to the 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.

一梦浮鱼 2024-10-11 18:12:55

以下是我配置 WriterAppender 的方式:

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

在 java 代码中:

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

至于可用的属性,我猜一切都以 set 开头: http://logging.apache.org/log4j/1.2/apidocs/org/apache /log4j/WriterAppender.html (以及在子类中)

Here's how I configured WriterAppender:

In 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:

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)

靖瑶 2024-10-11 18:12:55

我知道这已经很老了,但我只是想跟进这个问题,因为我花了整个早上试图找到相同的信息。

据我所知,log4j WriterAppender 无法在外部配置文件中配置,因为它没有可配置选项。该类旨在写入 WriterOutputStream,并且无法在基于字符串的配置文件中指定该对象。

如果这是不正确的,请纠正我并指出可以找到正确信息的地方。我有点惊讶的是,这个答案并没有更容易、更明显地找到。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文