小工具中的Logback+Swing

发布于 2024-09-11 23:49:46 字数 203 浏览 7 评论 0 原文

我需要破解一个小工具。它应该读取几个文件并转换它们。现在可以在我的 IDE 中运行了。对于用户,我想添加一个小的 UI,仅显示日志输出。

您知道用于 logback 的现成 Swing 附加程序吗?或者将 System.out 重定向到一个小 UI,只有一个文本字段和一个“关闭”按钮?

PS:我不是在寻找电锯、竖锯或莉莉丝。我想要在应用程序中显示日志消息。

I need to hack up a small tool. It should read a couple of files and convert them. Right now that works in my IDE. For the user, I'd like to add a small UI which simply shows the log output.

Do you know of a ready-to-use Swing appender for logback? Or something which redirects System.out to a little UI with nothing more than a text field and a "Close" button?

PS: I'm not looking for Chainsaw or Jigsaw or Lilith. I want the display of the log messages in the application, please.

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

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

发布评论

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

评论(3

葬﹪忆之殇 2024-09-18 23:49:46

您需要编写一个自定义附加程序类,如下所示:

public class MyConsoleAppender extends AppenderBase<ILoggingEvent> {
  private Encoder<ILoggingEvent> encoder = new EchoEncoder<ILoggingEvent>();
  private ByteArrayOutputStream  out     = new ByteArrayOutputStream();

  public MyConsoleAppender() {
     LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
     setContext(lc);
     start();
     lc.getLogger("ROOT").addAppender(this);
  }

  @Override
  public void start() {
     try {
        encoder.init(out);
     } catch (IOException e) {}
     super.start();
  }

  @Override
  public void append(ILoggingEvent event) {
     try {
        encoder.doEncode(event);
        out.flush();
        String line = out.toString(); // TODO: append _line_ to your JTextPane
        out.reset();
     } catch (IOException e) {}
  }
}

您可以将 EchoEncoder 替换为 PatternLayoutEncoder(请参阅 logback 示例文件夹中的 CountingConsoleAppender 示例)。

编码器会将每个事件写入字节缓冲区,然后您可以提取字符串并将其写入 JTextPane 或 JTextArea 或您想要的任何内容。

You need to write a custom appender class like so:

public class MyConsoleAppender extends AppenderBase<ILoggingEvent> {
  private Encoder<ILoggingEvent> encoder = new EchoEncoder<ILoggingEvent>();
  private ByteArrayOutputStream  out     = new ByteArrayOutputStream();

  public MyConsoleAppender() {
     LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
     setContext(lc);
     start();
     lc.getLogger("ROOT").addAppender(this);
  }

  @Override
  public void start() {
     try {
        encoder.init(out);
     } catch (IOException e) {}
     super.start();
  }

  @Override
  public void append(ILoggingEvent event) {
     try {
        encoder.doEncode(event);
        out.flush();
        String line = out.toString(); // TODO: append _line_ to your JTextPane
        out.reset();
     } catch (IOException e) {}
  }
}

You can replace the EchoEncoder with a PatternLayoutEncoder (see CountingConsoleAppender example in the logback examples folder).

The encoder will write each event to a byte buffer, which you can then extract a string and write this to your JTextPane or JTextArea, or whatever you want.

别念他 2024-09-18 23:49:46

我经常依赖 JTextArea#append(),如本示例。与大多数 Swing 不同,该方法恰好是线程安全的。

附录: Console 是一个相关示例将 System.out 和 System.err 重定向到 JTextArea

I often rely on JTextArea#append(), as suggested in this example. Unlike most of Swing, the method happens to be thread safe.

Addendum: Console is a related example that redirects System.out and System.err to a JTextArea.

乞讨 2024-09-18 23:49:46

没有保证,但这是我刚刚写的示例:

/**
 * A Logback appender that appends messages to a {@link JTextArea}.
 * @author David Tombs
 */
public class JTextAreaAppender extends AppenderBase<ILoggingEvent>
{
    private final JTextArea fTextArea;
    private final PatternLayout fPatternLayout;

    public JTextAreaAppender(final Context loggerContext, final JTextArea textArea)
    {
        fTextArea = textArea;

        // Log the date, level, class name (no package), and the message.
        fPatternLayout = new PatternLayout();
        fPatternLayout.setPattern("%d{HH:mm:ss.SSS} %-5level - %msg");
        fPatternLayout.setContext(loggerContext);
        fPatternLayout.start();

        // Make sure not to call any subclass methods right now.
        super.setContext(loggerContext);
    }

    @Override
    protected void append(final ILoggingEvent eventObject)
    {
        // Actual appending must be done from the EDT.
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run()
            {
                final String logStr = fPatternLayout.doLayout(eventObject);

                // If the text area already has lines in it, append a newline first.
                if (fTextArea.getDocument().getLength() > 0)
                {
                    fTextArea.append("\n" + logStr);
                }
                else
                {
                    fTextArea.setText(logStr);
                }
            }
        });
    }    
}

No warranty, but here's a sample that I just wrote:

/**
 * A Logback appender that appends messages to a {@link JTextArea}.
 * @author David Tombs
 */
public class JTextAreaAppender extends AppenderBase<ILoggingEvent>
{
    private final JTextArea fTextArea;
    private final PatternLayout fPatternLayout;

    public JTextAreaAppender(final Context loggerContext, final JTextArea textArea)
    {
        fTextArea = textArea;

        // Log the date, level, class name (no package), and the message.
        fPatternLayout = new PatternLayout();
        fPatternLayout.setPattern("%d{HH:mm:ss.SSS} %-5level - %msg");
        fPatternLayout.setContext(loggerContext);
        fPatternLayout.start();

        // Make sure not to call any subclass methods right now.
        super.setContext(loggerContext);
    }

    @Override
    protected void append(final ILoggingEvent eventObject)
    {
        // Actual appending must be done from the EDT.
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run()
            {
                final String logStr = fPatternLayout.doLayout(eventObject);

                // If the text area already has lines in it, append a newline first.
                if (fTextArea.getDocument().getLength() > 0)
                {
                    fTextArea.append("\n" + logStr);
                }
                else
                {
                    fTextArea.setText(logStr);
                }
            }
        });
    }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文