Skip to content

Instantly share code, notes, and snippets.

@skurfuerst
Created July 31, 2014 13:28
Show Gist options
  • Save skurfuerst/77f2b12c31f5327b4eba to your computer and use it in GitHub Desktop.
Save skurfuerst/77f2b12c31f5327b4eba to your computer and use it in GitHub Desktop.
require "cjson"
local dt = require 'date_time'
-- Generic decoder for JSON logs. This will extract all JSON
-- keys and add them to the `Fields` variable of the created
-- Heka message.
--
-- Example use:
--
-- [NginxJsonLogDecoder]
-- type = "SandboxDecoder"
-- script_type = "lua"
-- filename = "/location/of/json_log_decode.lua"
--
-- [NginxJsonLogDecoder.config]
-- app = "some application"
--
-- [some-nginx-json-log-file]
-- type = "LogfileInput"
-- logfile = "/some/location/of/an/nginx/log/file.json.log"
-- decoder = "NginxJsonLogDecoder"
function process_message()
local raw_message = read_message("Payload")
local json = cjson.decode(raw_message)
if not json then
-- When plain text is found, ship it in it's raw form.
inject_message(raw_message)
return 0
end
local message = {
Type = "jsonLog"
}
message.Payload = json['message']
json['message'] = null
message.Hostname = json['HOSTNAME']
json['HOSTNAME'] = null
message.Logger = json['logger_name']
json['logger_name'] = null
message.Fields = json
-- 2014-07-31T11:56:34.305+02:00
local fields = dt.rfc3339:match(json["@timestamp"])
json["@timestamp"] = null
message.Timestamp = dt.time_to_ns(fields)
inject_message(message)
return 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment