1
0
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:
James Hillyerd
2023-10-23 16:06:19 -07:00
committed by GitHub
parent b1acff08a3
commit d6c23df241
6 changed files with 47 additions and 13 deletions

View File

@@ -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)