JTextArea 作为 log4j Logger 的监听器

发布于 2024-09-11 20:58:42 字数 198 浏览 2 评论 0原文

如何设置 JTextArea 来接收我记录的任何内容(缺少创建诸如“MyLoggerListener”之类的接口并将其实现到我的 JTextArea 中)

编辑:

我通过创建 TextAreaOutputStream、用它制作一个 printwriter 并添加来解决这个问题构造函数中带有 printwriter 的 WriterAppender。

How can I set up a JTextArea to receive anything that I log (short of creating an interface such as "MyLoggerListener" and implementing it into my JTextArea)

EDIT:

I fixed this problem by creating a TextAreaOutputStream, making a printwriter with it, and adding a WriterAppender with the printwriter in the constructor.

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

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

发布评论

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

评论(3

戏蝶舞 2024-09-18 20:58:42

我认为@OscarRyz 的方法是一个合理的想法。

不过,我认为你应该好好考虑一下是否应该尝试这样做。问题在于将日志消息绘制到 Java GUI 中可能会占用大量 CPU 资源。这可能会使您的应用程序日志记录变慢,并可能因此扰乱应用程序的时间。

(并不是说您的应用程序应该对时间敏感。但是,如果您确实存在与时间相关的错误,那么更改日志记录级别等会导致应用程序的行为不同,则没有任何帮助。)

我的感觉是,在您的应用程序中嵌入一个精美的日志查看器很可能带来的麻烦超过了它的价值......

I think @OscarRyz's approach is a reasonable idea.

However, I think you should think hard whether you should try to do this. The problem is that painting log messages into a Java GUI is likely to be CPU intensive. This is likely to make your application logging slow, and is likely to perturb the timing of your application as a result.

(Not that your application should be timing sensitive. But if you do have timing related bugs, it is not helpful if changing logging levels etc causes the application to behave differently.)

My feeling is that embedding a fancy log viewer in your application is likely to be more trouble than it is worth ...

半窗疏影 2024-09-18 20:58:42

我对 Log4J2 的解决方案是

import java.io.Serializable;

import javax.swing.JTextArea;

import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.layout.PatternLayout;

public class TextAreaAppender extends AbstractAppender {

    private JTextArea textarea = null;
    private int maxLines;

    public TextAreaAppender(String name, Filter filter, Layout<? extends Serializable> layout, int maxLines, boolean ignoreExceptions, Property[] properties) {
        super(name, filter, layout, ignoreExceptions, properties);
        this.maxLines = maxLines;
    }

    @Override
    public void append(LogEvent event) {
        PatternLayout layout = (PatternLayout) getLayout();
        textarea.append(layout.toSerializable(event));
        if ((maxLines > 0) && (textarea.getLineCount() > (maxLines + 1))) {
            String text = textarea.getText();
            int pos = text.indexOf('\n');
            text = text.substring(pos+1);
            textarea.setText(text);
        }
    }

    public void setTextArea(JTextArea textArea) {
        this.textarea = textArea;
    }

}

这样使用它:

PatternLayout.Builder layoutbuilder = PatternLayout.newBuilder().withPattern("%-5level %logger{36} - %msg%n");
TextAreaAppender appender = new TextAreaAppender("textLog", null, layoutbuilder.build(), 200, false, null);
appender.setTextArea(textLog);
org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
appender.start();
logger.addAppender(appender);

My solution for Log4J2 is this

import java.io.Serializable;

import javax.swing.JTextArea;

import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.layout.PatternLayout;

public class TextAreaAppender extends AbstractAppender {

    private JTextArea textarea = null;
    private int maxLines;

    public TextAreaAppender(String name, Filter filter, Layout<? extends Serializable> layout, int maxLines, boolean ignoreExceptions, Property[] properties) {
        super(name, filter, layout, ignoreExceptions, properties);
        this.maxLines = maxLines;
    }

    @Override
    public void append(LogEvent event) {
        PatternLayout layout = (PatternLayout) getLayout();
        textarea.append(layout.toSerializable(event));
        if ((maxLines > 0) && (textarea.getLineCount() > (maxLines + 1))) {
            String text = textarea.getText();
            int pos = text.indexOf('\n');
            text = text.substring(pos+1);
            textarea.setText(text);
        }
    }

    public void setTextArea(JTextArea textArea) {
        this.textarea = textArea;
    }

}

use it like this:

PatternLayout.Builder layoutbuilder = PatternLayout.newBuilder().withPattern("%-5level %logger{36} - %msg%n");
TextAreaAppender appender = new TextAreaAppender("textLog", null, layoutbuilder.build(), 200, false, null);
appender.setTextArea(textLog);
org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
appender.start();
logger.addAppender(appender);
梦醒时光 2024-09-18 20:58:42

您可以尝试将其输出重定向到类似的内容 在面板内创建 Java 控制台

替代文本 http://img122.imageshack.us/img122/5692/dibujoof2.png< /a>

也就是说,您可以对控制台记录器(或任何名称)进行子类化,并将输出重定向到该组件。

You can try to redirect it's output to something like this Create Java console inside panel

alt text http://img122.imageshack.us/img122/5692/dibujoof2.png

That is, you can sort of subclass Console logger ( or whatever it's name is ) and redirect the output to that component.

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