-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorlog.go
155 lines (138 loc) · 3.8 KB
/
colorlog.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
package colorlog
import (
"fmt"
"github.com/ThreeKing2018/gocolor"
"io"
"log"
"os"
"sync"
)
// 定义日志等级
const (
DEBUG = iota // 用于调度,最低级
INFO //输出普通信息,常用
WARNING //输出警告,非错误信息,又比较重要
ERROR //错误,属严重信息
DEFAULT_FLAG = log.LstdFlags
LSHORTFILE_FLAG = log.Lshortfile | log.LstdFlags
)
//定义日志接口
type ColorLogger interface {
Debug(format string, s ...interface{})
Info(format string, s ...interface{})
Warn(format string, s ...interface{})
Error(format string, s ...interface{})
SetLevel(level int) //设置等级
}
//var isColorChan = make(chan bool, 1)
type Logger struct {
Level int //日志等级
debug *log.Logger
info *log.Logger
warning *log.Logger
error *log.Logger
IsColor bool //是否使用带颜色的日志
Depth int //详情深度
Prefix string //前缀
wg *sync.WaitGroup
}
//对外初使日志
func New(w io.Writer, isColor bool) ColorLogger {
return InitWriteLogger(w, 2, LSHORTFILE_FLAG, isColor)
}
//默认的日志
var defaultLogger = InitWriteLogger(os.Stdout, 2, DEFAULT_FLAG, true)
//初使写入日志,写入到一个buffer里
func InitWriteLogger(w io.Writer, depth int, flag int, isColor bool) ColorLogger {
logger := new(Logger)
logger.wg = new(sync.WaitGroup)
logger.IsColor = isColor
logger.Depth = depth
//初使每个等级的日志
logger.debug = log.New(w, logger.setColorString(DEBUG, "[BUG]"), flag)
logger.info = log.New(w, logger.setColorString(INFO, "[INF]"), flag)
logger.warning = log.New(w, logger.setColorString(WARNING, "[WAR]"), flag)
logger.error = log.New(w, logger.setColorString(ERROR, "[ERR]"), flag)
logger.SetLevel(DEBUG) //初使一下等级啦
return logger
}
//设置不同字体颜色
func (l *Logger) setColor(level int, format string, args ...interface{}) string {
if false == l.IsColor {
return fmt.Sprintf(format, args...)
}
switch level {
case DEBUG:
return gocolor.SMagenta(format, args...)
case INFO:
return gocolor.SGreen(format, args...)
case WARNING:
return gocolor.SYellow(format, args...)
case ERROR:
return gocolor.SRed(format, args...)
default:
return fmt.Sprintf(format, args...)
}
}
//设置不同背景颜色
func (l *Logger) setColorString(level int, format string, args ...interface{}) string {
if false == l.IsColor {
return fmt.Sprintf(format, args...)
}
switch level {
case DEBUG:
return gocolor.SMagentaBG(format, args...)
case INFO:
return gocolor.SGreenBG(format, args...)
case WARNING:
return gocolor.SYellowBG(format, args...)
case ERROR:
return gocolor.SRedBG(format, args...)
default:
return fmt.Sprintf(format, args...)
}
}
//设置等级,默认全输出
func (l *Logger) SetLevel(level int) {
l.Level = level
}
//用于调度的日志
func (l *Logger) Debug(format string, s ...interface{}) {
if l.Level > DEBUG {
return
}
l.debug.Output(l.Depth, l.setColor(DEBUG, format, s...))
}
//输出普通信息
func (l *Logger) Info(format string, s ...interface{}) {
if l.Level > INFO {
return
}
l.info.Output(l.Depth, l.setColor(INFO, format, s...))
}
//输出警告信息
func (l *Logger) Warn(format string, s ...interface{}) {
if l.Level > WARNING {
return
}
l.warning.Output(l.Depth, l.setColor(WARNING, format, s...))
}
//输出错误信息
func (l *Logger) Error(format string, s ...interface{}) {
if l.Level > ERROR {
return
}
l.error.Output(l.Depth, l.setColor(ERROR, format, s...))
}
func Debug(format string, s ...interface{}) {
defaultLogger.Debug(format, s...)
}
func Info(format string, s ...interface{}) {
defaultLogger.Info(format, s...)
}
func Warn(format string, s ...interface{}) {
defaultLogger.Warn(format, s...)
}
func Error(format string, s ...interface{}) {
defaultLogger.Error(format, s...)
}