diff options
Diffstat (limited to 'core/loggingmanager.lua')
-rw-r--r-- | core/loggingmanager.lua | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/core/loggingmanager.lua b/core/loggingmanager.lua index cfa8246a..d8e557f9 100644 --- a/core/loggingmanager.lua +++ b/core/loggingmanager.lua @@ -14,10 +14,14 @@ local io_open = io.open; local math_max, rep = math.max, string.rep; local os_date = os.date; local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; +local st = require "util.stanza"; local config = require "core.configmanager"; local logger = require "util.logger"; +local have_pposix, pposix = pcall(require, "util.pposix"); +have_pposix = have_pposix and pposix._VERSION == "0.4.0"; + local _ENV = nil; -- luacheck: std none @@ -33,6 +37,8 @@ local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset local get_levels; local logging_levels = { "debug", "info", "warn", "error" } +local function id(x) return x end + -- Put a rule into action. Requires that the sink type has already been registered. -- This function is called automatically when a new sink type is added [see apply_sink_rules()] local function add_rule(sink_config) @@ -181,15 +187,16 @@ local function log_to_file(sink_config, logfile) -- Column width for "source" (used by stdout and console) local sourcewidth = sink_config.source_width; + local filter = sink_config.filter or id; if sourcewidth then return function (name, level, message, ...) sourcewidth = math_max(#name+2, sourcewidth); - write(logfile, timestamps and os_date(timestamps) or "", name, rep(" ", sourcewidth-#name), level, "\t", format(message, ...), "\n"); + write(logfile, timestamps and os_date(timestamps) or "", name, rep(" ", sourcewidth-#name), level, "\t", filter(format(message, ...)), "\n"); end else return function (name, level, message, ...) - write(logfile, timestamps and os_date(timestamps) or "", name, "\t", level, "\t", format(message, ...), "\n"); + write(logfile, timestamps and os_date(timestamps) or "", name, "\t", level, "\t", filter(format(message, ...)), "\n"); end end end @@ -206,22 +213,25 @@ local function log_to_stdout(sink_config) end log_sink_types.stdout = log_to_stdout; -local do_pretty_printing = true; +local do_pretty_printing = not have_pposix or pposix.isatty(stdout); -local logstyles; +local logstyles, pretty; if do_pretty_printing then logstyles = {}; logstyles["info"] = getstyle("bold"); logstyles["warn"] = getstyle("bold", "yellow"); logstyles["error"] = getstyle("bold", "red"); + + pretty = st.pretty_print; end local function log_to_console(sink_config) -- Really if we don't want pretty colours then just use plain stdout - local logstdout = log_to_stdout(sink_config); if not do_pretty_printing then - return logstdout; + return log_to_stdout(sink_config); end + sink_config.filter = pretty; + local logstdout = log_to_stdout(sink_config); return function (name, level, message, ...) local logstyle = logstyles[level]; if logstyle then @@ -232,6 +242,22 @@ local function log_to_console(sink_config) end log_sink_types.console = log_to_console; +if have_pposix then + local syslog_opened; + local function log_to_syslog(sink_config) -- luacheck: ignore 212/sink_config + if not syslog_opened then + local facility = sink_config.syslog_facility or config.get("*", "syslog_facility"); + pposix.syslog_open(sink_config.syslog_name or "prosody", facility); + syslog_opened = true; + end + local syslog = pposix.syslog_log; + return function (name, level, message, ...) + syslog(level, name, format(message, ...)); + end; + end + log_sink_types.syslog = log_to_syslog; +end + local function register_sink_type(name, sink_maker) local old_sink_maker = log_sink_types[name]; log_sink_types[name] = sink_maker; |