Mercurial > prosody-modules
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 |
parents | 21cfbdaac767 |
children | b8bd79c57040 |
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) | |
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 | 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); |