Comparison

core/loggingmanager.lua @ 3358:b5a812cf698c

loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
author Matthew Wild <mwild1@gmail.com>
date Wed, 14 Jul 2010 01:25:32 +0100
parent 3045:ff5b3932e5f2
child 3438:9a8b716fda14
comparison
equal deleted inserted replaced
3357:1dd83dd2b832 3358:b5a812cf698c
24 end 24 end
25 25
26 local config = require "core.configmanager"; 26 local config = require "core.configmanager";
27 local eventmanager = require "core.eventmanager"; 27 local eventmanager = require "core.eventmanager";
28 local logger = require "util.logger"; 28 local logger = require "util.logger";
29 local prosody = prosody;
30
29 local debug_mode = config.get("*", "core", "debug"); 31 local debug_mode = config.get("*", "core", "debug");
30 32
31 _G.log = logger.init("general"); 33 _G.log = logger.init("general");
32 34
33 module "loggingmanager" 35 module "loggingmanager"
34 36
35 -- The log config used if none specified in the config file 37 -- The log config used if none specified in the config file (see reload_logging for initialization)
36 local default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } }; 38 local default_logging;
37 local default_file_logging = { { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true } }; 39 local default_file_logging;
38 local default_timestamp = "%b %d %H:%M:%S"; 40 local default_timestamp = "%b %d %H:%M:%S";
39 -- The actual config loggingmanager is using 41 -- The actual config loggingmanager is using
40 local logging_config = config.get("*", "core", "log") or default_logging; 42 local logging_config;
41 43
42 local apply_sink_rules; 44 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 log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
44 local get_levels; 46 local get_levels;
45 local logging_levels = { "debug", "info", "warn", "error", "critical" } 47 local logging_levels = { "debug", "info", "warn", "error", "critical" }
136 set[level] = true; 138 set[level] = true;
137 end 139 end
138 return set; 140 return set;
139 end 141 end
140 142
143 -- Initialize config, etc. --
144 function reload_logging()
145 local old_sink_types = {};
146
147 for name, sink_maker in pairs(log_sink_types) do
148 old_sink_types[name] = sink_maker;
149 log_sink_types[name] = nil;
150 end
151
152 logger.reset();
153
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";
157
158 logging_config = config.get("*", "core", "log") or default_logging;
159
160
161 for name, sink_maker in pairs(old_sink_types) do
162 log_sink_types[name] = sink_maker;
163 end
164
165 prosody.events.fire_event("logging-reloaded");
166 end
167
168 reload_logging();
169 prosody.events.add_handler("config-reloaded", reload_logging);
170
141 --- Definition of built-in logging sinks --- 171 --- Definition of built-in logging sinks ---
142 172
143 -- Null sink, must enter log_sink_types *first* 173 -- Null sink, must enter log_sink_types *first*
144 function log_sink_types.nowhere() 174 function log_sink_types.nowhere()
145 return function () return false; end; 175 return function () return false; end;
213 if not logfile then 243 if not logfile then
214 return empty_function; 244 return empty_function;
215 end 245 end
216 local write, flush = logfile.write, logfile.flush; 246 local write, flush = logfile.write, logfile.flush;
217 247
218 eventmanager.add_event_hook("reopen-log-files", function () 248 prosody.events.add_handler("logging-reloading", function ()
219 if logfile then 249 if logfile then
220 logfile:close(); 250 logfile:close();
221 end
222 logfile = io_open(log, "a+");
223 if not logfile then
224 write, flush = empty_function, empty_function;
225 else
226 write, flush = logfile.write, logfile.flush;
227 end 251 end
228 end); 252 end);
229 253
230 local timestamps = config.timestamps; 254 local timestamps = config.timestamps;
231 255