diff --git a/go.mod b/go.mod index 457ec04..0d5ae3f 100644 --- a/go.mod +++ b/go.mod @@ -3,21 +3,20 @@ module apigo.cc/gojs/log go 1.18 require ( - apigo.cc/gojs v0.0.1 - apigo.cc/gojs/util v0.0.1 - github.com/ssgo/u v1.7.9 + apigo.cc/gojs v0.0.8 + github.com/ssgo/u v1.7.13 ) require ( github.com/dlclark/regexp2 v1.11.4 // indirect - github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/fsnotify/fsnotify v1.8.0 // indirect github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect - github.com/ssgo/config v1.7.7 // indirect + github.com/ssgo/config v1.7.9 // indirect github.com/ssgo/log v1.7.7 // indirect github.com/ssgo/standard v1.7.7 // indirect github.com/ssgo/tool v0.4.27 // indirect - golang.org/x/sys v0.26.0 // indirect - golang.org/x/text v0.19.0 // indirect + golang.org/x/sys v0.28.0 // indirect + golang.org/x/text v0.21.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/log.go b/log.go index d92af33..661a4b0 100644 --- a/log.go +++ b/log.go @@ -2,9 +2,11 @@ package log import ( _ "embed" + "reflect" "apigo.cc/gojs" "apigo.cc/gojs/goja" + "github.com/ssgo/u" ) //go:embed log.ts @@ -17,24 +19,27 @@ func init() { obj := map[string]any{ "info": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := gojs.MakeArgs(&argsIn, vm).Check(1) - args.Logger.Info(args.Str(0), args.Map2Array(1)...) + args.Logger.Info(getExceptionString(argsIn, vm), args.Map2Array(1)...) return nil }, "warn": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := gojs.MakeArgs(&argsIn, vm).Check(1) - args.Logger.Warning(args.Str(0), args.Map2Array(1)...) + args.Logger.Warning(getExceptionString(argsIn, vm), args.Map2Array(1)...) return nil }, "error": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := gojs.MakeArgs(&argsIn, vm).Check(1) - args.Logger.Error(args.Str(0), args.Map2Array(1)...) + args.Logger.Error(getExceptionString(argsIn, vm), args.Map2Array(1)...) return nil }, "debug": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := gojs.MakeArgs(&argsIn, vm).Check(1) - args.Logger.Debug(args.Str(0), args.Map2Array(1)...) + args.Logger.Debug(getExceptionString(argsIn, vm), args.Map2Array(1)...) return nil }, + "getExceptionString": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { + return vm.ToValue(getExceptionString(argsIn, vm)) + }, } gojs.Register("apigo.cc/gojs/log", gojs.Module{ @@ -44,3 +49,24 @@ func init() { Example: logMD, }) } + +func getExceptionString(args goja.FunctionCall, vm *goja.Runtime) string { + if len(args.Arguments) == 0 { + return "" + } + if args.Argument(0).ExportType() != nil && args.Argument(0).ExportType().Kind() == reflect.Map { + ex := args.Argument(0).ToObject(vm) + message := ex.Get("message") + if message != nil { + messageStr := u.String(message.Export()) + if messageStr != "" { + stack := ex.Get("stack") + if stack != nil { + messageStr += "\n" + u.String(stack.Export()) + } + return messageStr + } + } + } + return u.String(args.Argument(0)) +} diff --git a/log.ts b/log.ts index 95de40e..de29611 100644 --- a/log.ts +++ b/log.ts @@ -7,7 +7,8 @@ export default { error, } -function debug(message:string, info?:Object): void {} -function info(message:string, info?:Object): void {} -function warn(message:string, info?:Object): void {} -function error(message:string, info?:Object): void {} +function debug(message: string, info?: Object): void { } +function info(message: string, info?: Object): void { } +function warn(message: string, info?: Object): void { } +function error(message: string, info?: Object): void { } +function getExceptionString(exception: any): string { return '' }