2021-06-19 08:30:46 -04:00
|
|
|
package log
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/log"
|
|
|
|
clog "github.com/v2fly/v2ray-core/v5/common/log"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func DefaultLogConfig() *log.Config {
|
|
|
|
return &log.Config{
|
2021-10-02 17:29:37 -04:00
|
|
|
Access: &log.LogSpecification{Type: log.LogType_None},
|
|
|
|
Error: &log.LogSpecification{Type: log.LogType_Console, Level: clog.Severity_Warning},
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 04:16:41 -05:00
|
|
|
type LogConfig struct { // nolint: revive
|
2019-02-10 13:04:11 -05:00
|
|
|
AccessLog string `json:"access"`
|
|
|
|
ErrorLog string `json:"error"`
|
|
|
|
LogLevel string `json:"loglevel"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *LogConfig) Build() *log.Config {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
config := &log.Config{
|
2021-10-02 17:29:37 -04:00
|
|
|
Access: &log.LogSpecification{Type: log.LogType_Console},
|
|
|
|
Error: &log.LogSpecification{Type: log.LogType_Console},
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2019-07-12 02:34:38 -04:00
|
|
|
if v.AccessLog == "none" {
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Access.Type = log.LogType_None
|
2019-07-12 02:34:38 -04:00
|
|
|
} else if len(v.AccessLog) > 0 {
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Access.Path = v.AccessLog
|
|
|
|
config.Access.Type = log.LogType_File
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2019-07-12 02:34:38 -04:00
|
|
|
if v.ErrorLog == "none" {
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Error.Type = log.LogType_None
|
2019-07-12 02:34:38 -04:00
|
|
|
} else if len(v.ErrorLog) > 0 {
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Error.Path = v.ErrorLog
|
|
|
|
config.Error.Type = log.LogType_File
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
level := strings.ToLower(v.LogLevel)
|
|
|
|
switch level {
|
|
|
|
case "debug":
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Error.Level = clog.Severity_Debug
|
2019-02-10 13:04:11 -05:00
|
|
|
case "info":
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Error.Level = clog.Severity_Info
|
2019-02-10 13:04:11 -05:00
|
|
|
case "error":
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Error.Level = clog.Severity_Error
|
2019-02-10 13:04:11 -05:00
|
|
|
case "none":
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Error.Type = log.LogType_None
|
|
|
|
config.Error.Type = log.LogType_None
|
2019-02-10 13:04:11 -05:00
|
|
|
default:
|
2021-10-02 17:29:37 -04:00
|
|
|
config.Error.Level = clog.Severity_Warning
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|