Log 实时日志

用于实时显示日志或程序输出结果。

基本用法

通过设置 source 来获取日志,支持 ANSI 基本颜色显示。

  1. {
  2. "type": "log",
  3. "height": 300,
  4. "source": "http://localhost:3000/"
  5. }

由于缺少线上服务,所以这个例子无法在线演,它的运行效果如下图所示

示例

后端实现参考

后端需要通过流的方式输出结果,比如 Node 实现示例:

  1. const http = require('http');
  2. let app = http.createServer((req, res) => {
  3. res.writeHead(200, {
  4. 'Content-Type': 'text/plain',
  5. 'Access-Control-Allow-Origin': 'http://localhost:8888',
  6. 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
  7. 'Access-Control-Allow-Credentials': 'true',
  8. 'Access-Control-Allow-Headers': '*'
  9. });
  10. let index = 1;
  11. let timer = setInterval(() => {
  12. if (index < 50) {
  13. // 每次 write 都会立刻传给前端
  14. res.write(`line: ${index}\n`);
  15. index += 1;
  16. } else {
  17. res.end('end');
  18. clearInterval(timer);
  19. }
  20. }, 100);
  21. });
  22. app.listen(3000, '127.0.0.1');
  23. console.log('Node server running on port 3000');

其它语言请查找如何使用 stream 的方式返回内容,比如 Spring 的 StreamingResponseBody

  1. @Controller
  2. public class StreamingResponseBodyController {
  3. @GetMapping("/logs")
  4. public ResponseEntity<StreamingResponseBody> handleLog() {
  5. StreamingResponseBody stream = out -> {
  6. for (int i = 0; i < 1000; i++) {
  7. String msg = "log" + " @ " + new Date();
  8. out.write(msg.getBytes());
  9. out.flush();
  10. }
  11. out.close();
  12. };
  13. return new ResponseEntity(stream, HttpStatus.OK);
  14. }
  15. }

需要注意有些反向代理有 buffer 设置,比如 nginx 的 proxy_buffer_size,它会使得即便后端返回内容也需要等 buffer 满了才会真正返回前端,如果需要更实时的效果就需要关掉此功能。

对于超长日志的优化

1.10.0 及以上版本

如果日志非常长会导致页面卡顿,这时有几种处理方法,请根据需求进行选择,也可以都使用:

  1. 设置 rowHeight,比如 "rowHeight": 22,这时就会默认启用虚拟渲染,避免渲染卡顿
    • 优点:仍然可以查看所有日志
    • 缺点:如果某一行日志很长也不会自动折行,会出现水平滚动条;目前暂时不支持 autoScroll
  2. 设置 maxLength,限制最大显示行数
    • 优点:某一行日志很长的时候会自动折行
    • 缺点:无法查看之前的日志
  3. 试试通过 "disableColor": false 关闭 ANSI 颜色

自动滚动到底部

通过 autoScroll 可以关闭此功能

source 支持变量

1.4.2 及以上版本

可以初始设置为空,这样初始不会加载,而等这个变量有值的时候再加载

  1. {
  2. "type": "form",
  3. "body": [
  4. {
  5. "label": "数据源",
  6. "type": "select",
  7. "name": "source",
  8. "options": [
  9. {
  10. "label": "A",
  11. "value": "http://localhost:3000/"
  12. },
  13. {
  14. "label": "B",
  15. "value": "http://localhost:4000/"
  16. }
  17. ]
  18. },
  19. {
  20. "type": "log",
  21. "height": 300,
  22. "placeholder": "请点击上面的数据源",
  23. "source": "${source | raw}"
  24. }
  25. ]
  26. }

source 支持高级配置

1.6.1 及以上版本

可以类似 api 那样自定义 header、method 等,比如:

  1. {
  2. "type": "log",
  3. "height": 300,
  4. "source": {
  5. "method": "post",
  6. "url": "[/api/mock2/form/saveForm](http://localhost:3000/)",
  7. "data": {
  8. "myName": "${name}",
  9. "myEmail": "${email}"
  10. },
  11. "headers": {
  12. "my-header": "${myHeader}"
  13. }
  14. }
  15. }

属性表

属性名 类型 默认值 说明
height number 500 展示区域高度
className string 外层 CSS 类名
autoScroll boolean true 是否自动滚动
placeholder string 加载中的文字
encoding string utf-8 返回内容的字符编码
source string 接口
rowHeight number 设置每行高度,将会开启虚拟渲染
maxLength number 最大显示行数
disableColor boolean 关闭 ANSI 颜色支持
operation Array 可选日志操作:[‘stop’,’clear’,’showLineNumber’]