Comparison

mod_log_json/mod_log_json.lua @ 3732:21cfbdaac767

mod_log_json: JSON log sink
author Kim Alvefur <zash@zash.se>
date Thu, 18 Oct 2018 15:41:52 +0200
child 3733:9a3d25311fd9
comparison
equal deleted inserted replaced
3731:406b32b50457 3732:21cfbdaac767
1 local pack = require "util.table".pack;
2 local json = require "util.json";
3 local array = require "util.array";
4 local datetime = require "util.datetime".datetime;
5
6 module:set_global();
7
8 local function sink_maker(config)
9 local logfile = io.open("blah.json", "a");
10 logfile:setvbuf("no");
11 return function (source, level, message, ...)
12 local args = pack(...);
13 for i = 1, args.n do
14 if args[i] == nil then
15 args[i] = json.null;
16 elseif type(args[i]) ~= "string" or type(args[i]) ~= "number" then
17 args[i] = tostring(args[i]);
18 end
19 end
20 args.n = nil;
21 local payload = {
22 datetime = datetime(),
23 source = source,
24 level = level,
25 message = message,
26 args = array(args);
27 };
28 logfile:write(json.encode(payload), "\n");
29 end
30 end
31
32 require"core.loggingmanager".register_sink_type("json", sink_maker);