-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlog.go
189 lines (145 loc) · 4.15 KB
/
log.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package log implements a simple logging package. It defines a type, Logger,
// with methods for formatting output. It also has a predefined 'standard'
// Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and
// Panic[f|ln], which are easier to use than creating a Logger manually.
// That logger writes to standard error and prints the date and time
// of each logged message.
// The Fatal functions call os.Exit(1) after writing the log message.
// The Panic functions call panic after writing the log message.
package golog
import (
"io"
"runtime"
"strings"
"sync"
)
type PartFunc func(*Logger)
// A Logger represents an active logging object that generates lines of
// output to an io.Writer. Each logging operation makes a single call to
// the Writer's Write method. A Logger can be used simultaneously from
// multiple goroutines; it guarantees to serialize access to the Writer.
type Logger struct {
mu sync.Mutex // ensures atomic writes; protects the following fields
buf []byte // for accumulating text to write
level Level
enableColor bool
name string
pkgName string
userData interface{}
colorFile *ColorFile
parts []PartFunc
output io.Writer
currColor Color
currLevel Level
currText string
currCondition bool
currContext interface{}
}
// New creates a new Logger. The out variable sets the
// destination to which log data will be written.
// The prefix appears at the beginning of each generated log line.
// The flag argument defines the logging properties.
const lineBuffer = 32
func getPackageName() string {
pc, _, _, _ := runtime.Caller(2)
raw := runtime.FuncForPC(pc).Name()
return strings.TrimSuffix(raw, ".init.ializers")
}
func New(name string) *Logger {
l := &Logger{
level: Level_Debug,
name: name,
pkgName: getPackageName(),
buf: make([]byte, 0, lineBuffer),
currCondition: true,
}
l.SetParts(LogPart_CurrLevel, LogPart_Name, LogPart_Time)
add(l)
return l
}
func (self *Logger) EnableColor(v bool) {
self.mu.Lock()
self.enableColor = v
self.mu.Unlock()
}
func (self *Logger) SetParts(f ...PartFunc) {
self.parts = []PartFunc{logPart_ColorBegin}
self.parts = append(self.parts, f...)
self.parts = append(self.parts, logPart_Text, logPart_ColorEnd, logPart_Line)
}
func (self *Logger) SetFullParts(f ...PartFunc) {
self.parts = f
}
// 二次开发接口
func (self *Logger) WriteRawString(s string) {
self.buf = append(self.buf, s...)
}
func (self *Logger) WriteRawByte(b byte) {
self.buf = append(self.buf, b)
}
func (self *Logger) WriteRawByteSlice(b []byte) {
self.buf = append(self.buf, b...)
}
func (self *Logger) Name() string {
return self.name
}
func (self *Logger) SetUserData(data interface{}) {
self.userData = data
}
func (self *Logger) UserData() interface{} {
return self.userData
}
func (self *Logger) PkgName() string {
return self.pkgName
}
func (self *Logger) Buff() []byte {
return self.buf
}
// 仅供LogPart访问
func (self *Logger) Text() string {
return self.currText
}
// 仅供LogPart访问
func (self *Logger) Context() interface{} {
return self.currContext
}
func (self *Logger) LogText(level Level, text string, ctx interface{}) {
// 防止日志并发打印导致的文本错位
self.mu.Lock()
defer self.mu.Unlock()
self.currLevel = level
self.currText = text
self.currContext = ctx
defer self.resetState()
if self.currLevel < self.level || !self.currCondition {
return
}
self.selectColorByText()
self.selectColorByLevel()
self.buf = self.buf[:0]
for _, p := range self.parts {
p(self)
}
if self.output != nil {
self.output.Write(self.buf)
} else {
globalWrite(self.buf)
}
}
func (self *Logger) Condition(value bool) *Logger {
self.mu.Lock()
self.currCondition = value
self.mu.Unlock()
return self
}
func (self *Logger) resetState() {
self.currColor = NoColor
self.currCondition = true
self.currContext = nil
}
func (self *Logger) IsDebugEnabled() bool {
return self.level == Level_Debug
}