support log exception

add getExceptionString
This commit is contained in:
Star 2024-12-13 19:26:36 +08:00
parent 6467191520
commit 85dee34f6b
3 changed files with 41 additions and 15 deletions

13
go.mod
View File

@ -3,21 +3,20 @@ module apigo.cc/gojs/log
go 1.18 go 1.18
require ( require (
apigo.cc/gojs v0.0.1 apigo.cc/gojs v0.0.8
apigo.cc/gojs/util v0.0.1 github.com/ssgo/u v1.7.13
github.com/ssgo/u v1.7.9
) )
require ( require (
github.com/dlclark/regexp2 v1.11.4 // indirect 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/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // 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/log v1.7.7 // indirect
github.com/ssgo/standard v1.7.7 // indirect github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.27 // indirect github.com/ssgo/tool v0.4.27 // indirect
golang.org/x/sys v0.26.0 // indirect golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.19.0 // indirect golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

34
log.go
View File

@ -2,9 +2,11 @@ package log
import ( import (
_ "embed" _ "embed"
"reflect"
"apigo.cc/gojs" "apigo.cc/gojs"
"apigo.cc/gojs/goja" "apigo.cc/gojs/goja"
"github.com/ssgo/u"
) )
//go:embed log.ts //go:embed log.ts
@ -17,24 +19,27 @@ func init() {
obj := map[string]any{ obj := map[string]any{
"info": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "info": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) 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 return nil
}, },
"warn": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "warn": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) 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 return nil
}, },
"error": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "error": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) 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 return nil
}, },
"debug": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "debug": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) 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 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{ gojs.Register("apigo.cc/gojs/log", gojs.Module{
@ -44,3 +49,24 @@ func init() {
Example: logMD, 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))
}

9
log.ts
View File

@ -7,7 +7,8 @@ export default {
error, error,
} }
function debug(message:string, info?:Object): void {} function debug(message: string, info?: Object): void { }
function info(message:string, info?:Object): void {} function info(message: string, info?: Object): void { }
function warn(message:string, info?:Object): void {} function warn(message: string, info?: Object): void { }
function error(message:string, info?:Object): void {} function error(message: string, info?: Object): void { }
function getExceptionString(exception: any): string { return '' }