diff --git a/common/log/config.go b/common/log/config.go new file mode 100644 index 000000000..7f25654bf --- /dev/null +++ b/common/log/config.go @@ -0,0 +1,25 @@ +package log + +func (this *Config) Apply() error { + if this == nil { + return nil + } + if this.AccessLogType == LogType_File { + if err := InitAccessLogger(this.AccessLogPath); err != nil { + return err + } + } + + if this.ErrorLogType == LogType_None { + SetLogLevel(LogLevel_Disabled) + } else { + if this.ErrorLogType == LogType_File { + if err := InitErrorLogger(this.ErrorLogPath); err != nil { + return err + } + } + SetLogLevel(this.ErrorLogLevel) + } + + return nil +} diff --git a/common/log/config.pb.go b/common/log/config.pb.go new file mode 100644 index 000000000..2d37b7272 --- /dev/null +++ b/common/log/config.pb.go @@ -0,0 +1,132 @@ +// Code generated by protoc-gen-go. +// source: v2ray.com/core/common/log/config.proto +// DO NOT EDIT! + +/* +Package log is a generated protocol buffer package. + +It is generated from these files: + v2ray.com/core/common/log/config.proto + +It has these top-level messages: + Config +*/ +package log + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type LogType int32 + +const ( + LogType_None LogType = 0 + LogType_Console LogType = 1 + LogType_File LogType = 2 + LogType_Event LogType = 3 +) + +var LogType_name = map[int32]string{ + 0: "None", + 1: "Console", + 2: "File", + 3: "Event", +} +var LogType_value = map[string]int32{ + "None": 0, + "Console": 1, + "File": 2, + "Event": 3, +} + +func (x LogType) String() string { + return proto.EnumName(LogType_name, int32(x)) +} +func (LogType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +type LogLevel int32 + +const ( + LogLevel_Disabled LogLevel = 0 + LogLevel_Error LogLevel = 1 + LogLevel_Warning LogLevel = 2 + LogLevel_Info LogLevel = 3 + LogLevel_Debug LogLevel = 4 +) + +var LogLevel_name = map[int32]string{ + 0: "Disabled", + 1: "Error", + 2: "Warning", + 3: "Info", + 4: "Debug", +} +var LogLevel_value = map[string]int32{ + "Disabled": 0, + "Error": 1, + "Warning": 2, + "Info": 3, + "Debug": 4, +} + +func (x LogLevel) String() string { + return proto.EnumName(LogLevel_name, int32(x)) +} +func (LogLevel) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } + +type Config struct { + ErrorLogType LogType `protobuf:"varint,1,opt,name=error_log_type,json=errorLogType,enum=v2ray.core.common.log.LogType" json:"error_log_type,omitempty"` + ErrorLogLevel LogLevel `protobuf:"varint,2,opt,name=error_log_level,json=errorLogLevel,enum=v2ray.core.common.log.LogLevel" json:"error_log_level,omitempty"` + ErrorLogPath string `protobuf:"bytes,3,opt,name=error_log_path,json=errorLogPath" json:"error_log_path,omitempty"` + AccessLogType LogType `protobuf:"varint,4,opt,name=access_log_type,json=accessLogType,enum=v2ray.core.common.log.LogType" json:"access_log_type,omitempty"` + AccessLogPath string `protobuf:"bytes,5,opt,name=access_log_path,json=accessLogPath" json:"access_log_path,omitempty"` +} + +func (m *Config) Reset() { *m = Config{} } +func (m *Config) String() string { return proto.CompactTextString(m) } +func (*Config) ProtoMessage() {} +func (*Config) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +func init() { + proto.RegisterType((*Config)(nil), "v2ray.core.common.log.Config") + proto.RegisterEnum("v2ray.core.common.log.LogType", LogType_name, LogType_value) + proto.RegisterEnum("v2ray.core.common.log.LogLevel", LogLevel_name, LogLevel_value) +} + +func init() { proto.RegisterFile("v2ray.com/core/common/log/config.proto", fileDescriptor0) } + +var fileDescriptor0 = []byte{ + // 322 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x8c, 0x91, 0x6d, 0x4b, 0xf3, 0x30, + 0x14, 0x86, 0xd7, 0x76, 0xa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0x3d, 0xf8, 0xe1, 0xb1, 0x71, 0x15, 0x1b, 0x57, + 0x70, 0x26, 0x6f, 0x74, 0x47, 0x6a, 0xf9, 0x48, 0x9d, 0x94, 0x1f, 0x51, 0x01, 0x2d, 0xb1, 0x26, + 0xa9, 0x4d, 0x51, 0xa5, 0x28, 0x81, 0x3a, 0xe6, 0x7b, 0x11, 0x49, 0xa0, 0x2e, 0x6b, 0x90, 0xca, + 0xfc, 0x00, 0x2a, 0xa3, 0xde, 0x68, 0x4e, 0xea, 0x5f, 0x8b, 0xb5, 0x48, 0x7d, 0x16, 0xa5, 0xe1, + 0x56, 0xc2, 0x2b, 0x2d, 0x59, 0xc8, 0x2c, 0x44, 0x1d, 0xa3, 0x79, 0x0e, 0xb5, 0x8a, 0x94, 0xa0, + 0xae, 0xd1, 0x3c, 0xa8, 0x1d, 0x52, 0xcf, 0x10, 0x33, 0xd8, 0xbe, 0x09, 0x5a, 0x9e, 0xdc, 0x90, + 0xff, 0x1c, 0xe3, 0xf3, 0x9b, 0x4d, 0x9a, 0xa7, 0x3b, 0xae, 0xcc, 0xb9, 0x5f, 0x3c, 0x89, 0x62, + 0x5b, 0xb5, 0xa7, 0xbf, 0xfd, 0x08, 0x00, 0x00, 0xff, 0xff, 0x11, 0x55, 0x7e, 0x04, 0x24, 0x02, + 0x00, 0x00, +} diff --git a/common/log/config.proto b/common/log/config.proto new file mode 100644 index 000000000..f8fa91ed0 --- /dev/null +++ b/common/log/config.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +package v2ray.core.common.log; +option go_package = "log"; +option java_package = "com.v2ray.core.common.log"; +option java_outer_classname = "ConfigProto"; + +enum LogType { + None = 0; + Console = 1; + File = 2; + Event = 3; +} + +enum LogLevel { + Disabled = 0; + Error = 1; + Warning = 2; + Info = 3; + Debug = 4; +} + +message Config { + LogType error_log_type = 1; + LogLevel error_log_level = 2; + string error_log_path = 3; + + LogType access_log_type = 4; + string access_log_path = 5; +} \ No newline at end of file diff --git a/common/log/config_json.go b/common/log/config_json.go new file mode 100644 index 000000000..8c1e24a44 --- /dev/null +++ b/common/log/config_json.go @@ -0,0 +1,42 @@ +package log + +import ( + "encoding/json" + "errors" + "strings" +) + +func (this *Config) UnmarshalJSON(data []byte) error { + type JsonLogConfig struct { + AccessLog string `json:"access"` + ErrorLog string `json:"error"` + LogLevel string `json:"loglevel"` + } + jsonConfig := new(JsonLogConfig) + if err := json.Unmarshal(data, jsonConfig); err != nil { + return errors.New("Log: Failed to parse log config: " + err.Error()) + } + if len(jsonConfig.AccessLog) > 0 { + this.AccessLogPath = jsonConfig.AccessLog + this.AccessLogType = LogType_File + } + if len(jsonConfig.ErrorLog) > 0 { + this.ErrorLogPath = jsonConfig.ErrorLog + this.ErrorLogType = LogType_File + } + + level := strings.ToLower(jsonConfig.LogLevel) + switch level { + case "debug": + this.ErrorLogLevel = LogLevel_Debug + case "info": + this.ErrorLogLevel = LogLevel_Info + case "error": + this.ErrorLogLevel = LogLevel_Error + case "none": + this.ErrorLogType = LogType_None + default: + this.ErrorLogLevel = LogLevel_Warning + } + return nil +} diff --git a/common/log/log.go b/common/log/log.go index 5f3e2f647..45c7d54fe 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -4,16 +4,6 @@ import ( "v2ray.com/core/common/log/internal" ) -type LogLevel int - -const ( - DebugLevel = LogLevel(0) - InfoLevel = LogLevel(1) - WarningLevel = LogLevel(2) - ErrorLevel = LogLevel(3) - NoneLevel = LogLevel(999) -) - var ( streamLoggerInstance internal.LogWriter = internal.NewStdOutLogWriter() @@ -25,28 +15,24 @@ var ( func SetLogLevel(level LogLevel) { debugLogger = new(internal.NoOpLogWriter) - if level <= DebugLevel { + if level >= LogLevel_Debug { debugLogger = streamLoggerInstance } infoLogger = new(internal.NoOpLogWriter) - if level <= InfoLevel { + if level >= LogLevel_Info { infoLogger = streamLoggerInstance } warningLogger = new(internal.NoOpLogWriter) - if level <= WarningLevel { + if level >= LogLevel_Warning { warningLogger = streamLoggerInstance } errorLogger = new(internal.NoOpLogWriter) - if level <= ErrorLevel { + if level >= LogLevel_Error { errorLogger = streamLoggerInstance } - - if level == NoneLevel { - accessLoggerInstance = new(internal.NoOpLogWriter) - } } func InitErrorLogger(file string) error { diff --git a/shell/point/config.go b/shell/point/config.go index d4b72e87f..8adb17daa 100644 --- a/shell/point/config.go +++ b/shell/point/config.go @@ -28,12 +28,6 @@ type OutboundConnectionConfig struct { Settings []byte } -type LogConfig struct { - AccessLog string - ErrorLog string - LogLevel log.LogLevel -} - const ( AllocationStrategyAlways = "always" AllocationStrategyRandom = "random" @@ -67,7 +61,7 @@ type OutboundDetourConfig struct { type Config struct { Port v2net.Port - LogConfig *LogConfig + LogConfig *log.Config RouterConfig *router.Config DNSConfig *dns.Config InboundConfig *InboundConnectionConfig diff --git a/shell/point/config_json.go b/shell/point/config_json.go index afc66b848..f0f0a88ff 100644 --- a/shell/point/config_json.go +++ b/shell/point/config_json.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "io" - "strings" "v2ray.com/core/app/dns" "v2ray.com/core/app/router" @@ -24,7 +23,7 @@ const ( func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { Port v2net.Port `json:"port"` // Port of this Point server. - LogConfig *LogConfig `json:"log"` + LogConfig *log.Config `json:"log"` RouterConfig *router.Config `json:"routing"` DNSConfig *dns.Config `json:"dns"` InboundConfig *InboundConnectionConfig `json:"inbound"` @@ -129,35 +128,6 @@ func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error { return nil } -func (this *LogConfig) UnmarshalJSON(data []byte) error { - type JsonLogConfig struct { - AccessLog string `json:"access"` - ErrorLog string `json:"error"` - LogLevel string `json:"loglevel"` - } - jsonConfig := new(JsonLogConfig) - if err := json.Unmarshal(data, jsonConfig); err != nil { - return errors.New("Point: Failed to parse log config: " + err.Error()) - } - this.AccessLog = jsonConfig.AccessLog - this.ErrorLog = jsonConfig.ErrorLog - - level := strings.ToLower(jsonConfig.LogLevel) - switch level { - case "debug": - this.LogLevel = log.DebugLevel - case "info": - this.LogLevel = log.InfoLevel - case "error": - this.LogLevel = log.ErrorLevel - case "none": - this.LogLevel = log.NoneLevel - default: - this.LogLevel = log.WarningLevel - } - return nil -} - func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error { type JsonInboundDetourAllocationConfig struct { Strategy string `json:"strategy"` diff --git a/shell/point/point.go b/shell/point/point.go index 715f03645..196f538de 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -48,22 +48,9 @@ func NewPoint(pConfig *Config) (*Point, error) { } if pConfig.LogConfig != nil { - logConfig := pConfig.LogConfig - if len(logConfig.AccessLog) > 0 { - err := log.InitAccessLogger(logConfig.AccessLog) - if err != nil { - return nil, err - } + if err := pConfig.LogConfig.Apply(); err != nil { + return nil, err } - - if len(logConfig.ErrorLog) > 0 { - err := log.InitErrorLogger(logConfig.ErrorLog) - if err != nil { - return nil, err - } - } - - log.SetLogLevel(logConfig.LogLevel) } vpoint.space = app.NewSpace()