# HG changeset patch # User Kim Alvefur # Date 1573663888 -3600 # Node ID bc865568ff02500db13693b02e113fe5e6454915 # Parent e43a82ddde1260b10a59dc7f7b6cd7f25035fef3 mod_log_json: Add UDP support diff -r e43a82ddde12 -r bc865568ff02 mod_log_json/README.markdown --- 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 ====== diff -r e43a82ddde12 -r bc865568ff02 mod_log_json/mod_log_json.lua --- 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