Mercurial > prosody-modules
changeset 3746:bc865568ff02
mod_log_json: Add UDP support
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 13 Nov 2019 17:51:28 +0100 (2019-11-13) |
parents | e43a82ddde12 |
children | f288177f4c5d |
files | mod_log_json/README.markdown mod_log_json/mod_log_json.lua |
diffstat | 2 files changed, 26 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/mod_log_json/README.markdown Wed Nov 13 17:51:08 2019 +0100 +++ b/mod_log_json/README.markdown Wed Nov 13 17:51:28 2019 +0100 @@ -16,6 +16,16 @@ } ``` +## UDP + +Alternatively, it can send logs via UDP: + +```lua +log = { + { to = "json", udp_host = "10.1.2.3", udp_port = "9999" }; +} +``` + Format ======
--- a/mod_log_json/mod_log_json.lua Wed Nov 13 17:51:08 2019 +0100 +++ b/mod_log_json/mod_log_json.lua Wed Nov 13 17:51:28 2019 +0100 @@ -2,12 +2,25 @@ local json = require "util.json"; local array = require "util.array"; local datetime = require "util.datetime".datetime; +local socket = require "socket"; module:set_global(); local function sink_maker(config) - local logfile = io.open(config.filename, "a+"); - logfile:setvbuf("no"); + local send = function () end + if config.filename then + local logfile = io.open(config.filename, "a+"); + logfile:setvbuf("no"); + function send(payload) + logfile:write(payload, "\n"); + end + elseif config.udp_host and config.udp_port then + local conn = socket.udp(); + conn:connect(config.udphost, config.udpport); + function send(payload) + conn:send(payload); + end + end return function (source, level, message, ...) local args = pack(...); for i = 1, args.n do @@ -25,7 +38,7 @@ message = message, args = array(args); }; - logfile:write(json.encode(payload), "\n"); + send(json.encode(payload)); end end