Comparison

core/loggingmanager.lua @ 11643:f534eeee1552

core.loggingmanager: Pretty-print logged XML snippets in console This replaces an earlier method in a private extension that logged pretty-printed XML, which broke due to the escaping added in util.format
author Kim Alvefur <zash@zash.se>
date Tue, 29 Jun 2021 16:07:57 +0200
parent 11641:b03053ec6dcf
child 11657:46fa1b939e88
comparison
equal deleted inserted replaced
11642:7f2dee4249aa 11643:f534eeee1552
12 local stdout = io.stdout; 12 local stdout = io.stdout;
13 local io_open = io.open; 13 local io_open = io.open;
14 local math_max, rep = math.max, string.rep; 14 local math_max, rep = math.max, string.rep;
15 local os_date = os.date; 15 local os_date = os.date;
16 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; 16 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
17 local st = require "util.stanza";
17 18
18 local config = require "core.configmanager"; 19 local config = require "core.configmanager";
19 local logger = require "util.logger"; 20 local logger = require "util.logger";
20 21
21 local have_pposix, pposix = pcall(require, "util.pposix"); 22 local have_pposix, pposix = pcall(require, "util.pposix");
212 end 213 end
213 log_sink_types.stdout = log_to_stdout; 214 log_sink_types.stdout = log_to_stdout;
214 215
215 local do_pretty_printing = true; 216 local do_pretty_printing = true;
216 217
217 local logstyles; 218 local logstyles, pretty;
218 if do_pretty_printing then 219 if do_pretty_printing then
219 logstyles = {}; 220 logstyles = {};
220 logstyles["info"] = getstyle("bold"); 221 logstyles["info"] = getstyle("bold");
221 logstyles["warn"] = getstyle("bold", "yellow"); 222 logstyles["warn"] = getstyle("bold", "yellow");
222 logstyles["error"] = getstyle("bold", "red"); 223 logstyles["error"] = getstyle("bold", "red");
224
225 pretty = st.pretty_print;
223 end 226 end
224 227
225 local function log_to_console(sink_config) 228 local function log_to_console(sink_config)
226 -- Really if we don't want pretty colours then just use plain stdout 229 -- Really if we don't want pretty colours then just use plain stdout
230 if not do_pretty_printing then
231 return log_to_stdout(sink_config);
232 end
233 sink_config.filter = pretty;
227 local logstdout = log_to_stdout(sink_config); 234 local logstdout = log_to_stdout(sink_config);
228 if not do_pretty_printing then
229 return logstdout;
230 end
231 return function (name, level, message, ...) 235 return function (name, level, message, ...)
232 local logstyle = logstyles[level]; 236 local logstyle = logstyles[level];
233 if logstyle then 237 if logstyle then
234 level = getstring(logstyle, level); 238 level = getstring(logstyle, level);
235 end 239 end