/
console.go
158 lines (132 loc) · 3.05 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
154
155
156
157
158
// Copyright 2022 ByteDance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
package utils
import (
"bytes"
"fmt"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/byted-apaas/server-common-go/structs"
)
const (
red = 31
green = 32
yellow = 33
blue = 36
gray = 37
)
type LogFormatter struct {
}
func (m *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
level, color := LevelInfo(entry.Level)
msg := fmt.Sprintf("%s %s %s", level, entry.Time.Format("15:04:05.999"), entry.Message)
if entry.Level == logrus.DebugLevel {
msg = fmt.Sprintf("%s %s %s", entry.Time.Format("15:04:05.999"), level, entry.Message)
}
b.WriteString(fmt.Sprintf("\x1b[%dm%s\x1b[0m\n", color, msg))
return b.Bytes(), nil
}
const (
// Log Type
Console = "Console"
System = "System"
Result = "Result"
WebIDESource = "apaas-ide"
ISWebIDE = "IS_WEB_IDE"
)
type WebIDELogFormatter struct {
}
func (m *WebIDELogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
// 构建日志结构体
log := structs.WebIDELog{
Source: WebIDESource,
Time: time.Now(),
Type: Console,
Level: WebIDELevelInfo(entry.Level),
Message: entry.Message,
}
if entry.Level == logrus.DebugLevel {
log.Type = Result
}
msg, err := JsonMarshalBytes(log)
if err != nil {
return nil, err
}
b.WriteString(fmt.Sprintf("%s\n", msg))
return b.Bytes(), nil
}
func WebIDELevelInfo(level logrus.Level) string {
switch level {
case logrus.InfoLevel:
return "info"
case logrus.ErrorLevel:
return "error"
case logrus.WarnLevel:
return "warn"
default:
return "info"
}
}
func LevelInfo(level logrus.Level) (string, int) {
switch level {
case logrus.InfoLevel:
return "[INFO] ", blue
case logrus.WarnLevel:
return "[WARN] ", yellow
case logrus.ErrorLevel:
return "[ERROR]", red
case logrus.DebugLevel:
return "Result ", green
}
return "[NULL] ", gray
}
type ConsoleLogger struct {
logID string
}
func GetConsoleLogger(logIDs ...string) *ConsoleLogger {
if os.Getenv(ISWebIDE) == "true" {
logrus.SetFormatter(&WebIDELogFormatter{})
} else {
logrus.SetFormatter(&LogFormatter{})
}
logrus.SetLevel(logrus.TraceLevel)
l := &ConsoleLogger{}
if len(logIDs) > 0 && logIDs[0] != "" {
l.logID = logIDs[0]
}
return l
}
func (c *ConsoleLogger) Infof(format string, args ...interface{}) {
if c.logID != "" {
format = fmt.Sprintf("%s %s", c.logID, format)
}
logrus.Infof(format, args...)
}
func (c *ConsoleLogger) Warnf(format string, args ...interface{}) {
if c.logID != "" {
format = fmt.Sprintf("%s %s", c.logID, format)
}
logrus.Warnf(format, args...)
}
func (c *ConsoleLogger) Errorf(format string, args ...interface{}) {
if c.logID != "" {
format = fmt.Sprintf("%s %s", c.logID, format)
}
logrus.Errorf(format, args...)
}
func (c *ConsoleLogger) Result(format string, args ...interface{}) {
logrus.Debugf(format, args...)
}