Mercurial > prosody-hg
comparison core/loggingmanager.lua @ 11641:b03053ec6dcf
core.loggingmanager: Support passing log messages trough a filter
This will be used by the console logger for pretty printing.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 29 Jun 2021 16:05:35 +0200 |
| parents | 1460c4966262 |
| children | f534eeee1552 |
comparison
equal
deleted
inserted
replaced
| 11640:51598e46e136 | 11641:b03053ec6dcf |
|---|---|
| 33 | 33 |
| 34 local apply_sink_rules; | 34 local apply_sink_rules; |
| 35 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; }); | 35 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; }); |
| 36 local get_levels; | 36 local get_levels; |
| 37 local logging_levels = { "debug", "info", "warn", "error" } | 37 local logging_levels = { "debug", "info", "warn", "error" } |
| 38 | |
| 39 local function id(x) return x end | |
| 38 | 40 |
| 39 -- Put a rule into action. Requires that the sink type has already been registered. | 41 -- Put a rule into action. Requires that the sink type has already been registered. |
| 40 -- This function is called automatically when a new sink type is added [see apply_sink_rules()] | 42 -- This function is called automatically when a new sink type is added [see apply_sink_rules()] |
| 41 local function add_rule(sink_config) | 43 local function add_rule(sink_config) |
| 42 local sink_maker = log_sink_types[sink_config.to]; | 44 local sink_maker = log_sink_types[sink_config.to]; |
| 182 logfile:setvbuf(sink_config.buffer_mode or "line"); | 184 logfile:setvbuf(sink_config.buffer_mode or "line"); |
| 183 end | 185 end |
| 184 | 186 |
| 185 -- Column width for "source" (used by stdout and console) | 187 -- Column width for "source" (used by stdout and console) |
| 186 local sourcewidth = sink_config.source_width; | 188 local sourcewidth = sink_config.source_width; |
| 189 local filter = sink_config.filter or id; | |
| 187 | 190 |
| 188 if sourcewidth then | 191 if sourcewidth then |
| 189 return function (name, level, message, ...) | 192 return function (name, level, message, ...) |
| 190 sourcewidth = math_max(#name+2, sourcewidth); | 193 sourcewidth = math_max(#name+2, sourcewidth); |
| 191 write(logfile, timestamps and os_date(timestamps) or "", name, rep(" ", sourcewidth-#name), level, "\t", format(message, ...), "\n"); | 194 write(logfile, timestamps and os_date(timestamps) or "", name, rep(" ", sourcewidth-#name), level, "\t", filter(format(message, ...)), "\n"); |
| 192 end | 195 end |
| 193 else | 196 else |
| 194 return function (name, level, message, ...) | 197 return function (name, level, message, ...) |
| 195 write(logfile, timestamps and os_date(timestamps) or "", name, "\t", level, "\t", format(message, ...), "\n"); | 198 write(logfile, timestamps and os_date(timestamps) or "", name, "\t", level, "\t", filter(format(message, ...)), "\n"); |
| 196 end | 199 end |
| 197 end | 200 end |
| 198 end | 201 end |
| 199 log_sink_types.file = log_to_file; | 202 log_sink_types.file = log_to_file; |
| 200 | 203 |
