log4j 重定向到 swing 中的桌面应用程序

发布于 2024-09-18 06:33:38 字数 614 浏览 2 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

清君侧 2024-09-25 06:33:39

最简单的选择是在 GUI 初始化后以编程方式添加附加程序。像这样的事情:

Logger.getRootLogger().addAppender(yourTextAreaAppender);

编辑:要仅记录信息级别,请执行以下操作:

yourTextAreaAppender.addFilter(new Filter() {
    @Override
    public int decide(LoggingEvent event) {
        if (event.getLevel().equals(Level.INFO)) {
            return ACCEPT;
        } else {
            return DENY;
        }
    }
});

The simplest option is to programmatically add your appender once your GUI has been initialised. Something like this:

Logger.getRootLogger().addAppender(yourTextAreaAppender);

EDIT: To only log the INFO level do this:

yourTextAreaAppender.addFilter(new Filter() {
    @Override
    public int decide(LoggingEvent event) {
        if (event.getLevel().equals(Level.INFO)) {
            return ACCEPT;
        } else {
            return DENY;
        }
    }
});
奶茶白久 2024-09-25 06:33:39

嗯,这可能非常简单,

  1. 在 log4j.property 文件中指定属性,在我的例子中是:

    log4j.rootLogger=S
    log4j.appender.S=com.ibm.nzna.projects.qit.gui.StatusMessageAppender
    log4j.appender.S.layout=org.apache.log4j.PatternLayout
    log4j.appender.S.layout.ConversionPattern=%m
    
  2. Write 是具有以下代码的新类:

    导入 org.apache.log4j.AppenderSkeleton;
    导入 org.apache.log4j.Level;
    导入 org.apache.log4j.spi.LoggingEvent;
    
    /**
     * @作者 Ashish Tyagi
     *
     */
    公共类 StatusMessageAppender 扩展 AppenderSkeleton {
        私有 StatusBar statusBar = AppDefaultWin.getStatusBar();
        受保护的无效附加(LoggingEvent事件){
            if(event.getLevel().equals(Level.INFO)){
                    //这里设置你的swing组件的文本;
                   //在我的例子中是:statusBar.st_STATUS.setText(event.getMessage().toString());
            }
        }
    
        公共无效关闭(){
    
        }
        公共布尔需要布局(){
            返回假;
        }
    
    }
    

Well this could be pretty simple,

  1. Specify the property in log4j.property file, in my case it is:

    log4j.rootLogger=S
    log4j.appender.S=com.ibm.nzna.projects.qit.gui.StatusMessageAppender
    log4j.appender.S.layout=org.apache.log4j.PatternLayout
    log4j.appender.S.layout.ConversionPattern=%m
    
  2. Write is new class with the following code :

    import org.apache.log4j.AppenderSkeleton;
    import org.apache.log4j.Level;
    import org.apache.log4j.spi.LoggingEvent;
    
    /**
     * @author Ashish Tyagi
     *
     */
    public class StatusMessageAppender extends AppenderSkeleton {
        private StatusBar statusBar = AppDefaultWin.getStatusBar();
        protected void append(LoggingEvent event) {
            if(event.getLevel().equals(Level.INFO)){
                    //here set the text of your swing component;
                   //in my case it is: statusBar.st_STATUS.setText(event.getMessage().toString());
            }
        }
    
        public void close() {
    
        }
        public boolean requiresLayout() {
            return false;
        }
    
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文