Introduce a new logging level for lua values

This commit is contained in:
2025-03-27 20:06:50 +01:00
parent da93770334
commit d236811cb9
2 changed files with 32 additions and 3 deletions

View File

@@ -28,6 +28,8 @@ const (
LevelDebug
// LevelTrace is for very detailed tracing information
LevelTrace
// LevelLua is specifically for output from Lua scripts
LevelLua
)
var levelNames = map[LogLevel]string{
@@ -36,6 +38,7 @@ var levelNames = map[LogLevel]string{
LevelInfo: "INFO",
LevelDebug: "DEBUG",
LevelTrace: "TRACE",
LevelLua: "LUA",
}
var levelColors = map[LogLevel]string{
@@ -44,6 +47,7 @@ var levelColors = map[LogLevel]string{
LevelInfo: "\033[1;32m", // Bold Green
LevelDebug: "\033[1;36m", // Bold Cyan
LevelTrace: "\033[1;35m", // Bold Magenta
LevelLua: "\033[1;34m", // Bold Blue
}
// ResetColor is the ANSI code to reset text color
@@ -84,6 +88,8 @@ func ParseLevel(levelStr string) LogLevel {
return LevelDebug
case "TRACE":
return LevelTrace
case "LUA":
return LevelLua
default:
return defaultLogLevel
}
@@ -307,7 +313,8 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
// log logs a message at the specified level
func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
if level > l.currentLevel {
// Always show LUA level logs regardless of the current log level
if level != LevelLua && level > l.currentLevel {
return
}
@@ -343,6 +350,11 @@ func (l *Logger) Trace(format string, args ...interface{}) {
l.log(LevelTrace, format, args...)
}
// Lua logs a Lua message
func (l *Logger) Lua(format string, args ...interface{}) {
l.log(LevelLua, format, args...)
}
// Global log functions that use DefaultLogger
// Error logs an error message using the default logger
@@ -385,6 +397,14 @@ func Trace(format string, args ...interface{}) {
DefaultLogger.Trace(format, args...)
}
// Lua logs a Lua message using the default logger
func Lua(format string, args ...interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.Lua(format, args...)
}
// LogPanic logs a panic error and its stack trace
func LogPanic(r interface{}) {
if DefaultLogger == nil {

View File

@@ -424,12 +424,21 @@ func BuildLuaScript(luaExpr string) string {
func printToGo(L *lua.LState) int {
top := L.GetTop()
args := make([]interface{}, top)
for i := 1; i <= top; i++ {
args[i-1] = L.Get(i)
}
message := fmt.Sprint(args...)
logger.Info("[Lua] %s", message)
// Format the message with proper spacing between arguments
var parts []string
for _, arg := range args {
parts = append(parts, fmt.Sprintf("%v", arg))
}
message := strings.Join(parts, " ")
// Use the LUA log level with a script tag
logger.Lua("%s", message)
return 0
}