Software /
code /
prosody
Comparison
core/loggingmanager.lua @ 1078:24c9ee99d900
loggingmanager: Support prepending timestamps in file/console/stdout log sinks
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 30 Apr 2009 03:05:26 +0100 |
parent | 1067:21f41b06f1d2 |
child | 1080:b02290fd8a75 |
comparison
equal
deleted
inserted
replaced
1077:d6a885cacd8c | 1078:24c9ee99d900 |
---|---|
4 local debug = debug; | 4 local debug = debug; |
5 local tostring, setmetatable, rawset, pairs, ipairs, type = | 5 local tostring, setmetatable, rawset, pairs, ipairs, type = |
6 tostring, setmetatable, rawset, pairs, ipairs, type; | 6 tostring, setmetatable, rawset, pairs, ipairs, type; |
7 local io_open, io_write = io.open, io.write; | 7 local io_open, io_write = io.open, io.write; |
8 local math_max, rep = math.max, string.rep; | 8 local math_max, rep = math.max, string.rep; |
9 local os_getenv = os.getenv; | 9 local os_date, os_getenv = os.date, os.getenv; |
10 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; | 10 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; |
11 | 11 |
12 local config = require "core.configmanager"; | 12 local config = require "core.configmanager"; |
13 | 13 |
14 local logger = require "util.logger"; | 14 local logger = require "util.logger"; |
18 module "loggingmanager" | 18 module "loggingmanager" |
19 | 19 |
20 -- The log config used if none specified in the config file | 20 -- The log config used if none specified in the config file |
21 local default_logging = { { to = "console" } }; | 21 local default_logging = { { to = "console" } }; |
22 local default_file_logging = { { to = "file", levels = { min = "info" } } }; | 22 local default_file_logging = { { to = "file", levels = { min = "info" } } }; |
23 | 23 local default_timestamp = "%b %d %T"; |
24 -- The actual config loggingmanager is using | 24 -- The actual config loggingmanager is using |
25 local logging_config = config.get("*", "core", "log") or default_logging; | 25 local logging_config = config.get("*", "core", "log") or default_logging; |
26 | 26 |
27 local apply_sink_rules; | 27 local apply_sink_rules; |
28 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; }); | 28 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; }); |
128 | 128 |
129 -- Column width for "source" (used by stdout and console) | 129 -- Column width for "source" (used by stdout and console) |
130 local sourcewidth = 20; | 130 local sourcewidth = 20; |
131 | 131 |
132 function log_sink_types.stdout() | 132 function log_sink_types.stdout() |
133 local timestamps = config.timestamps; | |
134 | |
135 if timestamps == true then | |
136 timestamps = default_timestamp; -- Default format | |
137 end | |
138 | |
133 return function (name, level, message, ...) | 139 return function (name, level, message, ...) |
134 sourcewidth = math_max(#name+2, sourcewidth); | 140 sourcewidth = math_max(#name+2, sourcewidth); |
135 local namelen = #name; | 141 local namelen = #name; |
142 if timestamps then | |
143 io_write(os_date(timestamps), " "); | |
144 end | |
136 if ... then | 145 if ... then |
137 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n"); | 146 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n"); |
138 else | 147 else |
139 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n"); | 148 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n"); |
140 end | 149 end |
154 -- Really if we don't want pretty colours then just use plain stdout | 163 -- Really if we don't want pretty colours then just use plain stdout |
155 if not do_pretty_printing then | 164 if not do_pretty_printing then |
156 return log_sink_types.stdout(config); | 165 return log_sink_types.stdout(config); |
157 end | 166 end |
158 | 167 |
168 local timestamps = config.timestamps; | |
169 | |
170 if timestamps == true then | |
171 timestamps = default_timestamp; -- Default format | |
172 end | |
173 | |
159 return function (name, level, message, ...) | 174 return function (name, level, message, ...) |
160 sourcewidth = math_max(#name+2, sourcewidth); | 175 sourcewidth = math_max(#name+2, sourcewidth); |
161 local namelen = #name; | 176 local namelen = #name; |
177 if timestamps then | |
178 io_write(os_date(timestamps), " "); | |
179 end | |
162 if ... then | 180 if ... then |
163 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); | 181 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); |
164 else | 182 else |
165 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); | 183 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); |
166 end | 184 end |
173 local logfile = io_open(log, "a+"); | 191 local logfile = io_open(log, "a+"); |
174 if not logfile then | 192 if not logfile then |
175 return function () end | 193 return function () end |
176 end | 194 end |
177 | 195 |
196 local timestamps = config.timestamps; | |
197 | |
198 if timestamps == true then | |
199 timestamps = default_timestamp; -- Default format | |
200 end | |
201 | |
178 local write, format, flush = logfile.write, format, logfile.flush; | 202 local write, format, flush = logfile.write, format, logfile.flush; |
179 return function (name, level, message, ...) | 203 return function (name, level, message, ...) |
204 if timestamps then | |
205 write(logfile, os_date(timestamps), " "); | |
206 end | |
180 if ... then | 207 if ... then |
181 write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); | 208 write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); |
182 else | 209 else |
183 write(logfile, name, "\t" , level, "\t", message, "\n"); | 210 write(logfile, name, "\t" , level, "\t", message, "\n"); |
184 end | 211 end |