luazap

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

luazap

GopherLua wrapper for zap.

Currently embedded as part of ZonePop so API stability is not guaranteed.

This lib only uses regular Logger and not SugaredLogger. In addition, not all of the features of zap are supported yet.

Quick Start

package main

import (
	"github.com/sapslaj/zonepop/config/luazap"
	lua "github.com/yuin/gopher-lua"
	"go.uber.org/zap"
)

func main() {
	// You can create a logger and use it like normal
	logger := zap.Must(zap.NewProduction())
	logger.Info("Startup")

	// Start up Lua
	state := lua.NewState()
	defer state.Close()

	// Load luazap
	state.PreloadModule(
		"zap",

		// Is it recommended to disable zap's built-in caller tracking because
		// luazap provides its own.
		luazap.NewLoader(logger.WithOptions(zap.WithCaller(false))),
	)

	state.DoFile("main.lua")
}
local log = require("zap")

log.info("info stuff")
log.warn("fields are supported", {extra_fields = "here"})
$ go run main.go
{"level":"info","ts":1678325793.447191,"caller":"main.go:12","msg":"Startup"}
{"level":"info","ts":1678325793.4476,"msg":"info stuff","caller":"main.lua:3"}
{"level":"warn","ts":1678325793.4476254,"msg":"fields are supported","caller":"main.lua:4","extra_fields":"here"}

Lua Functions

debug

zap.debug(msg [, fields])

Arguments:

msg string Message to write at debug level
fields table Optional fields to include in table format with string key, value pairs
error

zap.error(msg [, fields])

Arguments:

msg string Message to write at error level
fields table Optional fields to include in table format with string key, value pairs
fatal

zap.fatal(msg [, fields])

Arguments:

msg string Message to write at fatal level
fields table Optional fields to include in table format with string key, value pairs
info

zap.info(msg [, fields])

Arguments:

msg string Message to write at info level
fields table Optional fields to include in table format with string key, value pairs
warn

zap.warn(msg [, fields])

Arguments:

msg string Message to write at warn level
fields table Optional fields to include in table format with string key, value pairs
sprint

zap.sprint([... value]) -> string

Helper function for calling Go fmt.Sprint from Lua. This is helpful for getting the formatting of labels in a way that zap likes.

Arguments:

value any 1 or more values to call fmt.Sprint with

Returns:

string Result from fmt.Sprint
sprintf

zap.sprintf(format [, ... value]) -> string

Helper function for calling Go fmt.Sprintf from Lua. This is helpful for getting the formatting of labels in a way that zap likes.

Arguments:

format string Format string for fmt.Sprintf call
value any 1 or more values to call fmt.Sprintf with

Returns:

string Result from fmt.Sprintf

Documentation

Index

Constants

View Source
const DefaultCallerKey string = "caller"

Variables

This section is empty.

Functions

func NewLoader

func NewLoader(logger *zap.Logger, opts ...Option) func(*lua.LState) int

Types

type LuaZap

type LuaZap struct {
	Logger    *zap.Logger
	CallerKey string
}

func NewLuaZap

func NewLuaZap(logger *zap.Logger, opts ...Option) *LuaZap

func (*LuaZap) Loader

func (lz *LuaZap) Loader(L *lua.LState) int

func (*LuaZap) TableToZapFields

func (lz *LuaZap) TableToZapFields(lt *lua.LTable) []zapcore.Field

func (*LuaZap) ToGoValue

func (lz *LuaZap) ToGoValue(lv lua.LValue) any

type Option

type Option interface {
	// contains filtered or unexported methods
}

func AddCaller

func AddCaller() Option

func WithCaller

func WithCaller(enabled bool) Option

func WithCallerKey

func WithCallerKey(key string) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL