]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
lua: convert log functions to suricata.log lib 13339/head
authorJason Ish <jason.ish@oisf.net>
Fri, 30 May 2025 17:02:44 +0000 (11:02 -0600)
committerVictor Julien <victor@inliniac.net>
Fri, 30 May 2025 19:13:50 +0000 (21:13 +0200)
Convert the Lua global functions for logging (SCLogInfo, etc) to a Lua
lib names "suricata.log".

Ticket: #7727

doc/userguide/lua/libs/index.rst
doc/userguide/lua/libs/log.rst [new file with mode: 0644]
doc/userguide/lua/libs/packetlib.rst
doc/userguide/lua/lua-functions.rst
doc/userguide/output/lua-output.rst
src/Makefile.am
src/util-lua-builtins.c
src/util-lua-common.c
src/util-lua-log.c [new file with mode: 0644]
src/util-lua-log.h [new file with mode: 0644]

index b79492abdbd306b71669d72e023821d20de4941f..0778b63440a2451e13bab13ff8ae02a672a570c0 100644 (file)
@@ -16,6 +16,7 @@ environment without access to additional modules.
    flowvar
    hashlib
    http
+   log
    packetlib
    rule
    smtp
diff --git a/doc/userguide/lua/libs/log.rst b/doc/userguide/lua/libs/log.rst
new file mode 100644 (file)
index 0000000..bf547c6
--- /dev/null
@@ -0,0 +1,92 @@
+Log
+###
+
+The ``suricata.log`` Lua library exposes the Suricata application
+logging functions to Lua scripts. These are equivalant to
+``SCLogNotice``, ``SCLogError``, etc, in the Suricata source.
+
+In Suricata, the logging priority order is:
+
+* Error
+* Warning
+* Notice
+* Info
+* Perf
+* Config
+* Debug
+
+.. note:: Debug logging will only work if Suricata was compiled with
+          ``--enable-debug``.
+
+Setup
+*****
+
+To use the logging functions, first require the module::
+
+    local logger = require("suricata.log")
+
+Functions
+*********
+
+``info``
+========
+
+Log an informational message::
+
+  logger.info("Processing HTTP request")
+
+This is equivalent to ``SCLogInfo``.
+
+``notice``
+==========
+
+Log a notice message::
+
+  logger.notice("Unusual pattern detected")
+
+This is equivalent to ``SCLogNotice``.
+
+``warning``
+===========
+
+Log a warning message::
+
+  logger.warning("Connection limit approaching")
+
+This is equivalent to ``SCLogWarning``.
+
+``error``
+=========
+
+Log an error message::
+
+  logger.error("Failed to parse data")
+
+This is equivalent to ``SCLogError``.
+
+``debug``
+=========
+
+Log a debug message (only visible when debug logger.ing is enabled)::
+
+  logger.debug("Variable value: " .. tostring(value))
+
+This is equivalent to ``SCLogDebug``.
+
+``config``
+==========
+
+Log a configuration-related message::
+
+  logger.config("Loading configuration from " .. filename)
+
+This is equivalent to ``SCLogConfig``.
+
+``perf``
+========
+
+Log a performance-related message::
+
+  logger.perf("Processing took " .. elapsed .. " seconds")
+
+This is equivalent to ``SCLogPerf``.
index 8961a11310132c20eb03039c5f2a7c8dc1897c38..8924dba3fa43649f8490d7db856bce472493e759 100644 (file)
@@ -160,6 +160,8 @@ If it is found, issue a notice log with packet details.
 
 ::
 
+    local logger = require("suricata.log")
+
     function match (args)
         p = packet.get()
         payload = p:payload()
@@ -168,7 +170,7 @@ If it is found, issue a notice log with packet details.
         for line in payload:gmatch("([^\r\n]*)[\r\n]+") do
             if line == "GET /index.html HTTP/1.0" then
                 ipver, srcip, dstip, proto, sp, dp = p:tuple()
-                SCLogNotice(string.format("%s %s->%s %d->%d (pcap_cnt:%d) match! %s", ts, srcip, dstip, sp, dp, p:pcap_cnt(), line));
+                logger.notice(string.format("%s %s->%s %d->%d (pcap_cnt:%d) match! %s", ts, srcip, dstip, sp, dp, p:pcap_cnt(), line));
                 return 1
             end
         end
index 1bbbd541c05d36cb943b9f43748c9ccffd51d084..dce63ced6d77fe64f6edab41986fab3ed5afa98f 100644 (file)
@@ -314,17 +314,6 @@ SCThreadInfo
 
 It gives: tid (integer), tname (string), tgroup (string)
 
-SCLogError, SCLogWarning, SCLogNotice, SCLogInfo, SCLogDebug
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Print a message. It will go into the outputs defined in the
-yaml. Whether it will be printed depends on the log level.
-
-Example:
-
-::
-
-  SCLogError("some error message")
 
 SCLogPath
 ~~~~~~~~~
index 9401d94e5e21a553674128b42de5767890b426ff..d21ae7de761418f2d7d44389034f6277ddf5e02b 100644 (file)
@@ -27,6 +27,8 @@ Example:
 
 ::
 
