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, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
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 eventmanager = require "core.eventmanager";
28 local logger = require "util.logger";
29 local prosody = prosody;
31 local debug_mode = config.get("*", "core", "debug");
33 _G.log = logger.init("general");
35 module "loggingmanager"
37 -- The log config used if none specified in the config file (see reload_logging for initialization)
38 local default_logging;
39 local default_file_logging;
40 local default_timestamp = "%b %d %H:%M:%S";
41 -- The actual config loggingmanager is using
44 local apply_sink_rules;
45 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
47 local logging_levels = { "debug", "info", "warn", "error", "critical" }
49 -- Put a rule into action. Requires that the sink type has already been registered.
50 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
51 local function add_rule(sink_config)
52 local sink_maker = log_sink_types[sink_config.to];
54 if sink_config.levels and not sink_config.source then
56 local sink = sink_maker(sink_config);
58 -- Set sink for all chosen levels
59 for level in pairs(get_levels(sink_config.levels)) do
60 logger.add_level_sink(level, sink);
62 elseif sink_config.source and not sink_config.levels then
63 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
64 elseif sink_config.source and sink_config.levels then
65 local levels = get_levels(sink_config.levels);
66 local sink = sink_maker(sink_config);
67 logger.add_name_sink(sink_config.source,
68 function (name, level, ...)
70 return sink(name, level, ...);
76 local sink = sink_maker(sink_config);
78 -- Set sink for all levels
79 for _, level in pairs(logging_levels) do
80 logger.add_level_sink(level, sink);
88 -- Search for all rules using a particular sink type, and apply
89 -- them. Called automatically when a new sink type is added to
90 -- the log_sink_types table.
91 function apply_sink_rules(sink_type)
92 if type(logging_config) == "table" then
93 for _, sink_config in pairs(logging_config) do
94 if sink_config.to == sink_type then
95 add_rule(sink_config);
98 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
99 -- User specified simply a filename, and the "file" sink type
101 for _, sink_config in pairs(default_file_logging) do
102 sink_config.filename = logging_config;
103 add_rule(sink_config);
104 sink_config.filename = nil;
106 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
107 -- Log all levels (debug+) to this sink
108 add_rule({ levels = { min = "debug" }, to = sink_type });
114 --- Helper function to get a set of levels given a "criteria" table
115 function get_levels(criteria, set)
117 if type(criteria) == "string" then
118 set[criteria] = true;
121 local min, max = criteria.min, criteria.max;
124 for _, level in ipairs(logging_levels) do
128 elseif max == level then
137 for _, level in ipairs(criteria) do
143 -- Initialize config, etc. --
144 function reload_logging()
145 local old_sink_types = {};
147 for name, sink_maker in pairs(log_sink_types) do
148 old_sink_types[name] = sink_maker;
149 log_sink_types[name] = nil;
154 default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
155 default_file_logging = { { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true } };
156 default_timestamp = "%b %d %H:%M:%S";
158 logging_config = config.get("*", "core", "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 function log_sink_types.nowhere()
175 return function () return false; end;
178 -- Column width for "source" (used by stdout and console)
179 local sourcewidth = 20;
181 function log_sink_types.stdout()
182 local timestamps = config.timestamps;
184 if timestamps == true then
185 timestamps = default_timestamp; -- Default format
188 return function (name, level, message, ...)
189 sourcewidth = math_max(#name+2, sourcewidth);
190 local namelen = #name;
192 io_write(os_date(timestamps), " ");
195 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
197 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
203 local do_pretty_printing = not os_getenv("WINDIR");
205 local logstyles = {};
206 if do_pretty_printing then
207 logstyles["info"] = getstyle("bold");
208 logstyles["warn"] = getstyle("bold", "yellow");
209 logstyles["error"] = getstyle("bold", "red");
211 function log_sink_types.console(config)
212 -- Really if we don't want pretty colours then just use plain stdout
213 if not do_pretty_printing then
214 return log_sink_types.stdout(config);
217 local timestamps = config.timestamps;
219 if timestamps == true then
220 timestamps = default_timestamp; -- Default format
223 return function (name, level, message, ...)
224 sourcewidth = math_max(#name+2, sourcewidth);
225 local namelen = #name;
228 io_write(os_date(timestamps), " ");
231 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
233 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
239 local empty_function = function () end;
240 function log_sink_types.file(config)
241 local log = config.filename;
242 local logfile = io_open(log, "a+");
244 return empty_function;
246 local write, flush = logfile.write, logfile.flush;
248 prosody.events.add_handler("logging-reloading", function ()
254 local timestamps = config.timestamps;
256 if timestamps == nil or timestamps == true then
257 timestamps = default_timestamp; -- Default format
260 return function (name, level, message, ...)
262 write(logfile, os_date(timestamps), " ");
265 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
267 write(logfile, name, "\t" , level, "\t", message, "\n");
273 function register_sink_type(name, sink_maker)
274 local old_sink_maker = log_sink_types[name];
275 log_sink_types[name] = sink_maker;
276 return old_sink_maker;