LogicMachine Forum
Syslog differences between Resident and Event Scripts - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Syslog differences between Resident and Event Scripts (/showthread.php?tid=6349)



Syslog differences between Resident and Event Scripts - BarryFromBondi - 12.03.2026

I have the exact same Syslog set up in my Resident and Event Scripts - the Resident Scripts Syslog works, however the Event scripts doesnt

The syslog set up that works in Resident is 

local socket = require("socket")

-------------------------------------------------
-- SYSLOG CONFIG
-------------------------------------------------

local SYSLOG_HOST = "192.168.1.51"
local SYSLOG_PORT = 1514

local HOSTNAME = "logicmachine"
local APPNAME  = "solar-master"

local LOG_INTERVAL = 60  -- seconds

-------------------------------------------------
-- PERSISTENT UDP SOCKET
-------------------------------------------------

storage.syslog_socket = storage.syslog_socket or socket.udp()

local udp = storage.syslog_socket
udp:settimeout(0)

-------------------------------------------------
-- RFC5424 TIMESTAMP
-------------------------------------------------

local function rfc5424_time()

  local now = os.time()
  local utc = os.time(os.date("!*t", now))
  local offset = os.difftime(now, utc)

  local sign = "+"
  if offset < 0 then sign = "-" end

  offset = math.abs(offset)

  local hours = math.floor(offset / 3600)
  local mins  = math.floor((offset % 3600) / 60)

  return os.date("%Y-%m-%dT%H:%M:%S") ..
        string.format("%s%02d:%02d", sign, hours, mins)

end

-------------------------------------------------
-- SYSLOG SENDER
-------------------------------------------------

local function slog(fields)

  local timestamp = rfc5424_time()

  local msg = "<14>1 "..timestamp.." "..HOSTNAME.." "..APPNAME.." - - - "..fields

  udp:sendto(msg, SYSLOG_HOST, SYSLOG_PORT)

end

What needs to change for an Event Script to behave the same way and allow for logging to an external syslog server?

Thanks


RE: Syslog differences between Resident and Event Scripts - admin - 13.03.2026

Sharing socket between scripts is not possible. You can simply create a new socket for each slog() call.

Add the following code to Common functions to make slog() function available for all scripts.
Code:
local function rfc5424_time()
  local now = os.time()
  local utc = os.time(os.date("!*t", now))
  local offset = os.difftime(now, utc)

  local sign = "+"
  if offset < 0 then sign = "-" end

  offset = math.abs(offset)

  local hours = math.floor(offset / 3600)
  local mins  = math.floor((offset % 3600) / 60)

  return os.date("%Y-%m-%dT%H:%M:%S") ..
        string.format("%s%02d:%02d", sign, hours, mins)
end

function slog(fields)
  local SYSLOG_HOST = "192.168.0.2"
  local SYSLOG_PORT = 1514

  local HOSTNAME = "logicmachine"
  local APPNAME  = "solar-master"

  local timestamp = rfc5424_time()
  local msg = "<14>1 "..timestamp.." "..HOSTNAME.." "..APPNAME.." - - - "..fields
  local sock = require('socket').udp()
  sock:sendto(msg, SYSLOG_HOST, SYSLOG_PORT)
  sock:close()
end