diff 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
line wrap: on
line diff
--- 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