+  local logger = require("suricata.log")
+
   function init (args)
       local needs = {}
       needs["protocol"] = "http"
@@ -36,7 +38,7 @@ Example:
   function setup (args)
       filename = SCLogPath() .. "/" .. name
       file = assert(io.open(filename, "a"))
-      SCLogInfo("HTTP Log Filename " .. filename)
+      logger.info("HTTP Log Filename " .. filename)
       http = 0
   end
 
@@ -71,7 +73,7 @@ Example:
   end
 
   function deinit (args)
-      SCLogInfo ("HTTP transactions logged: " .. http);
+      logger.info ("HTTP transactions logged: " .. http);
       file:close(file)
   end
 
index 4b10a970cccd66f70ba2046480f215a2d5907af4..4501243b924a5e27c5aa3d98b89664a231e0e44f 100755 (executable)
@@ -547,6 +547,7 @@ noinst_HEADERS = \
        util-lua-hashlib.h \
        util-lua-http.h \
        util-lua-ja3.h \
+       util-lua-log.h \
        util-lua-packetlib.h \
        util-lua-rule.h \
        util-lua-sandbox.h \
@@ -1124,6 +1125,7 @@ libsuricata_c_a_SOURCES = \
        util-lua-hashlib.c \
        util-lua-http.c \
        util-lua-ja3.c \
+       util-lua-log.c \
        util-lua-packetlib.c \
        util-lua-rule.c \
        util-lua-sandbox.c \
index a14101da91f125a3fa18d0ab8868c281c193327e..5b45ab7130ad886c9cd09c9dce0ff5e2dba06c8a 100644 (file)
@@ -33,6 +33,7 @@
 #include "util-lua-rule.h"
 #include "util-lua-ja3.h"
 #include "util-lua-filelib.h"
+#include "util-lua-log.h"
 
 #include "lauxlib.h"
 
