# HG changeset patch # User Kim Alvefur # Date 1539870112 -7200 # Node ID 21cfbdaac7679608fe9f3436fdb0de86ffb13f6c # Parent 406b32b50457860331ed793075e0a513f528ab15 mod_log_json: JSON log sink diff -r 406b32b50457 -r 21cfbdaac767 mod_log_json/mod_log_json.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_log_json/mod_log_json.lua Thu Oct 18 15:41:52 2018 +0200 @@ -0,0 +1,32 @@ +local pack = require "util.table".pack; +local json = require "util.json"; +local array = require "util.array"; +local datetime = require "util.datetime".datetime; + +module:set_global(); + +local function sink_maker(config) + local logfile = io.open("blah.json", "a"); + logfile:setvbuf("no"); + return function (source, level, message, ...) + local args = pack(...); + for i = 1, args.n do + if args[i] == nil then + args[i] = json.null; + elseif type(args[i]) ~= "string" or type(args[i]) ~= "number" then + args[i] = tostring(args[i]); + end + end + args.n = nil; + local payload = { + datetime = datetime(), + source = source, + level = level, + message = message, + args = array(args); + }; + logfile:write(json.encode(payload), "\n"); + end +end + +require"core.loggingmanager".register_sink_type("json", sink_maker);