forked from fwhezfwhez/tcpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
58 lines (49 loc) · 1.12 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
package tcpx
import (
"fmt"
"github.com/fwhezfwhez/errorx"
"log"
"os"
"strings"
)
const (
// debug mode, logger of tcpx will print
DEBUG = 1 + iota
// release mode, logger of tcpx will not print
RELEASE
)
// tcpx logger
type Log struct {
Logger *log.Logger
Mode int
}
// Set mode of logger, value is tcpx.DEBUG, tcpx.RELEASE
func (l *Log) SetLogMode(mode int) {
l.Mode = mode
}
// Set logger flags, value of flags are the same as the official log
func (l *Log) SetLogFlags(flags int) {
l.Logger.SetFlags(flags)
}
// Println info in debug mode, do nothing in release mode
func (l Log) Println(info ...interface{}) {
rs:= fmt.Sprintf("%v", info)
rs = strings.TrimPrefix(rs, "[")
rs = strings.TrimSuffix(rs, "]")
if l.Mode == DEBUG {
fmt.Println(errorx.NewFromStringWithDepth(rs, 2).Error())
}
}
// Global instance of logger
var Logger = Log{
Logger: log.New(os.Stderr, "[tcpx] ", log.LstdFlags|log.Llongfile),
Mode: DEBUG,
}
// Set global instance logger mode
func SetLogMode(mode int) {
Logger.Mode = mode
}
// Set global instance logger flags
func SetLogFlags(flags int) {
Logger.Logger.SetFlags(flags)
}