Commit eb429319 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Extra metadata on log lines written through runtime loggers.

parent 3c3644bb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
### Added
- New API for client and runtime events known as event signals.
- Allow user account password updates from the developer console.
- Runtime log messages are now tagged with their source runtime type.

### Changed
- Default runtime HTTP key value is no longer the same as the default server key value.
+25 −11
Original line number Diff line number Diff line
@@ -16,20 +16,34 @@ package server

import (
	"fmt"
	"github.com/heroiclabs/nakama-common/runtime"
	nkruntime "github.com/heroiclabs/nakama-common/runtime"
	"go.uber.org/zap"
	"runtime"
	"strings"
)

type RuntimeGoLogger struct {
	logger *zap.Logger
}

func NewRuntimeGoLogger(logger *zap.Logger) runtime.Logger {
func NewRuntimeGoLogger(logger *zap.Logger) nkruntime.Logger {
	return &RuntimeGoLogger{
		logger: logger,
		logger: logger.With(zap.String("runtime", "go")),
	}
}

func (l *RuntimeGoLogger) getFileLine() zap.Field {
	_, filename, line, ok := runtime.Caller(2)
	if !ok {
		return zap.Skip()
	}
	filenameSplit := strings.SplitN(filename, "@", 2)
	if len(filenameSplit) >= 2 {
		filename = filenameSplit[1]
	}
	return zap.String("source", fmt.Sprintf("%v:%v", filename, line))
}

func (l *RuntimeGoLogger) Debug(format string, v ...interface{}) {
	if l.logger.Core().Enabled(zap.DebugLevel) {
		msg := fmt.Sprintf(format, v...)
@@ -47,14 +61,14 @@ func (l *RuntimeGoLogger) Info(format string, v ...interface{}) {
func (l *RuntimeGoLogger) Warn(format string, v ...interface{}) {
	if l.logger.Core().Enabled(zap.WarnLevel) {
		msg := fmt.Sprintf(format, v...)
		l.logger.Warn(msg)
		l.logger.Warn(msg, l.getFileLine())
	}
}

func (l *RuntimeGoLogger) Error(format string, v ...interface{}) {
	if l.logger.Core().Enabled(zap.ErrorLevel) {
		msg := fmt.Sprintf(format, v...)
		l.logger.Error(msg)
		l.logger.Error(msg, l.getFileLine())
	}
}

@@ -82,41 +96,41 @@ func (l *RuntimeGoLogger) Printf(format string, v ...interface{}) {
func (l *RuntimeGoLogger) Fatal(v ...interface{}) {
	if l.logger.Core().Enabled(zap.FatalLevel) {
		msg := fmt.Sprint(v...)
		l.logger.Fatal(msg)
		l.logger.Fatal(msg, l.getFileLine())
	}
}

func (l *RuntimeGoLogger) Fatalln(v ...interface{}) {
	if l.logger.Core().Enabled(zap.FatalLevel) {
		msg := fmt.Sprintln(v...)
		l.logger.Fatal(msg)
		l.logger.Fatal(msg, l.getFileLine())
	}
}

func (l *RuntimeGoLogger) Fatalf(format string, v ...interface{}) {
	if l.logger.Core().Enabled(zap.FatalLevel) {
		msg := fmt.Sprintf(format, v...)
		l.logger.Fatal(msg)
		l.logger.Fatal(msg, l.getFileLine())
	}
}

func (l *RuntimeGoLogger) Panic(v ...interface{}) {
	if l.logger.Core().Enabled(zap.PanicLevel) {
		msg := fmt.Sprint(v...)
		l.logger.Panic(msg)
		l.logger.Panic(msg, l.getFileLine())
	}
}

func (l *RuntimeGoLogger) Panicln(v ...interface{}) {
	if l.logger.Core().Enabled(zap.PanicLevel) {
		msg := fmt.Sprintln(v...)
		l.logger.Panic(msg)
		l.logger.Panic(msg, l.getFileLine())
	}
}

func (l *RuntimeGoLogger) Panicf(format string, v ...interface{}) {
	if l.logger.Core().Enabled(zap.PanicLevel) {
		msg := fmt.Sprintf(format, v...)
		l.logger.Panic(msg)
		l.logger.Panic(msg, l.getFileLine())
	}
}
+10 −3
Original line number Diff line number Diff line
@@ -1517,13 +1517,20 @@ func (n *RuntimeLuaNakamaModule) authenticateTokenGenerate(l *lua.LState) int {
	return 2
}

func (n *RuntimeLuaNakamaModule) getLuaModule(l *lua.LState) string {
	// "path/to/module.lua:123:"
	src := l.Where(-1)
	// "path/to/module.lua:123"
	return strings.TrimPrefix(src[:len(src)-1], n.config.GetRuntime().Path)
}

func (n *RuntimeLuaNakamaModule) loggerInfo(l *lua.LState) int {
	message := l.CheckString(1)
	if message == "" {
		l.ArgError(1, "expects message string")
		return 0
	}
	n.logger.Info(message)
	n.logger.Info(message, zap.String("runtime", "lua"))
	l.Push(lua.LString(message))
	return 1
}
@@ -1534,7 +1541,7 @@ func (n *RuntimeLuaNakamaModule) loggerWarn(l *lua.LState) int {
		l.ArgError(1, "expects message string")
		return 0
	}
	n.logger.Warn(message)
	n.logger.Warn(message, zap.String("runtime", "lua"))
	l.Push(lua.LString(message))
	return 1
}
@@ -1545,7 +1552,7 @@ func (n *RuntimeLuaNakamaModule) loggerError(l *lua.LState) int {
		l.ArgError(1, "expects message string")
		return 0
	}
	n.logger.Error(message)
	n.logger.Error(message, zap.String("runtime", "lua"), zap.String("source", n.getLuaModule(l)))
	l.Push(lua.LString(message))
	return 1
}