2015-09-11 11:27:36 -04:00
|
|
|
package log
|
|
|
|
|
2017-04-08 19:43:25 -04:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg log -path App,Log
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
import (
|
2017-02-01 15:35:40 -05:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/log/internal"
|
|
|
|
"v2ray.com/core/common"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2015-09-11 11:27:36 -04:00
|
|
|
)
|
|
|
|
|
2015-10-13 12:27:29 -04:00
|
|
|
var (
|
2016-05-12 02:24:41 -04:00
|
|
|
streamLoggerInstance internal.LogWriter = internal.NewStdOutLogWriter()
|
2015-10-13 12:27:29 -04:00
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
debugLogger internal.LogWriter = streamLoggerInstance
|
|
|
|
infoLogger internal.LogWriter = streamLoggerInstance
|
|
|
|
warningLogger internal.LogWriter = streamLoggerInstance
|
|
|
|
errorLogger internal.LogWriter = streamLoggerInstance
|
2015-10-13 12:27:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetLogLevel(level LogLevel) {
|
2016-05-12 02:24:41 -04:00
|
|
|
debugLogger = new(internal.NoOpLogWriter)
|
2016-10-03 16:07:28 -04:00
|
|
|
if level >= LogLevel_Debug {
|
2015-10-13 12:27:29 -04:00
|
|
|
debugLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
infoLogger = new(internal.NoOpLogWriter)
|
2016-10-03 16:07:28 -04:00
|
|
|
if level >= LogLevel_Info {
|
2015-10-13 12:27:29 -04:00
|
|
|
infoLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
warningLogger = new(internal.NoOpLogWriter)
|
2016-10-03 16:07:28 -04:00
|
|
|
if level >= LogLevel_Warning {
|
2015-10-13 12:27:29 -04:00
|
|
|
warningLogger = streamLoggerInstance
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:24:41 -04:00
|
|
|
errorLogger = new(internal.NoOpLogWriter)
|
2016-10-03 16:07:28 -04:00
|
|
|
if level >= LogLevel_Error {
|
2015-10-13 12:27:29 -04:00
|
|
|
errorLogger = streamLoggerInstance
|
|
|
|
}
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-12-05 15:10:14 -05:00
|
|
|
func InitErrorLogger(file string) error {
|
2016-05-12 02:24:41 -04:00
|
|
|
logger, err := internal.NewFileLogWriter(file)
|
2015-12-05 15:10:14 -05:00
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to create error logger on file (", file, ")").Base(err)
|
2015-12-05 15:10:14 -05:00
|
|
|
}
|
|
|
|
streamLoggerInstance = logger
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:03:10 -04:00
|
|
|
func getLoggerAndPrefix(s errors.Severity) (internal.LogWriter, string) {
|
2017-04-06 09:13:09 -04:00
|
|
|
switch s {
|
|
|
|
case errors.SeverityDebug:
|
2017-04-10 09:03:10 -04:00
|
|
|
return debugLogger, "[Debug]"
|
2017-04-06 09:13:09 -04:00
|
|
|
case errors.SeverityInfo:
|
2017-04-10 09:03:10 -04:00
|
|
|
return infoLogger, "[Info]"
|
2017-04-06 09:13:09 -04:00
|
|
|
case errors.SeverityWarning:
|
2017-04-11 15:18:23 -04:00
|
|
|
return warningLogger, "[Warning]"
|
2017-04-06 09:13:09 -04:00
|
|
|
case errors.SeverityError:
|
2017-04-10 09:03:10 -04:00
|
|
|
return errorLogger, "[Error]"
|
2017-04-06 09:13:09 -04:00
|
|
|
default:
|
2017-04-10 09:03:10 -04:00
|
|
|
return infoLogger, "[Info]"
|
2017-04-06 09:13:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:03:10 -04:00
|
|
|
// Trace logs an error message based on its severity.
|
|
|
|
func Trace(err error) {
|
2017-07-25 17:12:45 -04:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-10 09:03:10 -04:00
|
|
|
logger, prefix := getLoggerAndPrefix(errors.GetSeverity(err))
|
|
|
|
logger.Log(&internal.ErrorLog{
|
|
|
|
Prefix: prefix,
|
|
|
|
Error: err,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-01 15:35:40 -05: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 17:11:42 -04:00
|
|
|
streamLoggerInstance.Close()
|
|
|
|
accessLoggerInstance.Close()
|
|
|
|
}
|
2017-02-01 15:35:40 -05: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))
|
|
|
|
}))
|
|
|
|
}
|