log4j 重定向到 swing 中的桌面应用程序
我有一个 Swing 中的 GUI 应用程序,在 NetBeans 中实现。对于从用户输入提供的各种功能,使用了一个 jar,它使用 log4j 进行日志记录。一切正常,但我必须将信息从 log4j 重定向到 GUI 中的文本区域。我发现要从 log4j 重定向到 swing 文本区域,必须扩展 AppenderSkeleton。我的问题是我无法修改 gui(例如,以便有一个扩展 AppenderSkeleton 的 JTextArea),所以我必须有一个附加到我的 JTextarea 的类。现在我的应用程序在 log4j 之前初始化。我的问题是,我找不到一种方法来设置 AppenderSkeleton 自定义类的属性,即对我的 gui 的 jtextarea 的引用,以便当 log4j 初始化附加程序时,它将传递对应用程序文本区域的引用。 我在 log4J 配置文件中尝试过类似: log4j.appender.myAppender.theTextArea=path.to.myFrameclass.theTextArea 希望 log4j 会调用我的附加程序中的 setter 和框架中的 getter 来设置文本区域,但它不起作用。 如何使 log4j 初始化附加程序,将信息重定向到我的应用程序? 或者有没有办法让我的应用程序初始化自定义附加程序并通知 log4j 使用它进行日志记录? 谢谢你!
I have a GUI application in swing, implemented in NetBeans. For the various functionality provided from the input of the user, a jar is used, which uses log4j for logging. All is ok, but I have to redirect information from log4j to a text area in my GUI. I have found that to redirect from log4j to swing text area, one must extend an AppenderSkeleton. My problem is that I can not modify the gui (so as to have a JTextArea that extends an AppenderSkeleton for example) so I have to have such a class that appends to my JTextarea. Now my application initializes before log4j. My problem is that I can not find a way to set as property to the AppenderSkeleton custom class, a reference to the jtextarea of my gui , so that when log4j initializes the appender, it will pass a reference to the application's text area.
I tried in the log4J configuration file something like:
log4j.appender.myAppender.theTextArea=path.to.myFrameclass.theTextArea
hopping that log4j would call the setter in my appender and the getter from my frame to set the text area, but it does not work.
How can I make the appender initialized by log4j, redirect info to my application?
Or is there a way for my application to initialize the custom appender and notify log4j to use it for logging?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的选择是在 GUI 初始化后以编程方式添加附加程序。像这样的事情:
编辑:要仅记录信息级别,请执行以下操作:
The simplest option is to programmatically add your appender once your GUI has been initialised. Something like this:
EDIT: To only log the INFO level do this:
嗯,这可能非常简单,
在 log4j.property 文件中指定属性,在我的例子中是:
Write 是具有以下代码的新类:
Well this could be pretty simple,
Specify the property in log4j.property file, in my case it is:
Write is new class with the following code :