Repository navigation
Expand file tree
/
Copy pathglog.go
More file actions
139 lines (115 loc) · 3.76 KB
/
Copy pathglog.go
File metadata and controls
139 lines (115 loc) · 3.76 KB
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
// 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 implements powerful and easy-to-use levelled logging functionality.
package glog
import (
"io"
"github.com/gogf/gf/internal/cmdenv"
"github.com/gogf/gf/os/grpool"
)
const (
LEVEL_ALL = LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
LEVEL_DEV = LEVEL_ALL
LEVEL_PROD = LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
LEVEL_DEBU = 1 << iota
LEVEL_INFO
LEVEL_NOTI
LEVEL_WARN
LEVEL_ERRO
LEVEL_CRIT
)
var (
// Default logger object, for package method usage
logger = New()
// Goroutine pool for async logging output.
asyncPool = grpool.New(1)
)
func init() {
SetDebug(cmdenv.Get("gf.glog.debug", true).Bool())
}
// Default returns the default logger.
func Default() *Logger {
return logger
}
// SetPath sets the directory path for file logging.
func SetPath(path string) error {
return logger.SetPath(path)
}
// GetPath returns the logging directory path for file logging.
// It returns empty string if no directory path set.
func GetPath() string {
return logger.GetPath()
}
// 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 SetFile(pattern string) {
logger.SetFile(pattern)
}
// SetLevel sets the default logging level.
func SetLevel(level int) {
logger.SetLevel(level)
}
// GetLevel returns the default logging level value.
func GetLevel() int {
return logger.GetLevel()
}
// 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 SetWriter(writer io.Writer) {
logger.SetWriter(writer)
}
// GetWriter returns the customized writer object, which implements the io.Writer interface.
// It returns nil if no customized writer set.
func GetWriter() io.Writer {
return logger.GetWriter()
}
// SetDebug enables/disables the debug level for default logger.
// The debug level is enbaled in default.
func SetDebug(debug bool) {
logger.SetDebug(debug)
}
// SetAsync enables/disables async logging output feature for default logger.
func SetAsync(enabled bool) {
logger.SetAsync(enabled)
}
// SetStdoutPrint sets whether ouptput the logging contents to stdout, which is true in default.
func SetStdoutPrint(enabled bool) {
logger.SetStdoutPrint(enabled)
}
// SetHeaderPrint sets whether output header of the logging contents, which is true in default.
func SetHeaderPrint(enabled bool) {
logger.SetHeaderPrint(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 SetPrefix(prefix string) {
logger.SetPrefix(prefix)
}
// SetFlags sets extra flags for logging output features.
func SetFlags(flags int) {
logger.SetFlags(flags)
}
// GetFlags returns the flags of logger.
func GetFlags() int {
return logger.GetFlags()
}
// PrintStack prints the caller stack,
// the optional parameter <skip> specify the skipped stack offset from the end point.
func PrintStack(skip ...int) {
logger.PrintStack(skip...)
}
// GetStack returns the caller stack content,
// the optional parameter <skip> specify the skipped stack offset from the end point.
func GetStack(skip ...int) string {
return logger.GetStack(skip...)
}
// SetStack enables/disables the stack feature in failure logging outputs.
func SetStack(enabled bool) {
logger.SetStack(enabled)
}