写入 JSON 日志文件的格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10699953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format for writing a JSON log file?
提问by chiborg
Are there any format standards for writing and parsing JSONlog files?
是否有写入和解析JSON日志文件的格式标准?
The problem I see is that you can't have a "pure" JSONlog file since you need matching brackets and trailing commas are forbidden. So while the following may be written by an application, it can't be parsed by standard JSON parsers:
我看到的问题是你不能有一个“纯”的JSON日志文件,因为你需要匹配的括号和尾随逗号是被禁止的。因此,虽然以下内容可能由应用程序编写,但无法通过标准解析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?
因此,您必须有一些关于如何以解析器可以处理它们的方式构建日志文件的约定。最简单的事情是“每行一个日志消息对象,字符串值中的换行符被转义”。是否有任何现有的标准和工具?
采纳答案by HerbCSO
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.
您不会为每个 FILE 编写单个 JSON 对象,而是要为每个 LINE 编写一个 JSON 对象。然后可以单独解析每一行。您无需担心尾随逗号并将整个对象集括在括号等中。请参阅http://blog.nodejs.org/2012/03/28/service-logging-in-json-with- bunyan/以获得更详细的解释。
Also check out Fluentd http://fluentd.org/for a neat toolset to work with.
还可以查看 Fluentd http://fluentd.org/以获得可以使用的简洁工具集。
Edit: this format is now called JSONLines or jsonlas pointed out by @Mnebuerquo below - see http://jsonlines.org/
编辑:此格式现在称为 JSONLines 或jsonl如下面的@Mnebuerquo所指出的 - 请参阅http://jsonlines.org/
回答by chad
gem log_formatteris the ruby choice, as the formatter group, now support json formatter for ruby and log4r.
gemlog_formatter是 ruby 的选择,作为格式化程序组,现在支持 ruby 和 log4r 的 json 格式化程序。
simple to get stated for ruby.
很容易为 ruby 声明。
gem 'log_formatter'
require 'log_formatter'
require 'log_formatter/ruby_json_formatter'
logger.debug({data: "test data", author: 'chad'})
result
结果
{
"source": "examples",
"data": "test data",
"author": "chad",
"log_level": "DEBUG",
"log_type": null,
"log_app": "app",
"log_timestamp": "2016-08-25T15:34:25+08:00"
}
for log4r:
对于 log4r:
require 'log4r'
require 'log_formatter'
require 'log_formatter/log4r_json_formatter'
logger = Log4r::Logger.new('Log4RTest')
outputter = Log4r::StdoutOutputter.new(
"console",
:formatter => Log4r::JSONFormatter::Base.new
)
logger.add(outputter)
logger.debug( {data: "test data", author: 'chad'} )
Advance usage: README
高级用法:README
Full Example Code: examples
完整示例代码:示例

