Annotate

mod_log_json/mod_log_json.lua @ 3733:9a3d25311fd9

mod_log_json: Use filename from logging config
author Kim Alvefur <zash@zash.se>
date Sun, 03 Nov 2019 14:33:01 +0100
parent 3732:21cfbdaac767
child 3734:b8bd79c57040
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3732
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local pack = require "util.table".pack;
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local json = require "util.json";
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local array = require "util.array";
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local datetime = require "util.datetime".datetime;
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 module:set_global();
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local function sink_maker(config)
3733
9a3d25311fd9 mod_log_json: Use filename from logging config
Kim Alvefur <zash@zash.se>
parents: 3732
diff changeset
9 local logfile = io.open(config.filename, "a");
3732
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 logfile:setvbuf("no");
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 return function (source, level, message, ...)
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local args = pack(...);
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 for i = 1, args.n do
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 if args[i] == nil then
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 args[i] = json.null;
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 elseif type(args[i]) ~= "string" or type(args[i]) ~= "number" then
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 args[i] = tostring(args[i]);
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 end
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 args.n = nil;
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local payload = {
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 datetime = datetime(),
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 source = source,
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 level = level,
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 message = message,
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 args = array(args);
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 };
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 logfile:write(json.encode(payload), "\n");
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
21cfbdaac767 mod_log_json: JSON log sink
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 require"core.loggingmanager".register_sink_type("json", sink_maker);