-
Notifications
You must be signed in to change notification settings - Fork 2
/
syslog.lua
56 lines (48 loc) · 1.78 KB
/
syslog.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local logging = require "logging"
local lsyslog = require "lsyslog"
local log = lsyslog.log
local convert = {
[logging.DEBUG] = lsyslog.LOG_DEBUG,
[logging.INFO] = lsyslog.LOG_INFO,
[logging.WARN] = lsyslog.LOG_WARNING,
[logging.ERROR] = lsyslog.LOG_ERR,
[logging.FATAL] = lsyslog.LOG_ALERT,
}
function logging.syslog(params, ...)
params = logging.getDeprecatedParams({ "ident", "facility" }, params, ...)
local ident = params.ident or "lua"
local facility = params.facility or lsyslog.FACILITY_USER
-- timestampPattern and logPattern not supported, added by syslog itself
local logPattern = params.logPattern
local timestampPattern = params.timestampPattern
lsyslog.open(ident, facility)
if logPattern then
-- a pattern was provided, so return an appender that takes that into account
-- this is less performant, hence we provide 2 different appenders
return logging.new(function(self, level, message)
message = logging.prepareLogMsg(logPattern, os.date(timestampPattern), level, message)
log(convert[level], message)
return true
end)
end
-- no pattern provided, so return a lean-and-mean version of the appender
return logging.new(function(self, level, message)
log(convert[level], message)
return true
end)
end
-- Create a module table and copy lsyslog contents (we need the constants, but
-- copy everything because its simpler)
local _M = {} do
for key, value in pairs(lsyslog) do
_M[key] = value
end
end
return setmetatable(_M, {
-- the default appenders return the function that create the appenders,
-- we wrap that in a call with a metatable because we also want to return the
-- lsyslog constants. And the metatable __call methopd allows us to do both.
__call = function(self, ...)
return logging.syslog(...)
end,
})