Comparison

init.lua @ 238:9b6fca5d9a02

verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
author Matthew Wild <mwild1@gmail.com>
date Sun, 27 Nov 2011 22:45:26 +0000
parent 237:d158c97556b4
child 239:65a0d3fcbbad
comparison
equal deleted inserted replaced
237:d158c97556b4 238:9b6fca5d9a02
20 verse.plugins = {}; 20 verse.plugins = {};
21 21
22 function verse.new(logger, base) 22 function verse.new(logger, base)
23 local t = setmetatable(base or {}, stream); 23 local t = setmetatable(base or {}, stream);
24 t.id = tostring(t):match("%x*$"); 24 t.id = tostring(t):match("%x*$");
25 t:set_logger(logger or verse.log, true); 25 t.logger = logger or verse.new_logger("stream"..t.id);
26 t.events = events.new(); 26 t.events = events.new();
27 t.plugins = {}; 27 t.plugins = {};
28 return t; 28 return t;
29 end 29 end
30 30
31 verse.add_task = require "util.timer".add_task; 31 verse.add_task = require "util.timer".add_task;
32 32
33 verse.logger = logger.init; -- Deprecated 33 verse.logger = logger.init; -- COMPAT: Deprecated
34 verse.new_logger = logger.init; 34 verse.new_logger = logger.init;
35 verse.log = verse.logger("verse"); 35 verse.log = verse.logger("verse");
36 36
37 function verse.set_logger(logger) 37 function verse.set_log_handler(log_handler, levels)
38 verse.log = logger; 38 levels = levels or { "debug", "info", "warn", "error" };
39 server.setlogger(logger); 39 logger.reset();
40 end 40 for i, level in ipairs(levels) do
41 41 logger.add_level_sink(level, log_handler);
42 function verse.filter_log(levels, logger)
43 local level_set = {};
44 for _, level in ipairs(levels) do
45 level_set[level] = true;
46 end 42 end
47 return function (level, name, ...)
48 if level_set[level] then
49 return logger(level, name, ...);
50 end
51 end;
52 end 43 end
53 44
54 local function error_handler(err) 45 local function error_handler(err)
55 verse.log("error", "Error: %s", err); 46 verse.log("error", "Error: %s", err);
56 verse.log("error", "Traceback: %s", debug.traceback()); 47 verse.log("error", "Traceback: %s", debug.traceback());
112 on_disconnect(conn, reason); 103 on_disconnect(conn, reason);
113 end 104 end
114 105
115 -- Logging functions 106 -- Logging functions
116 function stream:debug(...) 107 function stream:debug(...)
117 if self.logger and self.log.debug then 108 return self.logger("debug", ...);
118 return self.logger("debug", ...);
119 end
120 end 109 end
121 110
122 function stream:warn(...) 111 function stream:warn(...)
123 if self.logger and self.log.warn then 112 return self.logger("warn", ...);
124 return self.logger("warn", ...);
125 end
126 end 113 end
127 114
128 function stream:error(...) 115 function stream:error(...)
129 if self.logger and self.log.error then 116 return self.logger("error", ...);
130 return self.logger("error", ...);
131 end
132 end
133
134 function stream:set_logger(logger, levels)
135 local old_logger = self.logger;
136 if logger then
137 self.logger = logger;
138 end
139 if levels then
140 if levels == true then
141 levels = { "debug", "info", "warn", "error" };
142 end
143 self.log = {};
144 for _, level in ipairs(levels) do
145 self.log[level] = true;
146 end
147 end
148 return old_logger;
149 end
150
151 function stream_mt:set_log_levels(levels)
152 self:set_logger(nil, levels);
153 end 117 end
154 118
155 -- Event handling 119 -- Event handling
156 function stream:event(name, ...) 120 function stream:event(name, ...)
157 self:debug("Firing event: "..tostring(name)); 121 self:debug("Firing event: "..tostring(name));