support log exception
add getExceptionString
This commit is contained in:
parent
6467191520
commit
85dee34f6b
13
go.mod
13
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
|
||||
)
|
||||
|
34
log.go
34
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))
|
||||
}
|
||||
|
9
log.ts
9
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 '' }
|
||||
|
Loading…
Reference in New Issue
Block a user