-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.go
153 lines (126 loc) · 3.1 KB
/
console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package licheejs
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/dop251/goja"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type ConsoleCallBack = func(args ...any)
type console struct {
logger *zap.Logger
CallBack ConsoleCallBack
}
type LogOutMode int
const (
LOM_RELEASE LogOutMode = iota
LOM_DEBUG
)
func getJsonEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder // 修改时间编码器
// 在日志文件中使用大写字母记录日志级别
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
// NewConsoleEncoder 打印更符合人们观察的方式
return zapcore.NewJSONEncoder(encoderConfig)
}
func getConsoleEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder // 修改时间编码器
// 在日志文件中使用大写字母记录日志级别
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
// NewConsoleEncoder 打印更符合人们观察的方式
return zapcore.NewConsoleEncoder(encoderConfig)
}
func getLogWriter(path string) (zapcore.WriteSyncer, func(), error) {
return zap.Open(path)
}
func newZap(path string, mod LogOutMode) (*zap.Logger, func(), error) {
ws, close, err := getLogWriter(path)
if err != nil {
return nil, nil, err
}
var core zapcore.Core
if mod == LOM_DEBUG {
core = zapcore.NewTee(
zapcore.NewCore(getConsoleEncoder(), zapcore.AddSync(os.Stdout), zapcore.DebugLevel),
zapcore.NewCore(getJsonEncoder(), ws, zap.DebugLevel),
)
} else {
core = zapcore.NewTee(
zapcore.NewCore(getJsonEncoder(), ws, zap.DebugLevel),
)
}
log := zap.New(core, zap.AddCaller())
return log, close, nil
}
func newConsole(log *zap.Logger, cb ConsoleCallBack) *console {
c := &console{logger: log}
c.CallBack = cb
return c
}
func (c console) log(level zapcore.Level, args ...goja.Value) {
var strs strings.Builder
for i := 0; i < len(args); i++ {
if i > 0 {
strs.WriteString(" ")
}
strs.WriteString(c.valueString(args[i]))
}
msg := strs.String()
flag := ""
switch level { //nolint:exhaustive
case zapcore.DebugLevel:
{
c.logger.Debug(msg)
flag = "[debug]"
}
case zapcore.InfoLevel:
{
c.logger.Info(msg)
flag = "[info]"
}
case zapcore.WarnLevel:
{
c.logger.Warn(msg)
flag = "[warn]"
}
case zapcore.ErrorLevel:
{
c.logger.Error(msg)
flag = "[error]"
}
}
// 写入回调
if c.CallBack != nil {
c.CallBack(fmt.Sprintf("%s %s", flag, msg))
}
}
func (c console) Log(args ...goja.Value) {
c.Info(args...)
}
func (c console) Debug(args ...goja.Value) {
c.log(zapcore.DebugLevel, args...)
}
func (c console) Info(args ...goja.Value) {
c.log(zapcore.InfoLevel, args...)
}
func (c console) Warn(args ...goja.Value) {
c.log(zapcore.WarnLevel, args...)
}
func (c console) Error(args ...goja.Value) {
c.log(zapcore.ErrorLevel, args...)
}
func (c console) valueString(v goja.Value) string {
mv, ok := v.(json.Marshaler)
if !ok {
return v.String()
}
b, err := json.Marshal(mv)
if err != nil {
return v.String()
}
return string(b)
}