2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local format = string.format;
11 local setmetatable, rawset, pairs, ipairs, type =
12 setmetatable, rawset, pairs, ipairs, type;
13 local stdout = io.stdout;
14 local io_open = io.open;
15 local math_max, rep = math.max, string.rep;
16 local os_date = os.date;
17 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
19 -- COMPAT: This should no longer be needed since the addition of setvbuf calls
20 if os.getenv("__FLUSH_LOG") then
21 local io_flush = io.flush;
22 local _io_write = io_write;
23 io_write = function(...) _io_write(...); io_flush(); end
26 local config = require "core.configmanager";
27 local logger = require "util.logger";
28 local prosody = prosody;
30 _G.log = logger.init("general");
31 prosody.log = logger.init("general");
35 -- The log config used if none specified in the config file (see reload_logging for initialization)
36 local default_logging;
37 local default_file_logging;
38 local default_timestamp = "%b %d %H:%M:%S ";
39 -- The actual config loggingmanager is using
42 local apply_sink_rules;
43 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
45 local logging_levels = { "debug", "info", "warn", "error" }
47 -- Put a rule into action. Requires that the sink type has already been registered.
48 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
49 local function add_rule(sink_config)
50 local sink_maker = log_sink_types[sink_config.to];
51 if not sink_maker then
52 return; -- No such sink type
56 local sink = sink_maker(sink_config);
58 -- Set sink for all chosen levels
59 for level in pairs(get_levels(sink_config.levels or logging_levels)) do
60 logger.add_level_sink(level, sink);
64 -- Search for all rules using a particular sink type, and apply
65 -- them. Called automatically when a new sink type is added to
66 -- the log_sink_types table.
67 function apply_sink_rules(sink_type)
68 if type(logging_config) == "table" then
70 for _, level in ipairs(logging_levels) do
71 if type(logging_config[level]) == "string" then
72 local value = logging_config[level];
73 if sink_type == "file" and not value:match("^%*") then
78 levels = { min = level };
80 elseif value == "*"..sink_type then
83 levels = { min = level };
89 for _, sink_config in ipairs(logging_config) do
90 if (type(sink_config) == "table" and sink_config.to == sink_type) then
91 add_rule(sink_config);
92 elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
93 add_rule({ levels = { min = "debug" }, to = sink_type });
96 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
97 -- User specified simply a filename, and the "file" sink type
99 for _, sink_config in pairs(default_file_logging) do
100 sink_config.filename = logging_config;
101 add_rule(sink_config);
102 sink_config.filename = nil;
104 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
105 -- Log all levels (debug+) to this sink
106 add_rule({ levels = { min = "debug" }, to = sink_type });
112 --- Helper function to get a set of levels given a "criteria" table
113 function get_levels(criteria, set)
115 if type(criteria) == "string" then
116 set[criteria] = true;
119 local min, max = criteria.min, criteria.max;
122 for _, level in ipairs(logging_levels) do
126 elseif max == level then
135 for _, level in ipairs(criteria) do
141 -- Initialize config, etc. --
142 local function reload_logging()
143 local old_sink_types = {};
145 for name, sink_maker in pairs(log_sink_types) do
146 old_sink_types[name] = sink_maker;
147 log_sink_types[name] = nil;
152 local debug_mode = config.get("*", "debug");
154 default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
155 default_file_logging = {
156 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
159 logging_config = config.get("*", "log") or default_logging;
161 for name, sink_maker in pairs(old_sink_types) do
162 log_sink_types[name] = sink_maker;
165 prosody.events.fire_event("logging-reloaded");
169 prosody.events.add_handler("config-reloaded", reload_logging);
171 --- Definition of built-in logging sinks ---
173 -- Null sink, must enter log_sink_types *first*
174 local function log_to_nowhere()
175 return function () return false; end;
177 log_sink_types.nowhere = log_to_nowhere;
179 local function log_to_file(sink_config, logfile)
180 logfile = logfile or io_open(sink_config.filename, "a+");
182 return log_to_nowhere(sink_config);
184 local write = logfile.write;
186 local timestamps = sink_config.timestamps;
188 if timestamps == true then
189 timestamps = default_timestamp; -- Default format
190 elseif timestamps then
191 timestamps = timestamps .. " ";
194 if sink_config.buffer_mode ~= false then
195 logfile:setvbuf(sink_config.buffer_mode or "line");
198 -- Column width for "source" (used by stdout and console)
199 local sourcewidth = sink_config.source_width;
201 return function (name, level, message, ...)
203 sourcewidth = math_max(#name+2, sourcewidth);
204 name = name .. rep(" ", sourcewidth-#name);
209 write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", format(message, ...), "\n");
211 write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", message, "\n");
215 log_sink_types.file = log_to_file;
217 local function log_to_stdout(sink_config)
218 if not sink_config.timestamps then
219 sink_config.timestamps = false;
221 if sink_config.source_width == nil then
222 sink_config.source_width = 20;
224 return log_to_file(sink_config, stdout);
226 log_sink_types.stdout = log_to_stdout;
228 local do_pretty_printing = true;
231 if do_pretty_printing then
233 logstyles["info"] = getstyle("bold");
234 logstyles["warn"] = getstyle("bold", "yellow");
235 logstyles["error"] = getstyle("bold", "red");
238 local function log_to_console(sink_config)
239 -- Really if we don't want pretty colours then just use plain stdout
240 local logstdout = log_to_stdout(sink_config);
241 if not do_pretty_printing then
244 return function (name, level, message, ...)
245 local logstyle = logstyles[level];
247 level = getstring(logstyle, level);
249 return logstdout(name, level, message, ...);
252 log_sink_types.console = log_to_console;
254 local function register_sink_type(name, sink_maker)
255 local old_sink_maker = log_sink_types[name];
256 log_sink_types[name] = sink_maker;
257 return old_sink_maker;
261 reload_logging = reload_logging;
262 register_sink_type = register_sink_type;