/
glog_logger_chaining.go
194 lines (179 loc) · 4.49 KB
/
glog_logger_chaining.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
190
191
192
193
194
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package glog
import (
"io"
"github.com/gogf/gf/g/os/gfile"
)
// To is a chaining function,
// which redirects current logging content output to the specified <writer>.
func (l *Logger) To(writer io.Writer) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetWriter(writer)
return logger
}
// Path is a chaining function,
// which sets the directory path to <path> for current logging content output.
func (l *Logger) Path(path string) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
if path != "" {
logger.SetPath(path)
}
return logger
}
// Cat is a chaining function,
// which sets the category to <category> for current logging content output.
// Param <category> can be hierarchical, eg: module/user.
func (l *Logger) Cat(category string) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
if logger.path != "" {
logger.SetPath(logger.path + gfile.Separator + category)
}
return logger
}
// File is a chaining function,
// which sets file name <pattern> for the current logging content output.
func (l *Logger) File(file string) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetFile(file)
return logger
}
// Level is a chaining function,
// which sets logging level for the current logging content output.
func (l *Logger) Level(level int) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetLevel(level)
return logger
}
// Skip is a chaining function,
// which sets stack skip for the current logging content output.
// It also affects the caller file path checks when line number printing enabled.
func (l *Logger) Skip(skip int) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetStackSkip(skip)
return logger
}
// Stack is a chaining function,
// which sets stack options for the current logging content output .
func (l *Logger) Stack(enabled bool, skip ...int) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetStack(enabled)
if len(skip) > 0 {
logger.SetStackSkip(skip[0])
}
return logger
}
// Stdout is a chaining function,
// which enables/disables stdout for the current logging content output.
// It's enabled in default.
func (l *Logger) Stdout(enabled ...bool) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
// stdout printing is enabled if <enabled> is not passed.
if len(enabled) > 0 && !enabled[0] {
logger.stdoutPrint = false
} else {
logger.stdoutPrint = true
}
return logger
}
// See Stdout.
// Deprecated.
func (l *Logger) StdPrint(enabled ...bool) *Logger {
return l.Stdout(enabled...)
}
// Header is a chaining function,
// which enables/disables log header for the current logging content output.
// It's enabled in default.
func (l *Logger) Header(enabled ...bool) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
// header is enabled if <enabled> is not passed.
if len(enabled) > 0 && !enabled[0] {
logger.SetHeaderPrint(false)
} else {
logger.SetHeaderPrint(true)
}
return logger
}
// Line is a chaining function,
// which enables/disables printing its caller file path along with its line number.
// The parameter <long> specified whether print the long absolute file path, eg: /a/b/c/d.go:23,
// or else short one: d.go:23.
func (l *Logger) Line(long ...bool) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
if len(long) > 0 && long[0] {
logger.flags |= F_FILE_LONG
} else {
logger.flags |= F_FILE_SHORT
}
return logger
}
// Async is a chaining function,
// which enables/disables async logging output feature.
func (l *Logger) Async(enabled ...bool) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
// async feature is enabled if <enabled> is not passed.
if len(enabled) > 0 && !enabled[0] {
logger.SetAsync(false)
} else {
logger.SetAsync(true)
}
return logger
}