v2ray-core/app/log/log.go

136 lines
3.0 KiB
Go
Raw Normal View History

package log
import (
2017-02-01 20:35:40 +00:00
"context"
"v2ray.com/core/app"
"v2ray.com/core/app/log/internal"
"v2ray.com/core/common"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
)
var (
2016-05-12 06:24:41 +00:00
streamLoggerInstance internal.LogWriter = internal.NewStdOutLogWriter()
2016-05-12 06:24:41 +00:00
debugLogger internal.LogWriter = streamLoggerInstance
infoLogger internal.LogWriter = streamLoggerInstance
warningLogger internal.LogWriter = streamLoggerInstance
errorLogger internal.LogWriter = streamLoggerInstance
)
func SetLogLevel(level LogLevel) {
2016-05-12 06:24:41 +00:00
debugLogger = new(internal.NoOpLogWriter)
2016-10-03 20:07:28 +00:00
if level >= LogLevel_Debug {
debugLogger = streamLoggerInstance
}
2016-05-12 06:24:41 +00:00
infoLogger = new(internal.NoOpLogWriter)
2016-10-03 20:07:28 +00:00
if level >= LogLevel_Info {
infoLogger = streamLoggerInstance
}
2016-05-12 06:24:41 +00:00
warningLogger = new(internal.NoOpLogWriter)
2016-10-03 20:07:28 +00:00
if level >= LogLevel_Warning {
warningLogger = streamLoggerInstance
}
2016-05-12 06:24:41 +00:00
errorLogger = new(internal.NoOpLogWriter)
2016-10-03 20:07:28 +00:00
if level >= LogLevel_Error {
errorLogger = streamLoggerInstance
}
}
2015-12-05 20:10:14 +00:00
func InitErrorLogger(file string) error {
2016-05-12 06:24:41 +00:00
logger, err := internal.NewFileLogWriter(file)
2015-12-05 20:10:14 +00:00
if err != nil {
2016-12-04 08:10:47 +00:00
return errors.Base(err).Message("Log: Failed to create error logger on file (", file, ")")
2015-12-05 20:10:14 +00:00
}
streamLoggerInstance = logger
return nil
}
2015-10-15 11:15:59 +00:00
// Debug outputs a debug log with given format and optional arguments.
2016-11-27 16:01:44 +00:00
func Debug(val ...interface{}) {
2016-05-12 06:24:41 +00:00
debugLogger.Log(&internal.ErrorLog{
Prefix: "[Debug]",
2016-11-27 16:01:44 +00:00
Values: val,
2015-12-05 20:10:14 +00:00
})
2015-09-12 09:51:42 +00:00
}
2015-10-15 11:15:59 +00:00
// Info outputs an info log with given format and optional arguments.
2016-11-27 16:01:44 +00:00
func Info(val ...interface{}) {
2016-05-12 06:24:41 +00:00
infoLogger.Log(&internal.ErrorLog{
Prefix: "[Info]",
2016-11-27 16:01:44 +00:00
Values: val,
2015-12-05 20:10:14 +00:00
})
}
2015-10-15 11:15:59 +00:00
// Warning outputs a warning log with given format and optional arguments.
2016-11-27 16:01:44 +00:00
func Warning(val ...interface{}) {
2016-05-12 06:24:41 +00:00
warningLogger.Log(&internal.ErrorLog{
Prefix: "[Warning]",
2016-11-27 16:01:44 +00:00
Values: val,
2015-12-05 20:10:14 +00:00
})
}
2015-10-15 11:15:59 +00:00
// Error outputs an error log with given format and optional arguments.
2016-11-27 16:01:44 +00:00
func Error(val ...interface{}) {
2016-05-12 06:24:41 +00:00
errorLogger.Log(&internal.ErrorLog{
Prefix: "[Error]",
2016-11-27 16:01:44 +00:00
Values: val,
2015-12-05 20:10:14 +00:00
})
}
2016-07-10 21:11:42 +00:00
2017-02-01 20:35:40 +00:00
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() {
2016-07-10 21:11:42 +00:00
streamLoggerInstance.Close()
accessLoggerInstance.Close()
}
2017-02-01 20:35:40 +00:00
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))
}))
}