-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
129 lines (108 loc) · 2.93 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
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
package log
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg log -path App,Log
import (
"context"
"v2ray.com/core/app"
"v2ray.com/core/app/log/internal"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
)
var (
streamLoggerInstance internal.LogWriter = internal.NewStdOutLogWriter()
debugLogger internal.LogWriter = streamLoggerInstance
infoLogger internal.LogWriter = streamLoggerInstance
warningLogger internal.LogWriter = streamLoggerInstance
errorLogger internal.LogWriter = streamLoggerInstance
)
func SetLogLevel(level LogLevel) {
debugLogger = new(internal.NoOpLogWriter)
if level >= LogLevel_Debug {
debugLogger = streamLoggerInstance
}
infoLogger = new(internal.NoOpLogWriter)
if level >= LogLevel_Info {
infoLogger = streamLoggerInstance
}
warningLogger = new(internal.NoOpLogWriter)
if level >= LogLevel_Warning {
warningLogger = streamLoggerInstance
}
errorLogger = new(internal.NoOpLogWriter)
if level >= LogLevel_Error {
errorLogger = streamLoggerInstance
}
}
func InitErrorLogger(file string) error {
logger, err := internal.NewFileLogWriter(file)
if err != nil {
return newError("failed to create error logger on file (", file, ")").Base(err)
}
streamLoggerInstance = logger
return nil
}
func getLoggerAndPrefix(s errors.Severity) (internal.LogWriter, string) {
switch s {
case errors.SeverityDebug:
return debugLogger, "[Debug]"
case errors.SeverityInfo:
return infoLogger, "[Info]"
case errors.SeverityWarning:
return warningLogger, "[Warning]"
case errors.SeverityError:
return errorLogger, "[Error]"
default:
return infoLogger, "[Info]"
}
}
// Trace logs an error message based on its severity.
func Trace(err error) {
logger, prefix := getLoggerAndPrefix(errors.GetSeverity(err))
logger.Log(&internal.ErrorLog{
Prefix: prefix,
Error: err,
})
}
type Instance struct {
config *Config
}
func New(ctx context.Context, config *Config) (*Instance, error) {
return &Instance{config: config}, nil
}
func (*Instance) Interface() interface{} {
return (*Instance)(nil)
}
func (g *Instance) Start() error {
config := g.config
if config.AccessLogType == LogType_File {
if err := InitAccessLogger(config.AccessLogPath); err != nil {
return err
}
}
if config.ErrorLogType == LogType_None {
SetLogLevel(LogLevel_Disabled)
} else {
if config.ErrorLogType == LogType_File {
if err := InitErrorLogger(config.ErrorLogPath); err != nil {
return err
}
}
SetLogLevel(config.ErrorLogLevel)
}
return nil
}
func (*Instance) Close() {
streamLoggerInstance.Close()
accessLoggerInstance.Close()
}
func FromSpace(space app.Space) *Instance {
v := space.GetApplication((*Instance)(nil))
if logger, ok := v.(*Instance); ok && logger != nil {
return logger
}
return nil
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
}