Mercurial > prosody-modules
annotate mod_log_json/mod_log_json.lua @ 4876:0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
In particular this transitions PEP nodes and data to be imported via mod_pep's
APIs, fixing issues with importing at runtime while PEP data may already be
live in RAM.
Next obvious candidate for this approach is rosters, so clients get immediate
roster pushes and other special handling (such as emitting subscribes to reach
the desired subscription state).
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 18 Jan 2022 17:01:18 +0000 |
parents | 4356088ad675 |
children | 807007913f67 |
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; | |
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); |