Software /
code /
prosody-modules
Annotate
mod_log_json/mod_log_json.lua @ 5939:db5128d1a16c
mod_report_forward: Fix traceback when reporting a specific message (thanks singpolyma)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 30 May 2024 17:55:07 +0100 |
parent | 4974:807007913f67 |
rev | line source |
---|---|
4974
807007913f67
mod_log_json: Prefer native Lua table.pack over Prosody util.table one
Kim Alvefur <zash@zash.se>
parents:
4462
diff
changeset
|
1 local pack = table.pack or require "util.table".pack; |
3732 | 2 local json = require "util.json"; |
3 local array = require "util.array"; | |
4 local datetime = require "util.datetime".datetime; | |
3746 | 5 local socket = require "socket"; |
3732 | 6 |
7 module:set_global(); | |
8 | |
9 local function sink_maker(config) | |
3746 | 10 local send = function () end |
11 if config.filename then | |
12 local logfile = io.open(config.filename, "a+"); | |
13 logfile:setvbuf("no"); | |
14 function send(payload) | |
15 logfile:write(payload, "\n"); | |
16 end | |
17 elseif config.udp_host and config.udp_port then | |
18 local conn = socket.udp(); | |
3748
27abf3b6819a
mod_log_json: Use correct method to specify remote endpoint
Kim Alvefur <zash@zash.se>
parents:
3747
diff
changeset
|
19 conn:setpeername(config.udp_host, config.udp_port); |
3746 | 20 function send(payload) |
21 conn:send(payload); | |
22 end | |
23 end | |
4462
4356088ad675
mod_log_json: allow logging of formatted message
Jonas Schäfer <jonas@wielicki.name>
parents:
3758
diff
changeset
|
24 local format = require "util.format".format; |
4356088ad675
mod_log_json: allow logging of formatted message
Jonas Schäfer <jonas@wielicki.name>
parents:
3758
diff
changeset
|
25 local do_format = config.formatted_as or false; |
3732 | 26 return function (source, level, message, ...) |
27 local args = pack(...); | |
28 for i = 1, args.n do | |
29 if args[i] == nil then | |
30 args[i] = json.null; | |
31 elseif type(args[i]) ~= "string" or type(args[i]) ~= "number" then | |
32 args[i] = tostring(args[i]); | |
33 end | |
34 end | |
35 args.n = nil; | |
36 local payload = { | |
37 datetime = datetime(), | |
38 source = source, | |
39 level = level, | |
40 message = message, | |
41 args = array(args); | |
42 }; | |
4462
4356088ad675
mod_log_json: allow logging of formatted message
Jonas Schäfer <jonas@wielicki.name>
parents:
3758
diff
changeset
|
43 if do_format then |
4356088ad675
mod_log_json: allow logging of formatted message
Jonas Schäfer <jonas@wielicki.name>
parents:
3758
diff
changeset
|
44 payload[do_format] = format(message, ...) |
4356088ad675
mod_log_json: allow logging of formatted message
Jonas Schäfer <jonas@wielicki.name>
parents:
3758
diff
changeset
|
45 end |
3746 | 46 send(json.encode(payload)); |
3732 | 47 end |
48 end | |
49 | |
3758
900ea02ab00b
mod_log_json: Deregister log sink on unload
Kim Alvefur <zash@zash.se>
parents:
3748
diff
changeset
|
50 function module.unload() |
900ea02ab00b
mod_log_json: Deregister log sink on unload
Kim Alvefur <zash@zash.se>
parents:
3748
diff
changeset
|
51 -- deregister |
900ea02ab00b
mod_log_json: Deregister log sink on unload
Kim Alvefur <zash@zash.se>
parents:
3748
diff
changeset
|
52 require"core.loggingmanager".register_sink_type("json", nil); |
900ea02ab00b
mod_log_json: Deregister log sink on unload
Kim Alvefur <zash@zash.se>
parents:
3748
diff
changeset
|
53 end |
900ea02ab00b
mod_log_json: Deregister log sink on unload
Kim Alvefur <zash@zash.se>
parents:
3748
diff
changeset
|
54 |
3732 | 55 require"core.loggingmanager".register_sink_type("json", sink_maker); |