Several people have asked for this, so…
… if you want to write messages to a syslog somewhere, this little piece of code will do it:
-- January, 2014 @akbooer
local socket = require "socket"
local function syslog_server (ip_and_port, tag, hostname)
local sock = socket.udp()
local facility = 1 -- 'user'
local emergency, alert, critical, error, warning, notice, info, debug = 0,1,2,3,4,5,6,7
local ip, port = ip_and_port: match "^(%d+%.%d+%.%d+%.%d+):(%d+)$"
if not ip or not port then return nil, "invalid IP or PORT" end
local serialNo = luup.pk_accesspoint
hostname = ("Vera-"..serialNo) or "Vera"
if not tag or tag == '' then tag = "Plugin" end
tag = tag: gsub("[^%w]","") -- only alphanumeric, no spaces or other
local function send (self, content, severity)
content = tostring (content)
severity = tonumber (severity) or info
local priority = facility*8 + (severity%8)
local msg = ("<%d>%s %s %s: %s\n"):format (priority, os.date "%b %d %H:%M:%S", hostname, tag, content)
sock:send (msg)
end
local ok, err = sock:setpeername(ip, port)
if ok then ok = {send = send} end
return ok, err
end
You use it like this…
b Initialisation:[/b]
Just ONCE, either in Startup Lua (if you’re going to use this from scene code), or in your app’s init phase, set up the connection and other static data.
syslog, err = syslog_server (syslogInfo, syslogTag)
if not syslog then luup.log ('UDP syslog service error: '..err) end
[ul][li][tt]syslogInfo[/tt] - a string containing the IP and PORT of your syslog server, eg. “172.16.55:514”[/li]
[li][tt]syslogTag[/tt] - a string with the tag name for your app (anything other than alphanumeric will be stripped off, eg. no spaces)[/li]
[li]the hostname field is filled in with [tt]Vera-nnnnnn[/tt], where nnnnnn is the serial number of your Vera[/li]
[li]the facility level is set to [tt]user[/tt][/li][/ul]
b Sending a message:[/b]
Whenever/wherever you like, send a message.
if syslog then syslog:send (message) end
[ul][li]note that this uses the colon syntax, not the dot notation[/li]
[li]an optional second parameter (0-7) is the severity level, default is 6[/li]
[li][tt](emergency, alert, critical, error, warning, notice, info, debug = 0,1,2,3,4,5,6,7)[/tt][/li][/ul]
I’ve replaced most of my [tt]luup.log (message)[/tt] calls with [tt]syslog:send (message)[/tt], so no need to trawl through Vera’s log anymore and that means removing one whole app which was there just for that purpose.
Since syslog uses UDP datagrams, the system/network load is very low, there is no acknowledgement of receipt or handshake, and networks errors may mean that messages do not get to their destination. However, (a) this very rarely happens, (b) it doesn’t really matter for non-critical notifications.
Enjoy.