@@ -48,6 +49,7 @@ static const luaL_Reg builtins[] = {
     { "suricata.hashlib", SCLuaLoadHashlib },
     { "suricata.http", SCLuaLoadHttpLib },
     { "suricata.ja3", SCLuaLoadJa3Lib },
+    { "suricata.log", SCLuaLoadLogLib },
     { "suricata.packet", LuaLoadPacketLib },
     { "suricata.rule", SCLuaLoadRuleLib },
     { "suricata.smtp", SCLuaLoadSmtpLib },
index 4082c0e1cc033b72b5d4a00ccec1a4d9866725dc..33498b088f1c848db6b9eee7ad0144742673df20 100644 (file)
  */
 
 #include "suricata-common.h"
-#include "detect.h"
-#include "pkt-var.h"
-#include "conf.h"
 
 #include "threads.h"
 #include "threadvars.h"
-#include "tm-threads.h"
-
-#include "util-print.h"
-#include "util-unittest.h"
-
-#include "util-debug.h"
 
 #include "output.h"
-#include "app-layer-htp.h"
-#include "app-layer.h"
-#include "app-layer-parser.h"
-#include "util-privs.h"
-#include "util-buffer.h"
-#include "util-proto-name.h"
-#include "util-logopenfile.h"
-#include "util-time.h"
 #include "util-conf.h"
 
 #include "lua.h"
-#include "lualib.h"
-#include "lauxlib.h"
 
 #include "util-lua.h"
 #include "util-lua-common.h"
-#include "action-globals.h"
 
 int LuaCallbackError(lua_State *luastate, const char *msg)
 {
@@ -142,70 +122,6 @@ static int LuaCallbackLogPath(lua_State *luastate)
     return LuaPushStringBuffer(luastate, (const uint8_t *)ld, strlen(ld));
 }
 
-static int LuaCallbackLogDebug(lua_State *luastate)
-{
-    const char *msg = LuaGetStringArgument(luastate, 1);
-    if (msg == NULL)
-        return LuaCallbackError(luastate, "1st argument missing, empty or wrong type");
-    SCLogDebug("%s", msg);
-    return 0;
-}
-
-static int LuaCallbackLogInfo(lua_State *luastate)
-{
-    const char *msg = LuaGetStringArgument(luastate, 1);
-    if (msg == NULL)
-        return LuaCallbackError(luastate, "1st argument missing, empty or wrong type");
-
-    lua_Debug ar;
-    lua_getstack(luastate, 1, &ar);
-    lua_getinfo(luastate, "nSl", &ar);
-    const char *funcname = ar.name ? ar.name : ar.what;
-    SCLogInfoRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
-    return 0;
-}
-
-static int LuaCallbackLogNotice(lua_State *luastate)
-{
-    const char *msg = LuaGetStringArgument(luastate, 1);
-    if (msg == NULL)
-        return LuaCallbackError(luastate, "1st argument missing, empty or wrong type");
-
-    lua_Debug ar;
-    lua_getstack(luastate, 1, &ar);
-    lua_getinfo(luastate, "nSl", &ar);
-    const char *funcname = ar.name ? ar.name : ar.what;
-    SCLogNoticeRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
-    return 0;
-}
-
-static int LuaCallbackLogWarning(lua_State *luastate)
-{
-    const char *msg = LuaGetStringArgument(luastate, 1);
-    if (msg == NULL)
-        return LuaCallbackError(luastate, "1st argument missing, empty or wrong type");
-
-    lua_Debug ar;
-    lua_getstack(luastate, 1, &ar);
-    lua_getinfo(luastate, "nSl", &ar);
-    const char *funcname = ar.name ? ar.name : ar.what;
-    SCLogWarningRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
-    return 0;
-}
-
-static int LuaCallbackLogError(lua_State *luastate)
-{
-    const char *msg = LuaGetStringArgument(luastate, 1);
-    if (msg == NULL)
-        return LuaCallbackError(luastate, "1st argument missing, empty or wrong type");
-    lua_Debug ar;
-    lua_getstack(luastate, 1, &ar);
-    lua_getinfo(luastate, "nSl", &ar);
-    const char *funcname = ar.name ? ar.name : ar.what;
-    SCLogErrorRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
-    return 0;
-}
-
 /** \internal
  *  \brief fill lua stack with thread info
  *  \param luastate the lua state
@@ -245,17 +161,6 @@ int LuaRegisterFunctions(lua_State *luastate)
     lua_pushcfunction(luastate, LuaCallbackLogPath);
     lua_setglobal(luastate, "SCLogPath");
 
-    lua_pushcfunction(luastate, LuaCallbackLogDebug);
-    lua_setglobal(luastate, "SCLogDebug");
-    lua_pushcfunction(luastate, LuaCallbackLogInfo);
-    lua_setglobal(luastate, "SCLogInfo");
-    lua_pushcfunction(luastate, LuaCallbackLogNotice);
-    lua_setglobal(luastate, "SCLogNotice");
-    lua_pushcfunction(luastate, LuaCallbackLogWarning);
-    lua_setglobal(luastate, "SCLogWarning");
-    lua_pushcfunction(luastate, LuaCallbackLogError);
-    lua_setglobal(luastate, "SCLogError");
-
     lua_pushcfunction(luastate, LuaCallbackThreadInfo);
     lua_setglobal(luastate, "SCThreadInfo");
     return 0;
diff --git a/src/util-lua-log.c b/src/util-lua-log.c
new file mode 100644 (file)
index 0000000..4fed9f7
--- /dev/null
@@ -0,0 +1,93 @@
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "suricata-common.h"
+#include "util-lua-log.h"
+#include "util-lua.h"
+#include "util-debug.h"
+
+#include "lauxlib.h"
+
+static int LuaLogInfo(lua_State *L)
+{
+    const char *msg = luaL_checkstring(L, 1);
+    SCLogInfo("%s", msg);
+    return 0;
+}
+
+static int LuaLogNotice(lua_State *L)
+{
+    const char *msg = luaL_checkstring(L, 1);
+    SCLogNotice("%s", msg);
+    return 0;
+}
+
+static int LuaLogWarning(lua_State *L)
+{
+    const char *msg = luaL_checkstring(L, 1);
+    SCLogWarning("%s", msg);
+    return 0;
+}
+
+static int LuaLogError(lua_State *L)
+{
+    const char *msg = luaL_checkstring(L, 1);
+    SCLogError("%s", msg);
+    return 0;
+}
+
+static int LuaLogDebug(lua_State *L)
+{
+#ifdef DEBUG
+    const char *msg = luaL_checkstring(L, 1);
+    SCLogDebug("%s", msg);
+#endif
+    return 0;
+}
+
+static int LuaLogConfig(lua_State *L)
+{
+    const char *msg = luaL_checkstring(L, 1);
+    SCLogConfig("%s", msg);
+    return 0;
+}
+
+static int LuaLogPerf(lua_State *L)
+{
+    const char *msg = luaL_checkstring(L, 1);
+    SCLogPerf("%s", msg);
+    return 0;
+}
+
+static const struct luaL_Reg loglib[] = {
+    // clang-format off
+    { "info", LuaLogInfo },
+    { "notice", LuaLogNotice },
+    { "warning", LuaLogWarning },
+    { "error", LuaLogError },
+    { "debug", LuaLogDebug },
+    { "config", LuaLogConfig },
+    { "perf", LuaLogPerf },
+    { NULL, NULL }
+    // clang-format on
+};
+
+int SCLuaLoadLogLib(lua_State *L)
+{
+    luaL_newlib(L, loglib);
+    return 1;
+}
diff --git a/src/util-lua-log.h b/src/util-lua-log.h
new file mode 100644 (file)
index 0000000..476f36f
--- /dev/null
@@ -0,0 +1,25 @@
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef SURICATA_UTIL_LUA_LOG_H
+#define SURICATA_UTIL_LUA_LOG_H
+
+#include <lua.h>
+
+int SCLuaLoadLogLib(lua_State *L);
+
+#endif /* SURICATA_UTIL_LUA_LOG_H */