Mercurial > prosody-modules
annotate mod_log_json/mod_log_json.lua @ 3734:b8bd79c57040
mod_log_json: Open file in read+append mode
Dunno why but this is what core.loggingmanager does
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 03 Nov 2019 14:33:03 +0100 |
parents | 9a3d25311fd9 |
children | bc865568ff02 |
rev | line source |
---|---|
3732 | 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) | |
3734
b8bd79c57040
mod_log_json: Open file in read+append mode
Kim Alvefur <zash@zash.se>
parents:
3733
diff
changeset
|
9 local logfile = io.open(config.filename, "a+"); |
3732 | 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); |