Java高级文本日志记录窗格,可实现大输出 [英] A Java advanced text logging pane for large output

查看:326
本文介绍了Java高级文本日志记录窗格,可实现大输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mathematica带有一个简单的Java程序,该程序可检查前端与内核之间的通信.它被称为 LinkSnooper 总的来说,它的工作相当不错.看起来像这样

Mathematica comes with a simple java program that allows to inspect the communication between front end and the kernel. It's called LinkSnooper and in general it works quite nice. It looks like this

我认为我可以在某种程度上改善程序的行为和可用性,但是要做到这一点,我需要重新实现某些部分.我需要的一个基本组件是文本窗格,它具有以下属性:

I think I can improve the behavior and usability of the program to some extend, but to do this, I need to reimplement some parts. One fundamental piece that I need is a text pane, which has the following properties:

  • 它可以接收大量数据,并且可能应该使用快速环形缓冲区,以便在数据增长过多时删除最前面的日志行.另一种可能性是,它会自动开始将数据写入磁盘,并在用户向上滚动以查看第一个条目时可能会重新加载数据.
  • 它应该能够处理彩色文本.我计划在每条到达的行上使用一个简单的荧光笔(日志数据实际上是真正的Mathematica语法),以使阅读更加轻松
  • 它不需要可写.如果文本窗格为只读,则可以.

问题:是否已经存在类似的内容?目前,LinkSnooper在下面使用JTextArea,在我开始编写自己的版本之前,我想问问是否有人已经这样做了.

Question: Does something like this already exist? Currently, LinkSnooper uses a JTextArea underneath and before I start do write my own version, I wanted to ask whether someone has already done this.

我计划做的是使用一些Logger框架,因为对我来说那些库应该能够处理很多数据似乎很自然.此外,它们通常提供用于格式化消息的接口,并且您可以定义可以处理不同消息的不同处理程序.我希望的是有人已经将其与可以处理大输出的整齐工作的文本窗口结合在一起.

What I planned to do was to use some Logger framework because it seems natural to me that those libraries should be able to handle a lot of data. Additionally, they often provide interfaces to format the messages and you can define different handlers that can take care of different messages. What I was hoping for was that someone already has combined this with a neatly working text window that can handle large output.

推荐答案

正如Simon所指出的,我建议对此任务使用JavaFX.

As Simon has pointed out I would suggest using JavaFX for this task.

如果只是"需要显示大量日志数据而无需高级突出显示(子字符串范围突出显示),请

If you "just" need to display large amounts of log data without advanced highlighting (sub-string range highlighting), ListView is the component for you.

它使用虚拟化的布局容器,因此实际上仅渲染视口可见区域中的单元格.这允许延迟加载,细胞回收等. ListView使用 ObservableList 作为其DataStructure.与EMF EList类似,ObservableList自动将其包含的数据更改通知ListView.

It uses a virtualized layout container, so only the cells that are in the visible area of the viewport are actually rendered. This allows for lazy loading, cell recycling etc. The ListView uses an ObservableList as its DataStructure. Similar to EMF EList, the ObservableListautomatically notifies the ListView on changes in its contained data.

有几种工厂方法可通过 FXCollections 甚至允许包装现有的List(例如RingBuffer).

There are several factory methods to create an ObservableList via FXCollections even allowing to wrap an existing List (e.g. RingBuffer).

如果您需要高级突出显示,则 RichTextFX 可能是适合的解决方案允许对其包含的文本进行详细的样式设置. RichTextFX也使用虚拟布局.

If you need the advanced highlighting, RichTextFX is probably the solution to go for as it allows detailed styling of its contained text. RichTextFX uses a virtualized layout, too.

编辑#2

汤姆(Tom)在他的博客中写道:

Tom has written about this in his blog: http://tomsondev.bestsolution.at/2014/12/27/displaying-and-editing-large-styled-texts/

编辑#1 ListView示例

JavaFX在从视图中分离模型方面做得非常好,因此我们尽量避免混淆,需要创建两件事:

JavaFX does a very good job at separating the model from the view, so we try not to mix this up and need to create two things:

  1. 数据类(模型)
  2. 该数据类(视图)的Cell渲染器.
  1. A data class (model)
  2. A Cell renderer for that data class (view).

首先是数据类:

public class LogData {

    private final String logMessage;
    private List<String> highlightedFragments = null;

    public LogData(String pLogMessage) {
        logMessage = pLogMessage;
    }

    public String getLogMessage() {
        return logMessage;
    }

    public List<String> getHighlightedFragments() {
        if (highlightedFragments == null) {
            doHighlight();
        }
        return highlightedFragments;
    }

    private void doHighlight() {
        List<String> highlightedParts = Collections.emptyList(); // TODO lexer
        highlightedFragments = highlightedParts;
    }
}

有趣的是,突出显示是按需完成的,而不是初始化的.换句话说:只有当单元格渲染器请求数据时,词法分析器才执行其工作.

The interesting part is, that the highlighting is done on demand not on initialization. Or in other words: The lexer only performs its work, when the cell renderer requests the data.

现在Cell渲染器:

ListView<LogData> listView = new ListView<>();
listView.setCellFactory(cb -> new LogDataCell(){});

public class LogDataCell extends ListCell<LogData>
{
    @Override
    protected void updateItem(LogData item, boolean empty) {
        super.updateItem(item, empty);

        if(empty || item == null) {
            setText(null);
            setGraphic(null);
        }
        else {
            List<String> fragments = item.getHighlightedFragments();
            if(fragments == null || fragments.isEmpty()) {
                setText(item.getLogMessage());
                setGraphic(null);
            }
            else {
                TextFlow textFlow = null; //TODO
                setText(null);
                setGraphic(textFlow);
            }
        }
    }
}

这不是一个完整的示例,还有几个待办事项,但希望您能理解.

This is not a fully working example, there are several TODOs left, but hopefully you get the idea.

如果要添加搜索突出显示,我在此处介绍了TableView控件元素的类似方法:

If you want to add search highlighting, I described a similar approach for the TableView control element here: JavaFX Table with highlighted text (Labels) with poor performance

这篇关于Java高级文本日志记录窗格,可实现大输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