diff --git a/plugins/wasm-go/pkg/wrapper/log_wrapper.go b/plugins/wasm-go/pkg/wrapper/log_wrapper.go index 65c0aa346c..b8da27db39 100644 --- a/plugins/wasm-go/pkg/wrapper/log_wrapper.go +++ b/plugins/wasm-go/pkg/wrapper/log_wrapper.go @@ -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 == "" { @@ -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 == "" { @@ -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...) } diff --git a/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go b/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go index cce0456f8c..162f343b43 100644 --- a/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go +++ b/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go @@ -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] { @@ -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 {