-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
127 lines (109 loc) · 3.16 KB
/
logger.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
package log
import (
"errors"
"fmt"
"github.com/gen-iot/std"
logger "github.com/sirupsen/logrus"
"os"
"path/filepath"
"time"
)
/**
* Created by suzhen on 2018-06-15 .
*/
const kDefaultLogTimeFormat = "2006-01-02T15:04:05.999999999-07:00"
const kLoggerTag = "[Logger]"
const kDefaultLogDir = "logs"
var DefaultConfig = Config{
Level: logger.InfoLevel,
LogToFile: false,
LogsDir: kDefaultLogDir,
}
type Config struct {
Level logger.Level `json:"level" validate:"required"`
LogToFile bool `json:"logToFile"`
LogsDir string `json:"logsDir"`
Timestamp bool `json:"timestamp"`
}
var gConf = DefaultConfig
type Logger interface {
Print(v ...interface{})
Println(v ...interface{})
Printf(format string, v ...interface{})
}
type LoggerProxy struct {
PrintlnCb func(v ...interface{})
PrintfCb func(format string, v ...interface{})
}
func (l *LoggerProxy) Print(v ...interface{}) {
l.Println(v...)
}
func (l *LoggerProxy) Println(v ...interface{}) {
l.PrintlnCb(v...)
}
func (l *LoggerProxy) Printf(format string, v ...interface{}) {
l.PrintfCb(format, v...)
}
var STD Logger = &LoggerProxy{
PrintlnCb: func(v ...interface{}) {
if gConf.Timestamp {
fmt.Println("STD ["+time.Now().Format(kDefaultLogTimeFormat)+"]", v)
} else {
fmt.Println("STD ", v)
}
},
PrintfCb: func(format string, v ...interface{}) {
fmt.Printf(format, v...)
},
}
//noinspection ALL
var DEBUG Logger = &EmptyLogger{}
//noinspection ALL
var INFO Logger = &EmptyLogger{}
//noinspection ALL
var WARN Logger = &EmptyLogger{}
var ERROR Logger = &EmptyLogger{}
//noinspection GoUnusedGlobalVariable
var FATAL Logger = &EmptyLogger{}
//noinspection ALL
var PANIC Logger = &EmptyLogger{}
func Init() {
InitWithConfig(DefaultConfig)
}
func InitWithConfig(config Config) {
if err := std.ValidateStruct(gConf); err != nil {
panic(errors.New(fmt.Sprintf("logger配置不正确 : %s", err)))
}
STD.Println(kLoggerTag, "logger(level=", config.Level, ",toFile=", config.LogToFile, ",logsDir=", config.LogsDir, ") init ... ")
logger.SetLevel(config.Level)
logger.SetFormatter(&logger.TextFormatter{
EnvironmentOverrideColors: true,
ForceColors: true,
FullTimestamp: config.Timestamp,
DisableTimestamp: !config.Timestamp,
TimestampFormat: kDefaultLogTimeFormat,
})
logger.SetOutput(os.Stdout)
if config.LogToFile {
dir := config.LogsDir
if len(dir) == 0 {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
dir = filepath.Join(wd, kDefaultLogDir)
}
logFile := filepath.Join(dir, "logs.log")
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
STD.Println(kLoggerTag, "redirect log to file error :", err)
}
logger.SetOutput(file)
}
DEBUG = &LoggerProxy{PrintlnCb: logger.Debug, PrintfCb: logger.Debugf}
INFO = &LoggerProxy{PrintlnCb: logger.Info, PrintfCb: logger.Infof}
WARN = &LoggerProxy{PrintlnCb: logger.Warn, PrintfCb: logger.Warnf}
ERROR = &LoggerProxy{PrintlnCb: logger.Error, PrintfCb: logger.Errorf}
FATAL = &LoggerProxy{PrintlnCb: logger.Fatal, PrintfCb: logger.Fatalf}
PANIC = &LoggerProxy{PrintlnCb: logger.Panic, PrintfCb: logger.Panicf}
}