mirror of https://github.com/OISF/suricata
lua: convert log functions to suricata.log lib
Convert the Lua global functions for logging (SCLogInfo, etc) to a Lua lib names "suricata.log". Ticket: #7727pull/13339/head
parent
9046f30731
commit
278a9c3806
@ -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``.
|
||||
@ -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;
|
||||
}
|
||||
@ -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 */
|
||||
Loading…
Reference in New Issue