Mercurial > prosody-modules
annotate mod_log_json/mod_log_json.lua @ 4651:8231774f5bfd
mod_cloud_notify_encrypted: Ensure body substring remains valid UTF-8
The `body:sub()` call risks splitting the string in the middle of a
multi-byte UTF-8 sequence. This should have been caught by util.stanza
validation, but that would have caused some havoc, at the very least causing
the notification to not be sent.
There have been no reports of this happening. Likely because this module
isn't widely deployed among users with languages that use many longer UTF-8
sequences.
The util.encodings.utf8.valid() function is O(n) where only the last
sequence really needs to be checked, but it's in C and expected to be fast.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 22 Aug 2021 13:22:59 +0200 |
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); |