开发者

log4j: List of properties for each 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?


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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