是否有编写和解析JSON个日志(log)文件的格式标准?

我看到的问题是,你不能有一个"纯"JSON日志(log)文件,因为你需要匹配的括号和尾随逗号是禁止的.因此,虽然以下内容可能由应用程序编写,但标准JSON parsers:无法解析

[{date:'2012-01-01 02:00:01', severity:"ERROR", msg:"Foo failed"},
{date:'2012-01-01 02:04:02', severity:"INFO", msg:"Bar was successful"},
{date:'2012-01-01 02:10:12', severity:"DEBUG", msg:"Baz was notified"},

So you must have some conventions on how to structure your log files in a way that a parser can process them. The easiest thing would be "one log message object per line, newlines in string values are escaped". Are there any existing standards and tools?

推荐答案

You're not going to write a single JSON object per FILE, you're going to write a JSON object per LINE. Each line can then be parsed individually. You don't need to worry about trailing commas and have the whole set of objects enclosed by brackets, etc. See http://blog.nodejs.org/2012/03/28/service-logging-in-json-with-bunyan/ for a more detailed explanation of what this can look like.

另外,请查看Fluentd http://fluentd.org/,以获得一个整洁的工具集.

编辑:此格式现在称为JSONLines或jsonl,正如下面的@MnebuerQuo所指出的-请参见http://jsonlines.org/

Json相关问答推荐

从先前的REST调用创建动态JSON主体

使用Jolt库对多个数组进行嵌套循环

NiFi QueryRecord处理器- Select 可选的JSON属性

给定一个包含两个数组的JSON输入文件,如何使用Jolt将一个数组中的每个元素与另一个数组组合在一起?

无法访问id的第三级json

从 Inno Setup 中的 JSON 文件的每个对象中读取特定字符串

如何从 json 中获取单个元素?

在Flutter 中将 map 的 Json 转换为 list

判断golang中解析的json响应中是否存在所需的json键(不是值)

在 Perl Mojolicious 中呈现 JSON 时防止转义字符

如何在不解析的情况下在javascript中同步包含JSON数据?

Jackson Json:如何将数组转换为 JsonNode 和 ObjectNode?

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

Python - 如何将 JSON 文件转换为数据框

在自定义 JsonConverter 的 ReadJson 方法中处理空对象

使用 JSON.Net 的 C# 到 JSON 序列化

我可以使用空字符串作为对象标识符吗?

字符串格式 JSON 字符串给出 KeyError

强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

来自 Gson 的 JSON 字符串:删除双引号