Skip to content

Commit

Permalink
feat: support custom log
Browse files Browse the repository at this point in the history
  • Loading branch information
pepesi committed Nov 19, 2024
1 parent 1b1c08a commit 19dc46b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
45 changes: 30 additions & 15 deletions plugins/wasm-go/pkg/wrapper/log_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ const (
LogLevelCritical
)

type Log struct {
type Log interface {
Trace(msg string)
Tracef(format string, args ...interface{})
Debug(msg string)
Debugf(format string, args ...interface{})
Info(msg string)
Infof(format string, args ...interface{})
Warn(msg string)
Warnf(format string, args ...interface{})
Error(msg string)
Errorf(format string, args ...interface{})
Critical(msg string)
Criticalf(format string, args ...interface{})
}

type DefaultLog struct {
pluginName string
}

func (l Log) log(level LogLevel, msg string) {
func (l *DefaultLog) log(level LogLevel, msg string) {
requestIDRaw, _ := proxywasm.GetProperty([]string{"x_request_id"})
requestID := string(requestIDRaw)
if requestID == "" {
Expand All @@ -58,7 +73,7 @@ func (l Log) log(level LogLevel, msg string) {
}
}

func (l Log) logFormat(level LogLevel, format string, args ...interface{}) {
func (l *DefaultLog) logFormat(level LogLevel, format string, args ...interface{}) {
requestIDRaw, _ := proxywasm.GetProperty([]string{"x_request_id"})
requestID := string(requestIDRaw)
if requestID == "" {
Expand All @@ -81,50 +96,50 @@ func (l Log) logFormat(level LogLevel, format string, args ...interface{}) {
}
}

func (l Log) Trace(msg string) {
func (l *DefaultLog) Trace(msg string) {
l.log(LogLevelTrace, msg)
}

func (l Log) Tracef(format string, args ...interface{}) {
func (l *DefaultLog) Tracef(format string, args ...interface{}) {
l.logFormat(LogLevelTrace, format, args...)
}

func (l Log) Debug(msg string) {
func (l *DefaultLog) Debug(msg string) {
l.log(LogLevelDebug, msg)
}

func (l Log) Debugf(format string, args ...interface{}) {
func (l *DefaultLog) Debugf(format string, args ...interface{}) {
l.logFormat(LogLevelDebug, format, args...)
}

func (l Log) Info(msg string) {
func (l *DefaultLog) Info(msg string) {
l.log(LogLevelInfo, msg)
}

func (l Log) Infof(format string, args ...interface{}) {
func (l *DefaultLog) Infof(format string, args ...interface{}) {
l.logFormat(LogLevelInfo, format, args...)
}

func (l Log) Warn(msg string) {
func (l *DefaultLog) Warn(msg string) {
l.log(LogLevelWarn, msg)
}

func (l Log) Warnf(format string, args ...interface{}) {
func (l *DefaultLog) Warnf(format string, args ...interface{}) {
l.logFormat(LogLevelWarn, format, args...)
}

func (l Log) Error(msg string) {
func (l *DefaultLog) Error(msg string) {
l.log(LogLevelError, msg)
}

func (l Log) Errorf(format string, args ...interface{}) {
func (l *DefaultLog) Errorf(format string, args ...interface{}) {
l.logFormat(LogLevelError, format, args...)
}

func (l Log) Critical(msg string) {
func (l *DefaultLog) Critical(msg string) {
l.log(LogLevelCritical, msg)
}

func (l Log) Criticalf(format string, args ...interface{}) {
func (l *DefaultLog) Criticalf(format string, args ...interface{}) {
l.logFormat(LogLevelCritical, format, args...)
}
11 changes: 10 additions & 1 deletion plugins/wasm-go/pkg/wrapper/plugin_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func SetCtx[PluginConfig any](pluginName string, setFuncs ...SetPluginFunc[Plugi
proxywasm.SetVMContext(NewCommonVmCtx(pluginName, setFuncs...))
}

func SetCtxWithLoger[PluginConfig any](pluginName string, log Log, setFuncs ...SetPluginFunc[PluginConfig]) {
proxywasm.SetVMContext(NewCommonVmCtxWithLoger(pluginName, log, setFuncs...))
}

type SetPluginFunc[PluginConfig any] func(*CommonVmCtx[PluginConfig])

func ParseConfigBy[PluginConfig any](f ParseConfigFunc[PluginConfig]) SetPluginFunc[PluginConfig] {
Expand Down Expand Up @@ -164,9 +168,14 @@ func parseEmptyPluginConfig[PluginConfig any](gjson.Result, *PluginConfig, Log)
}

func NewCommonVmCtx[PluginConfig any](pluginName string, setFuncs ...SetPluginFunc[PluginConfig]) *CommonVmCtx[PluginConfig] {
log := &DefaultLog{pluginName}
return NewCommonVmCtxWithLoger(pluginName, log, setFuncs...)
}

func NewCommonVmCtxWithLoger[PluginConfig any](pluginName string, log Log, setFuncs ...SetPluginFunc[PluginConfig]) *CommonVmCtx[PluginConfig] {
ctx := &CommonVmCtx[PluginConfig]{
pluginName: pluginName,
log: Log{pluginName},
log: log,
hasCustomConfig: true,
}
for _, set := range setFuncs {
Expand Down

0 comments on commit 19dc46b

Please sign in to comment.