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];
52 local sink = sink_maker(sink_config);
54 -- Set sink for all chosen levels
55 for level in pairs(get_levels(sink_config.levels or logging_levels)) do
56 logger.add_level_sink(level, sink);
63 -- Search for all rules using a particular sink type, and apply
64 -- them. Called automatically when a new sink type is added to
65 -- the log_sink_types table.
66 function apply_sink_rules(sink_type)
67 if type(logging_config) == "table" then
69 for _, level in ipairs(logging_levels) do
70 if type(logging_config[level]) == "string" then
71 local value = logging_config[level];
72 if sink_type == "file" then
77 levels = { min = level };
79 elseif value == "*"..sink_type then
82 levels = { min = level };
88 for _, sink_config in ipairs(logging_config) do
89 if (type(sink_config) == "table" and sink_config.to == sink_type) then
90 add_rule(sink_config);
91 elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
92 add_rule({ levels = { min = "debug" }, to = sink_type });
95 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
96 -- User specified simply a filename, and the "file" sink type
98 for _, sink_config in pairs(default_file_logging) do
99 sink_config.filename = logging_config;
100 add_rule(sink_config);
101 sink_config.filename = nil;
103 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
104 -- Log all levels (debug+) to this sink
105 add_rule({ levels = { min = "debug" }, to = sink_type });
111 --- Helper function to get a set of levels given a "criteria" table
112 function get_levels(criteria, set)
114 if type(criteria) == "string" then
115 set[criteria] = true;
118 local min, max = criteria.min, criteria.max;
121 for _, level in ipairs(logging_levels) do
125 elseif max == level then
134 for _, level in ipairs(criteria) do
140 -- Initialize config, etc. --
141 function reload_logging()
142 local old_sink_types = {};
144 for name, sink_maker in pairs(log_sink_types) do
145 old_sink_types[name] = sink_maker;
146 log_sink_types[name] = nil;
151 local debug_mode = config.get("*", "core", "debug");
153 default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
154 default_file_logging = {
155 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
157 default_timestamp = "%b %d %H:%M:%S";
159 logging_config = config.get("*", "core", "log") or default_logging;
162 for name, sink_maker in pairs(old_sink_types) do
163 log_sink_types[name] = sink_maker;
166 prosody.events.fire_event("logging-reloaded");
170 prosody.events.add_handler("config-reloaded", reload_logging);
172 --- Definition of built-in logging sinks ---
174 -- Null sink, must enter log_sink_types *first*
175 function log_sink_types.nowhere()
176 return function () return false; end;
179 -- Column width for "source" (used by stdout and console)
180 local sourcewidth = 20;
182 function log_sink_types.stdout(config)
183 local timestamps = config.timestamps;
185 if timestamps == true then
186 timestamps = default_timestamp; -- Default format
189 return function (name, level, message, ...)
190 sourcewidth = math_max(#name+2, sourcewidth);
191 local namelen = #name;
193 io_write(os_date(timestamps), " ");
196 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
198 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
204 local do_pretty_printing = true;
206 local logstyles = {};
207 if do_pretty_printing then
208 logstyles["info"] = getstyle("bold");
209 logstyles["warn"] = getstyle("bold", "yellow");
210 logstyles["error"] = getstyle("bold", "red");
212 function log_sink_types.console(config)
213 -- Really if we don't want pretty colours then just use plain stdout
214 if not do_pretty_printing then
215 return log_sink_types.stdout(config);
218 local timestamps = config.timestamps;
220 if timestamps == true then
221 timestamps = default_timestamp; -- Default format
224 return function (name, level, message, ...)
225 sourcewidth = math_max(#name+2, sourcewidth);
226 local namelen = #name;
229 io_write(os_date(timestamps), " ");
231 io_write(name, rep(" ", sourcewidth-namelen));
232 setstyle(logstyles[level]);
236 io_write("\t", format(message, ...), "\n");
238 io_write("\t", message, "\n");
244 local empty_function = function () end;
245 function log_sink_types.file(config)
246 local log = config.filename;
247 local logfile = io_open(log, "a+");
249 return empty_function;
251 local write, flush = logfile.write, logfile.flush;
253 local timestamps = config.timestamps;
255 if timestamps == nil or timestamps == true then
256 timestamps = default_timestamp; -- Default format
259 return function (name, level, message, ...)
261 write(logfile, os_date(timestamps), " ");
264 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
266 write(logfile, name, "\t" , level, "\t", message, "\n");
272 function register_sink_type(name, sink_maker)
273 local old_sink_maker = log_sink_types[name];
274 log_sink_types[name] = sink_maker;
275 return old_sink_maker;