本文介绍了如何在字符串数组中获取 Logger 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我使用了 log4j.Logger
,并使用 FileAppender
将日志值写入了一个文件.
I used the log4j.Logger
and I have written the log value in to a file using the FileAppender
.
现在我想在文本区域中显示日志值.如何将记录器值分配给字符串数组?
Now I want to show the log value in a textarea. How can I assign the logger value to an array of Strings?
推荐答案
您可以在 Logger
中添加额外的 Appender
.使用 WriterAppender 写入 StringWriter
然后将 TextArea
的内容更改为 StringWriter#toString()
You can add an additional Appender
to your Logger
. Use a WriterAppender to write to a StringWriter
then you change the content of your TextArea
to the value of StringWriter#toString()
示例代码:
public static void main(String[] args) {
Logger logger = Logger.getLogger("logger");
Layout layout = new PatternLayout();
StringWriter stringWriter = new StringWriter();
WriterAppender writerAppender = new WriterAppender(layout, stringWriter);
logger.addAppender(writerAppender);
TextArea textArea = new TextArea();
logger.error("test");
//if(stringWriter changed){// compare to old TextArea-content?
textArea.setText(stringWriter.toString());
/
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!