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", "critical" }
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 if sink_type == "file" then
92 for _, level in ipairs(logging_levels) do
93 if type(logging_config[level]) == "string" then
96 filename = logging_config[level],
98 levels = { min = level },
104 for _, sink_config in pairs(logging_config) do
105 if (type(sink_config) == "table" and sink_config.to == sink_type) then
106 add_rule(sink_config);
107 elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
108 add_rule({ levels = { min = "debug" }, to = sink_type });
111 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
112 -- User specified simply a filename, and the "file" sink type
114 for _, sink_config in pairs(default_file_logging) do
115 sink_config.filename = logging_config;
116 add_rule(sink_config);
117 sink_config.filename = nil;
119 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
120 -- Log all levels (debug+) to this sink
121 add_rule({ levels = { min = "debug" }, to = sink_type });
127 --- Helper function to get a set of levels given a "criteria" table
128 function get_levels(criteria, set)
130 if type(criteria) == "string" then
131 set[criteria] = true;
134 local min, max = criteria.min, criteria.max;
137 for _, level in ipairs(logging_levels) do
141 elseif max == level then
150 for _, level in ipairs(criteria) do
156 -- Initialize config, etc. --
157 function reload_logging()
158 local old_sink_types = {};
160 for name, sink_maker in pairs(log_sink_types) do
161 old_sink_types[name] = sink_maker;
162 log_sink_types[name] = nil;
167 local debug_mode = config.get("*", "core", "debug");
169 default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
170 default_file_logging = {
171 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
173 default_timestamp = "%b %d %H:%M:%S";
175 logging_config = config.get("*", "core", "log") or default_logging;
178 for name, sink_maker in pairs(old_sink_types) do
179 log_sink_types[name] = sink_maker;
182 prosody.events.fire_event("logging-reloaded");
186 prosody.events.add_handler("config-reloaded", reload_logging);
188 --- Definition of built-in logging sinks ---
190 -- Null sink, must enter log_sink_types *first*
191 function log_sink_types.nowhere()
192 return function () return false; end;
195 -- Column width for "source" (used by stdout and console)
196 local sourcewidth = 20;
198 function log_sink_types.stdout()
199 local timestamps = config.timestamps;
201 if timestamps == true then
202 timestamps = default_timestamp; -- Default format
205 return function (name, level, message, ...)
206 sourcewidth = math_max(#name+2, sourcewidth);
207 local namelen = #name;
209 io_write(os_date(timestamps), " ");
212 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
214 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
220 local do_pretty_printing = true;
222 local logstyles = {};
223 if do_pretty_printing then
224 logstyles["info"] = getstyle("bold");
225 logstyles["warn"] = getstyle("bold", "yellow");
226 logstyles["error"] = getstyle("bold", "red");
228 function log_sink_types.console(config)
229 -- Really if we don't want pretty colours then just use plain stdout
230 if not do_pretty_printing then
231 return log_sink_types.stdout(config);
234 local timestamps = config.timestamps;
236 if timestamps == true then
237 timestamps = default_timestamp; -- Default format
240 return function (name, level, message, ...)
241 sourcewidth = math_max(#name+2, sourcewidth);
242 local namelen = #name;
245 io_write(os_date(timestamps), " ");
247 io_write(name, rep(" ", sourcewidth-namelen));
248 setstyle(logstyles[level]);
252 io_write("\t", format(message, ...), "\n");
254 io_write("\t", message, "\n");
260 local empty_function = function () end;
261 function log_sink_types.file(config)
262 local log = config.filename;
263 local logfile = io_open(log, "a+");
265 return empty_function;
267 local write, flush = logfile.write, logfile.flush;
269 local timestamps = config.timestamps;
271 if timestamps == nil or timestamps == true then
272 timestamps = default_timestamp; -- Default format
275 return function (name, level, message, ...)
277 write(logfile, os_date(timestamps), " ");
280 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
282 write(logfile, name, "\t" , level, "\t", message, "\n");
288 function register_sink_type(name, sink_maker)
289 local old_sink_maker = log_sink_types[name];
290 log_sink_types[name] = sink_maker;
291 return old_sink_maker;