Using the following code you can send logging information “prints” to small task that can then echo them to the screen or to a file or whatever you want.
Start by adding adding a new logging function in your app like so:
do
local socket = require "socket"
local udp = socket.udp()
function log(text)
udp:sendto(text .. "\n", "127.0.0.1", 9999)
end
end
You can add it to your main.lua and call it from anywhere in your app like so:
log("some text I want to log")
you could also map all your existing calls to print to this log function by adding
print = log
Now, you need something to listen for your log messages. You could write it in any language you want.
My example uses Lua:
local socket = require("socket")
local host = "127.0.0.1"
local port = 9999
print("Binding to host '" ..host.. "' and port " ..port.. "...")
local udp = assert(socket.udp())
assert(udp:setsockname(host, port))
assert(udp:settimeout(60))
while true do
dgram, ip, port = udp:receivefrom()
if dgram then
print(tostring(ip) .. ":" .. dgram)
else
print(ip)
end
end
EDIT: I liked chinway’s idea of using netcat, so instead of the above little program you can instead use the command:
nc -ul -p 9999
[import]uid: 846 topic_id: 11057 reply_id: 311057[/import]