You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Grafana日志面板中实现堆栈跟踪多行显示?

在Grafana中解析CloudWatch日志里的换行符(\n)

以下是几种可行的解决方法,让日志中的\n显示为实际换行:

方法1:HTML渲染+字段替换

  • 编辑日志面板,进入Panel options
  • 在Display区域,将Content mode设置为HTML
  • 针对堆栈跟踪字段,在字段显示模板中使用替换语法:
    ${exception.stacktrace:replace(/\\n/g, '<br>&nbsp;&nbsp;&nbsp;&nbsp;')}
    
    这样既会把\n转为换行,还能保留堆栈的缩进格式。

方法2:CloudWatch Insights预处理

  • 使用CloudWatch Insights查询日志时,提前替换换行符:
    fields @timestamp, @message
    | parse @message '"stacktrace":"*"' as stacktrace
    | replace(stacktrace, /\\n/g, '\n') as formatted_stacktrace
    | display @timestamp, formatted_stacktrace, @message
    
  • 查询结果导入Grafana后,开启面板的Wrap lines选项,即可正常显示换行。

方法3:自定义字段格式化函数

  • 针对目标字段(如exception.stacktrace)添加字段覆盖:
    1. 选中该字段,点击Add override
    2. 选择Value formatting -> Custom
    3. 输入格式化函数:
      (value) => value.replace(/\\n/g, '\n')
      
    4. 开启面板的Wrap lines选项,完成设置。

内容的提问来源于stack exchange,提问作者mattmin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 13:21:28