-
Notifications
You must be signed in to change notification settings - Fork 11
/
iFormatter.go
48 lines (39 loc) · 1.13 KB
/
iFormatter.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
package flog
import (
"encoding/json"
"fmt"
"github.com/farseer-go/fs/core/eumLogLevel"
)
// IFormatter 日志格式
type IFormatter interface {
Formatter(log *LogData) string
}
// JsonFormatter json格式输出
type JsonFormatter struct {
}
func (r JsonFormatter) Formatter(log *LogData) string {
marshal, _ := json.Marshal(LogData{
CreateAt: log.CreateAt,
LogLevel: log.LogLevel,
Component: log.Component,
Content: mustCompile.ReplaceAllString(log.Content, ""),
newLine: log.newLine,
})
return string(marshal)
}
// TextFormatter 文本格式输出
type TextFormatter struct {
config levelFormat
}
func (r TextFormatter) Formatter(log *LogData) string {
var logLevelString string
if log.LogLevel != eumLogLevel.NoneLevel {
logLevelString = Colors[log.LogLevel]("[" + log.LogLevel.ToString() + "]")
} else if log.Component != "" {
logLevelString = Colors[0]("[" + log.Component + "]")
}
if logLevelString != "" {
return fmt.Sprintf("%s %s %s", log.CreateAt.ToString(r.config.TimeFormat), logLevelString, log.Content)
}
return fmt.Sprintf("%s %s", log.CreateAt.ToString(r.config.TimeFormat), log.Content)
}