Format for writing a JSON log file? Format for writing a JSON log file? json json

Format for writing a JSON log file?


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.

Also check out Fluentd http://fluentd.org/ for a neat toolset to work with.

Edit: this format is now called JSONLines or jsonl as pointed out by @Mnebuerquo below - see http://jsonlines.org/


gem log_formatter is the ruby choice, as the formatter group, now support json formatter for ruby and log4r.

simple to get stated for 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:

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

Full Example Code: examples