简体   繁体   English

如何过滤 Grafana-Loki 中的日志?

[英]How to filter logs in Grafana-Loki?

I use the last versions of:我使用的最新版本:

  • Prometheus普罗米修斯
  • prometheus-cpp library prometheus-cpp 库
  • Grafana格拉法纳
  • Loki洛基
  • Promtail长尾

in Windows 10.在 Windows 10 中。

So I just start bin files of these applications.所以我只是启动这些应用程序的 bin 文件。 And I get my logfile.log in Grafana panel.我在 Grafana 面板中得到了我的logfile.log

带日志的 Grafana 面板

There are lines marked [INFO] in the log file.日志文件中有标记为 [INFO] 的行。
There may also be an [ERROR] mark.也可能有 [ERROR] 标记。 I want to be able to filter rows in a panel.我希望能够过滤面板中的行。 For example, how can I display only lines marked with [ERROR] in the panel?例如,如何在面板中只显示标有 [ERROR] 的行?

Loki indexes logs by their labels. Loki 按标签索引日志。 When you query Loki you must first specify a stream filter in the format {<label><operator><value>} .当您查询 Loki 时,您必须首先指定格式为{<label><operator><value>}的流过滤器。 I'm guessing you're already aware of this since you've got your logs being displayed there, probably with something like {app="vocoder"} .我猜你已经意识到了这一点,因为你的日志显示在那里,可能像{app="vocoder"} You can then filter the stream of logs by plain text or regular expression with something like {app="vocoder"} |= "[ERROR]" (see the Log queries documentation ).然后,您可以使用诸如{app="vocoder"} |= "[ERROR]"之类的纯文本或正则表达式过滤日志流(请参阅日志查询文档)。 If you configure your Promtail scrapers to extract additional labels from the log messages (see Labels documentation andScraping documentation ), then you can also write stream filter expressions based on this, or filter based on this labels as part of your filter pipeline: {app="vocoder"} | level = "error"如果您将 Promtail 抓取工具配置为从日志消息中提取其他标签(请参阅标签文档抓取文档),那么您还可以基于此编写流过滤器表达式,或基于此标签进行过滤器作为过滤器管道的一部分: {app="vocoder"} | level = "error" {app="vocoder"} | level = "error" . {app="vocoder"} | level = "error"

One can use line filters to accomplish that:可以使用线路过滤器来实现这一点:

|= : Log line contains string.
!= : Log line does not contain string.
|~ : Log line matches regular expression.
!~ : Log line does not match regular expression.

For instance:例如:

A given log line from target cluster and app contains the string ERROR :来自目标集群和应用程序的给定日志行包含字符串ERROR

{cluster="my-dev-cluster", app="my-app"} |= "ERROR"

A given log line from target cluster and app contains the string ERROR but does not contain the string "TIMEOUT":来自目标集群和应用程序的给定日志行包含字符串ERROR但不包含字符串“TIMEOUT”:

{cluster="my-dev-cluster", app="my-app"} |= "ERROR" != "TIMEOUT"

See more in this cheat-sheet .在此备忘单中查看更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM