-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
glog_logger_config.go
208 lines (185 loc) · 6 KB
/
glog_logger_config.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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 (
"errors"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
"io"
"strings"
)
// Config is the configuration object for logger.
type Config struct {
Writer io.Writer // Customized io.Writer.
Flags int // Extra flags for logging output features.
Path string // Logging directory path.
File string // Format for logging file.
Level int // Output level.
Prefix string // Prefix string for every logging content.
StSkip int // Skip count for stack.
StStatus int // Stack status(1: enabled - default; 0: disabled)
StFilter string // Stack string filter.
HeaderPrint bool `c:"header"` // Print header or not(true in default).
StdoutPrint bool `c:"stdout"` // Output to stdout or not(true in default).
LevelPrefixes map[int]string // Logging level to its prefix string mapping.
}
// DefaultConfig returns the default configuration for logger.
func DefaultConfig() Config {
c := Config{
File: gDEFAULT_FILE_FORMAT,
Flags: F_TIME_STD,
Level: LEVEL_ALL,
StStatus: 1,
HeaderPrint: true,
StdoutPrint: true,
LevelPrefixes: make(map[int]string, len(defaultLevelPrefixes)),
}
for k, v := range defaultLevelPrefixes {
c.LevelPrefixes[k] = v
}
if !defaultDebug {
c.Level = c.Level & ^LEVEL_DEBU
}
return c
}
// SetConfig set configurations for the logger.
func (l *Logger) SetConfig(config Config) error {
l.config = config
// Necessary validation.
if config.Path != "" {
if err := l.SetPath(config.Path); err != nil {
intlog.Error(err)
return err
}
}
intlog.Printf("SetConfig: %+v", l.config)
return nil
}
// SetConfigWithMap set configurations with map for the logger.
func (l *Logger) SetConfigWithMap(m map[string]interface{}) error {
if m == nil || len(m) == 0 {
return errors.New("configuration cannot be empty")
}
// Change string configuration to int value for level.
levelKey, levelValue := gutil.MapPossibleItemByKey(m, "level")
if levelValue != nil {
switch gconv.String(levelValue) {
case "all":
m[levelKey] = LEVEL_ALL
case "dev":
m[levelKey] = LEVEL_DEV
case "prod":
m[levelKey] = LEVEL_PROD
}
}
config := DefaultConfig()
err := gconv.Struct(m, &config)
if err != nil {
return err
}
return l.SetConfig(config)
}
// SetLevel sets the logging level.
func (l *Logger) SetLevel(level int) {
l.config.Level = level
}
// GetLevel returns the logging level value.
func (l *Logger) GetLevel() int {
return l.config.Level
}
// SetDebug enables/disables the debug level for logger.
// The debug level is enabled in default.
func (l *Logger) SetDebug(debug bool) {
if debug {
l.config.Level = l.config.Level | LEVEL_DEBU
} else {
l.config.Level = l.config.Level & ^LEVEL_DEBU
}
}
// SetAsync enables/disables async logging output feature.
func (l *Logger) SetAsync(enabled bool) {
if enabled {
l.config.Flags = l.config.Flags | F_ASYNC
} else {
l.config.Flags = l.config.Flags & ^F_ASYNC
}
}
// SetFlags sets extra flags for logging output features.
func (l *Logger) SetFlags(flags int) {
l.config.Flags = flags
}
// GetFlags returns the flags of logger.
func (l *Logger) GetFlags() int {
return l.config.Flags
}
// SetStack enables/disables the stack feature in failure logging outputs.
func (l *Logger) SetStack(enabled bool) {
if enabled {
l.config.StStatus = 1
} else {
l.config.StStatus = 0
}
}
// SetStackSkip sets the stack offset from the end point.
func (l *Logger) SetStackSkip(skip int) {
l.config.StSkip = skip
}
// SetStackFilter sets the stack filter from the end point.
func (l *Logger) SetStackFilter(filter string) {
l.config.StFilter = filter
}
// SetWriter sets the customized logging <writer> for logging.
// The <writer> object should implements the io.Writer interface.
// Developer can use customized logging <writer> to redirect logging output to another service,
// eg: kafka, mysql, mongodb, etc.
func (l *Logger) SetWriter(writer io.Writer) {
l.config.Writer = writer
}
// GetWriter returns the customized writer object, which implements the io.Writer interface.
// It returns nil if no writer previously set.
func (l *Logger) GetWriter() io.Writer {
return l.config.Writer
}
// SetPath sets the directory path for file logging.
func (l *Logger) SetPath(path string) error {
if path == "" {
return errors.New("logging path is empty")
}
if !gfile.Exists(path) {
if err := gfile.Mkdir(path); err != nil {
//fmt.Fprintln(os.Stderr, fmt.Sprintf(`[glog] mkdir "%s" failed: %s`, path, err.Error()))
return err
}
}
l.config.Path = strings.TrimRight(path, gfile.Separator)
return nil
}
// GetPath returns the logging directory path for file logging.
// It returns empty string if no directory path set.
func (l *Logger) GetPath() string {
return l.config.Path
}
// SetFile sets the file name <pattern> for file logging.
// Datetime pattern can be used in <pattern>, eg: access-{Ymd}.log.
// The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log
func (l *Logger) SetFile(pattern string) {
l.config.File = pattern
}
// SetStdoutPrint sets whether output the logging contents to stdout, which is true in default.
func (l *Logger) SetStdoutPrint(enabled bool) {
l.config.StdoutPrint = enabled
}
// SetHeaderPrint sets whether output header of the logging contents, which is true in default.
func (l *Logger) SetHeaderPrint(enabled bool) {
l.config.HeaderPrint = enabled
}
// SetPrefix sets prefix string for every logging content.
// Prefix is part of header, which means if header output is shut, no prefix will be output.
func (l *Logger) SetPrefix(prefix string) {
l.config.Prefix = prefix
}