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, rep = string.format, string.rep;
13 local tostring, setmetatable, rawset, pairs, ipairs, type =
14 tostring, setmetatable, rawset, pairs, ipairs, type;
15 local io_open, io_write = io.open, io.write;
16 local math_max, rep = math.max, string.rep;
17 local os_date, os_getenv = os.date, os.getenv;
18 local getstyle, setstyle = require "util.termcolours".getstyle, require "util.termcolours".setstyle;
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");
32 module "loggingmanager"
34 -- The log config used if none specified in the config file (see reload_logging for initialization)
35 local default_logging;
36 local default_file_logging;
37 local default_timestamp = "%b %d %H:%M:%S";
38 -- The actual config loggingmanager is using
41 local apply_sink_rules;
42 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
44 local logging_levels = { "debug", "info", "warn", "error" }
46 -- Put a rule into action. Requires that the sink type has already been registered.
47 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
48 local function add_rule(sink_config)
49 local sink_maker = log_sink_types[sink_config.to];
51 if sink_config.levels and not sink_config.source then
53 local sink = sink_maker(sink_config);
55 -- Set sink for all chosen levels
56 for level in pairs(get_levels(sink_config.levels)) do
57 logger.add_level_sink(level, sink);
59 elseif sink_config.source and not sink_config.levels then
60 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
61 elseif sink_config.source and sink_config.levels then
62 local levels = get_levels(sink_config.levels);
63 local sink = sink_maker(sink_config);
64 logger.add_name_sink(sink_config.source,
65 function (name, level, ...)
67 return sink(name, level, ...);
73 local sink = sink_maker(sink_config);
75 -- Set sink for all levels
76 for _, level in pairs(logging_levels) do
77 logger.add_level_sink(level, sink);
85 -- Search for all rules using a particular sink type, and apply
86 -- them. Called automatically when a new sink type is added to
87 -- the log_sink_types table.
88 function apply_sink_rules(sink_type)
89 if type(logging_config) == "table" then
91 for _, level in ipairs(logging_levels) do
92 if type(logging_config[level]) == "string" then
93 local value = logging_config[level];
94 if sink_type == "file" then
99 levels = { min = level };
101 elseif value == "*"..sink_type then
104 levels = { min = level };
110 for _, sink_config in ipairs(logging_config) do
111 if (type(sink_config) == "table" and sink_config.to == sink_type) then
112 add_rule(sink_config);
113 elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
114 add_rule({ levels = { min = "debug" }, to = sink_type });
117 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
118 -- User specified simply a filename, and the "file" sink type
120 for _, sink_config in pairs(default_file_logging) do
121 sink_config.filename = logging_config;
122 add_rule(sink_config);
123 sink_config.filename = nil;
125 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
126 -- Log all levels (debug+) to this sink
127 add_rule({ levels = { min = "debug" }, to = sink_type });
133 --- Helper function to get a set of levels given a "criteria" table
134 function get_levels(criteria, set)
136 if type(criteria) == "string" then
137 set[criteria] = true;
140 local min, max = criteria.min, criteria.max;
143 for _, level in ipairs(logging_levels) do
147 elseif max == level then
156 for _, level in ipairs(criteria) do
162 -- Initialize config, etc. --
163 function reload_logging()
164 local old_sink_types = {};
166 for name, sink_maker in pairs(log_sink_types) do
167 old_sink_types[name] = sink_maker;
168 log_sink_types[name] = nil;
173 local debug_mode = config.get("*", "core", "debug");
175 default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
176 default_file_logging = {
177 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
179 default_timestamp = "%b %d %H:%M:%S";
181 logging_config = config.get("*", "core", "log") or default_logging;
184 for name, sink_maker in pairs(old_sink_types) do
185 log_sink_types[name] = sink_maker;
188 prosody.events.fire_event("logging-reloaded");
192 prosody.events.add_handler("config-reloaded", reload_logging);
194 --- Definition of built-in logging sinks ---
196 -- Null sink, must enter log_sink_types *first*
197 function log_sink_types.nowhere()
198 return function () return false; end;
201 -- Column width for "source" (used by stdout and console)
202 local sourcewidth = 20;
204 function log_sink_types.stdout(config)
205 local timestamps = config.timestamps;
207 if timestamps == true then
208 timestamps = default_timestamp; -- Default format
211 return function (name, level, message, ...)
212 sourcewidth = math_max(#name+2, sourcewidth);
213 local namelen = #name;
215 io_write(os_date(timestamps), " ");
218 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
220 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
226 local do_pretty_printing = true;
228 local logstyles = {};
229 if do_pretty_printing then
230 logstyles["info"] = getstyle("bold");
231 logstyles["warn"] = getstyle("bold", "yellow");
232 logstyles["error"] = getstyle("bold", "red");
234 function log_sink_types.console(config)
235 -- Really if we don't want pretty colours then just use plain stdout
236 if not do_pretty_printing then
237 return log_sink_types.stdout(config);
240 local timestamps = config.timestamps;
242 if timestamps == true then
243 timestamps = default_timestamp; -- Default format
246 return function (name, level, message, ...)
247 sourcewidth = math_max(#name+2, sourcewidth);
248 local namelen = #name;
251 io_write(os_date(timestamps), " ");
253 io_write(name, rep(" ", sourcewidth-namelen));
254 setstyle(logstyles[level]);
258 io_write("\t", format(message, ...), "\n");
260 io_write("\t", message, "\n");
266 local empty_function = function () end;
267 function log_sink_types.file(config)
268 local log = config.filename;
269 local logfile = io_open(log, "a+");
271 return empty_function;
273 local write, flush = logfile.write, logfile.flush;
275 local timestamps = config.timestamps;
277 if timestamps == nil or timestamps == true then
278 timestamps = default_timestamp; -- Default format
281 return function (name, level, message, ...)
283 write(logfile, os_date(timestamps), " ");
286 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
288 write(logfile, name, "\t" , level, "\t", message, "\n");
294 function register_sink_type(name, sink_maker)
295 local old_sink_maker = log_sink_types[name];
296 log_sink_types[name] = sink_maker;
297 return old_sink_maker;