# HG changeset patch # User Jonas Schäfer # Date 1614006535 -3600 # Node ID 4356088ad675f98dda340aa0ded571db4c2e14fd # Parent b9c1216987aec884c9b989b2d6ce2ca5f1d64aab 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. diff -r b9c1216987ae -r 4356088ad675 mod_log_json/README.markdown --- 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. diff -r b9c1216987ae -r 4356088ad675 mod_log_json/mod_log_json.lua --- 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