Changeset

4462:4356088ad675

mod_log_json: allow logging of formatted message This is for logging pipelines which can not or do not want to interpret sprintf-style strings but still need the complete string for search or whatever.
author Jonas Schäfer <jonas@wielicki.name>
date Mon, 22 Feb 2021 16:08:55 +0100
parents 4461:b9c1216987ae
children 4463:8b8246031a5e
files mod_log_json/README.markdown mod_log_json/mod_log_json.lua
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_log_json/README.markdown	Mon Feb 22 17:56:27 2021 +0100
+++ b/mod_log_json/README.markdown	Mon Feb 22 16:08:55 2021 +0100
@@ -70,3 +70,22 @@
 `args`
 :   Array of extra arguments, corresponding to `printf`-style `%s`
     formatting in the `message`.
+
+Formatted message
+-----------------
+
+If desired, at the obvious expense of performance, the formatted version of
+the string can be included in the JSON object by specifying the `formatted_as`
+key in the logger config:
+
+``` {.lua}
+log = {
+  -- ... other sinks ...
+  { to = "json", formatted_as = "msg_formatted", filename = "/var/log/prosody/prosody.json" };
+}
+
+```
+
+This will expose the formatted message in the JSON as separate `msg_formatted`
+key. It is possible to override existing keys using this (for example, the
+`message` key), but not advisible.
--- a/mod_log_json/mod_log_json.lua	Mon Feb 22 17:56:27 2021 +0100
+++ b/mod_log_json/mod_log_json.lua	Mon Feb 22 16:08:55 2021 +0100
@@ -21,6 +21,8 @@
 			conn:send(payload);
 		end
 	end
+	local format = require "util.format".format;
+	local do_format = config.formatted_as or false;
 	return function (source, level, message, ...)
 		local args = pack(...);
 		for i = 1, args.n do
@@ -38,6 +40,9 @@
 			message = message,
 			args = array(args);
 		};
+		if do_format then
+			payload[do_format] = format(message, ...)
+		end
 		send(json.encode(payload));
 	end
 end