mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 01:57:02 +00:00
lua: Expose logger object (#407)
Allows Lua scripts to add entries to Inbuckets log Closes #327 Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -47,14 +47,13 @@ func New(conf config.Lua, extHost *extension.Host) (*Host, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewFromReader(extHost, bufio.NewReader(file), scriptPath)
|
||||
return NewFromReader(logContext.Logger(), extHost, bufio.NewReader(file), scriptPath)
|
||||
}
|
||||
|
||||
// NewFromReader constructs a new Lua Host, loading Lua source from the provided reader.
|
||||
// The provided path is used in logging and error messages.
|
||||
func NewFromReader(extHost *extension.Host, r io.Reader, path string) (*Host, error) {
|
||||
logContext := log.With().Str("module", "lua")
|
||||
logger := logContext.Str("phase", "startup").Str("path", path).Logger()
|
||||
func NewFromReader(logger zerolog.Logger, extHost *extension.Host, r io.Reader, path string) (*Host, error) {
|
||||
startLogger := logger.With().Str("phase", "startup").Str("path", path).Logger()
|
||||
|
||||
// Pre-parse, and compile script.
|
||||
chunk, err := parse.Parse(r, path)
|
||||
@@ -67,10 +66,10 @@ func NewFromReader(extHost *extension.Host, r io.Reader, path string) (*Host, er
|
||||
}
|
||||
|
||||
// Build the pool and confirm LState is retrievable.
|
||||
pool := newStatePool(proto)
|
||||
h := &Host{extHost: extHost, pool: pool, logContext: logContext}
|
||||
pool := newStatePool(logger, proto)
|
||||
h := &Host{extHost: extHost, pool: pool, logContext: logger.With()}
|
||||
if ls, err := pool.getState(); err == nil {
|
||||
h.wireFunctions(logger, ls)
|
||||
h.wireFunctions(startLogger, ls)
|
||||
|
||||
// State creation works, put it back.
|
||||
pool.putState(ls)
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"github.com/inbucket/inbucket/v3/pkg/extension"
|
||||
"github.com/inbucket/inbucket/v3/pkg/extension/event"
|
||||
"github.com/inbucket/inbucket/v3/pkg/extension/luahost"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
@@ -56,10 +58,26 @@ func TestEmptyScript(t *testing.T) {
|
||||
script := ""
|
||||
extHost := extension.NewHost()
|
||||
|
||||
_, err := luahost.NewFromReader(extHost, strings.NewReader(script), "test.lua")
|
||||
_, err := luahost.NewFromReader(zerolog.Nop(), extHost, strings.NewReader(script), "test.lua")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
script := `
|
||||
local logger = require("logger")
|
||||
logger.info("_test log entry_", {})
|
||||
`
|
||||
|
||||
extHost := extension.NewHost()
|
||||
output := &strings.Builder{}
|
||||
logger := zerolog.New(output)
|
||||
|
||||
_, err := luahost.NewFromReader(logger, extHost, strings.NewReader(script), "test.lua")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Contains(t, output.String(), "_test log entry_")
|
||||
}
|
||||
|
||||
func TestAfterMessageDeleted(t *testing.T) {
|
||||
// Register lua event listener, setup notify channel.
|
||||
script := `
|
||||
@@ -73,7 +91,7 @@ func TestAfterMessageDeleted(t *testing.T) {
|
||||
end
|
||||
`
|
||||
extHost := extension.NewHost()
|
||||
luaHost, err := luahost.NewFromReader(extHost, strings.NewReader(LuaInit+script), "test.lua")
|
||||
luaHost, err := luahost.NewFromReader(zerolog.Nop(), extHost, strings.NewReader(LuaInit+script), "test.lua")
|
||||
require.NoError(t, err)
|
||||
notify := luaHost.CreateChannel("notify")
|
||||
|
||||
@@ -104,7 +122,7 @@ func TestAfterMessageStored(t *testing.T) {
|
||||
end
|
||||
`
|
||||
extHost := extension.NewHost()
|
||||
luaHost, err := luahost.NewFromReader(extHost, strings.NewReader(LuaInit+script), "test.lua")
|
||||
luaHost, err := luahost.NewFromReader(zerolog.Nop(), extHost, strings.NewReader(LuaInit+script), "test.lua")
|
||||
require.NoError(t, err)
|
||||
notify := luaHost.CreateChannel("notify")
|
||||
|
||||
@@ -130,7 +148,7 @@ func TestBeforeMailAccepted(t *testing.T) {
|
||||
end
|
||||
`
|
||||
extHost := extension.NewHost()
|
||||
_, err := luahost.NewFromReader(extHost, strings.NewReader(script), "test.lua")
|
||||
_, err := luahost.NewFromReader(zerolog.Nop(), extHost, strings.NewReader(script), "test.lua")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Send event to be accepted.
|
||||
|
||||
@@ -5,7 +5,9 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/cjoudrey/gluahttp"
|
||||
"github.com/cosmotek/loguago"
|
||||
"github.com/inbucket/gopher-json"
|
||||
"github.com/rs/zerolog"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
@@ -14,12 +16,14 @@ type statePool struct {
|
||||
funcProto *lua.FunctionProto // Compiled lua.
|
||||
states []*lua.LState // Pool of available LStates.
|
||||
channels map[string]chan lua.LValue // Global interop channels.
|
||||
logger zerolog.Logger // Logger exported to Lua scripts.
|
||||
}
|
||||
|
||||
func newStatePool(funcProto *lua.FunctionProto) *statePool {
|
||||
func newStatePool(logger zerolog.Logger, funcProto *lua.FunctionProto) *statePool {
|
||||
return &statePool{
|
||||
funcProto: funcProto,
|
||||
channels: make(map[string]chan lua.LValue),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,9 +31,12 @@ func newStatePool(funcProto *lua.FunctionProto) *statePool {
|
||||
func (lp *statePool) newState() (*lua.LState, error) {
|
||||
ls := lua.NewState()
|
||||
|
||||
logger := loguago.NewLogger(lp.logger)
|
||||
|
||||
// Load supplemental native modules.
|
||||
ls.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader)
|
||||
ls.PreloadModule("json", json.Loader)
|
||||
ls.PreloadModule("logger", logger.Loader)
|
||||
|
||||
// Setup channels.
|
||||
for name, ch := range lp.channels {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
@@ -23,7 +24,7 @@ func makeEmptyPool() *statePool {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return newStatePool(proto)
|
||||
return newStatePool(zerolog.Nop(), proto)
|
||||
}
|
||||
|
||||
func TestPoolGetsDistinct(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user