commit 3f7488fa697af392f7fad2746bffad1fa07ddbaa Author: Star Date: Fri Oct 11 10:52:22 2024 +0800 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8530174 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.* +!.gitignore +go.sum diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d894367 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 apigo + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8812242 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# log for GoJS + +## set log config in env.yml + +```yaml +log: + level: info # specify the log level (error/warning/info/debug), default: info + file: out.log # specify the log file, default logs to standard output + splitTag: 20060102 # log file split format, refer to Golang date time format syntax, default does not split + sensitive: phone,password,secret,token,accessToken # fields to be desensitized in logs, separated by commas. The matching values in requests, responses, HTTP headers, and request/response JSON data indices will be desensitized +``` + +### more config see [ssgo/log](https://github.com/ssgo/log) + +## example + +```javascript +import log from 'log' +log.info('hello world') +log.error('some error', {name: 'abc'}) +``` + +## module.exports + +```ts +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 {} +``` + +## full api see [log.ts](https://apigo.cc/gojs/log/log.ts) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..457ec04 --- /dev/null +++ b/go.mod @@ -0,0 +1,23 @@ +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 +) + +require ( + github.com/dlclark/regexp2 v1.11.4 // indirect + github.com/fsnotify/fsnotify v1.7.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/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 + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/log.go b/log.go new file mode 100644 index 0000000..d92af33 --- /dev/null +++ b/log.go @@ -0,0 +1,46 @@ +package log + +import ( + _ "embed" + + "apigo.cc/gojs" + "apigo.cc/gojs/goja" +) + +//go:embed log.ts +var logTS string + +//go:embed README.md +var logMD string + +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)...) + 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)...) + 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)...) + 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)...) + return nil + }, + } + + gojs.Register("apigo.cc/gojs/log", gojs.Module{ + Object: obj, + Desc: "logger api by github.com/ssgo/log", + TsCode: logTS, + Example: logMD, + }) +} diff --git a/log.ts b/log.ts new file mode 100644 index 0000000..95de40e --- /dev/null +++ b/log.ts @@ -0,0 +1,13 @@ +// just for develop + +export default { + debug, + info, + warn, + 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 {} diff --git a/log_test.go b/log_test.go new file mode 100644 index 0000000..388459f --- /dev/null +++ b/log_test.go @@ -0,0 +1,15 @@ +package log_test + +import ( + "testing" + + "apigo.cc/gojs" + _ "apigo.cc/gojs/log" +) + +func Test(t *testing.T) { + gojs.Run(` + import log from 'apigo.cc/gojs/log' + log.info('some info', {extinfo: 'extinfo'}) + `, "") +}