Mercurial > prosody-modules
comparison mod_log_json/mod_log_json.lua @ 3746:bc865568ff02
mod_log_json: Add UDP support
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 13 Nov 2019 17:51:28 +0100 |
parents | b8bd79c57040 |
children | f288177f4c5d |
comparison
equal
deleted
inserted
replaced
3745:e43a82ddde12 | 3746:bc865568ff02 |
---|---|
1 local pack = require "util.table".pack; | 1 local pack = require "util.table".pack; |
2 local json = require "util.json"; | 2 local json = require "util.json"; |
3 local array = require "util.array"; | 3 local array = require "util.array"; |
4 local datetime = require "util.datetime".datetime; | 4 local datetime = require "util.datetime".datetime; |
5 local socket = require "socket"; | |
5 | 6 |
6 module:set_global(); | 7 module:set_global(); |
7 | 8 |
8 local function sink_maker(config) | 9 local function sink_maker(config) |
9 local logfile = io.open(config.filename, "a+"); | 10 local send = function () end |
10 logfile:setvbuf("no"); | 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(); | |
19 conn:connect(config.udphost, config.udpport); | |
20 function send(payload) | |
21 conn:send(payload); | |
22 end | |
23 end | |
11 return function (source, level, message, ...) | 24 return function (source, level, message, ...) |
12 local args = pack(...); | 25 local args = pack(...); |
13 for i = 1, args.n do | 26 for i = 1, args.n do |
14 if args[i] == nil then | 27 if args[i] == nil then |
15 args[i] = json.null; | 28 args[i] = json.null; |
23 source = source, | 36 source = source, |
24 level = level, | 37 level = level, |
25 message = message, | 38 message = message, |
26 args = array(args); | 39 args = array(args); |
27 }; | 40 }; |
28 logfile:write(json.encode(payload), "\n"); | 41 send(json.encode(payload)); |
29 end | 42 end |
30 end | 43 end |
31 | 44 |
32 require"core.loggingmanager".register_sink_type("json", sink_maker); | 45 require"core.loggingmanager".register_sink_type("json", sink_maker); |