mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 01:08:33 +00:00
Refactor log (#3446)
* Refactor log * Add new log methods * Fix logger test * Change all logging code * Clean up pathObj * Rebase to latest main * Remove invoking method name after the dot
This commit is contained in:
parent
8320732743
commit
079d0bd8a9
291 changed files with 1837 additions and 2368 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
|
@ -92,11 +93,11 @@ func loadArg(arg string) (out io.Reader, err error) {
|
|||
func FetchHTTPContent(target string) ([]byte, error) {
|
||||
parsedTarget, err := url.Parse(target)
|
||||
if err != nil {
|
||||
return nil, newError("invalid URL: ", target).Base(err)
|
||||
return nil, errors.New("invalid URL: ", target).Base(err)
|
||||
}
|
||||
|
||||
if s := strings.ToLower(parsedTarget.Scheme); s != "http" && s != "https" {
|
||||
return nil, newError("invalid scheme: ", parsedTarget.Scheme)
|
||||
return nil, errors.New("invalid scheme: ", parsedTarget.Scheme)
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
|
@ -108,17 +109,17 @@ func FetchHTTPContent(target string) ([]byte, error) {
|
|||
Close: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, newError("failed to dial to ", target).Base(err)
|
||||
return nil, errors.New("failed to dial to ", target).Base(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, newError("unexpected HTTP status code: ", resp.StatusCode)
|
||||
return nil, errors.New("unexpected HTTP status code: ", resp.StatusCode)
|
||||
}
|
||||
|
||||
content, err := buf.ReadAllToBytes(resp.Body)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read HTTP response").Base(err)
|
||||
return nil, errors.New("failed to read HTTP response").Base(err)
|
||||
}
|
||||
|
||||
return content, nil
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package all
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -1,8 +1,11 @@
|
|||
package confloader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -19,7 +22,7 @@ var (
|
|||
// actual work is in external module
|
||||
func LoadConfig(file string) (io.Reader, error) {
|
||||
if EffectiveConfigFileLoader == nil {
|
||||
newError("external config module not loaded, reading from stdin").AtInfo().WriteToLog()
|
||||
errors.LogInfo(context.Background(), "external config module not loaded, reading from stdin")
|
||||
return os.Stdin, nil
|
||||
}
|
||||
return EffectiveConfigFileLoader(file)
|
||||
|
@ -29,7 +32,7 @@ func LoadConfig(file string) (io.Reader, error) {
|
|||
// the actual work also in external module
|
||||
func LoadExtConfig(files []string, reader io.Reader) (io.Reader, error) {
|
||||
if EffectiveExtConfigLoader == nil {
|
||||
return nil, newError("external config module not loaded").AtError()
|
||||
return nil, errors.New("external config module not loaded").AtError()
|
||||
}
|
||||
|
||||
return EffectiveExtConfigLoader(files, reader)
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package confloader
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
9
main/confloader/external/errors.generated.go
vendored
9
main/confloader/external/errors.generated.go
vendored
|
@ -1,9 +0,0 @@
|
|||
package external
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
11
main/confloader/external/external.go
vendored
11
main/confloader/external/external.go
vendored
|
@ -12,6 +12,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/platform/ctlcmd"
|
||||
"github.com/xtls/xray-core/main/confloader"
|
||||
)
|
||||
|
@ -39,11 +40,11 @@ func ConfigLoader(arg string) (out io.Reader, err error) {
|
|||
func FetchHTTPContent(target string) ([]byte, error) {
|
||||
parsedTarget, err := url.Parse(target)
|
||||
if err != nil {
|
||||
return nil, newError("invalid URL: ", target).Base(err)
|
||||
return nil, errors.New("invalid URL: ", target).Base(err)
|
||||
}
|
||||
|
||||
if s := strings.ToLower(parsedTarget.Scheme); s != "http" && s != "https" {
|
||||
return nil, newError("invalid scheme: ", parsedTarget.Scheme)
|
||||
return nil, errors.New("invalid scheme: ", parsedTarget.Scheme)
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
|
@ -55,17 +56,17 @@ func FetchHTTPContent(target string) ([]byte, error) {
|
|||
Close: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, newError("failed to dial to ", target).Base(err)
|
||||
return nil, errors.New("failed to dial to ", target).Base(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, newError("unexpected HTTP status code: ", resp.StatusCode)
|
||||
return nil, errors.New("unexpected HTTP status code: ", resp.StatusCode)
|
||||
}
|
||||
|
||||
content, err := buf.ReadAllToBytes(resp.Body)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read HTTP response").Base(err)
|
||||
return nil, errors.New("failed to read HTTP response").Base(err)
|
||||
}
|
||||
|
||||
return content, nil
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package main
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package json
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/cmdarg"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
|
@ -20,14 +22,14 @@ func init() {
|
|||
case cmdarg.Arg:
|
||||
cf := &conf.Config{}
|
||||
for i, arg := range v {
|
||||
newError("Reading config: ", arg).AtInfo().WriteToLog()
|
||||
errors.LogInfo(context.Background(), "Reading config: ", arg)
|
||||
r, err := confloader.LoadConfig(arg)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config: ", arg).Base(err)
|
||||
return nil, errors.New("failed to read config: ", arg).Base(err)
|
||||
}
|
||||
c, err := serial.DecodeJSONConfig(r)
|
||||
if err != nil {
|
||||
return nil, newError("failed to decode config: ", arg).Base(err)
|
||||
return nil, errors.New("failed to decode config: ", arg).Base(err)
|
||||
}
|
||||
if i == 0 {
|
||||
// This ensure even if the muti-json parser do not support a setting,
|
||||
|
@ -41,7 +43,7 @@ func init() {
|
|||
case io.Reader:
|
||||
return serial.LoadJSONConfig(v)
|
||||
default:
|
||||
return nil, newError("unknow type")
|
||||
return nil, errors.New("unknow type")
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common/cmdarg"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
clog "github.com/xtls/xray-core/common/log"
|
||||
"github.com/xtls/xray-core/common/platform"
|
||||
"github.com/xtls/xray-core/core"
|
||||
|
@ -217,12 +218,12 @@ func startXray() (core.Server, error) {
|
|||
|
||||
c, err := core.LoadConfig(getConfigFormat(), configFiles)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load config files: [", configFiles.String(), "]").Base(err)
|
||||
return nil, errors.New("failed to load config files: [", configFiles.String(), "]").Base(err)
|
||||
}
|
||||
|
||||
server, err := core.New(c)
|
||||
if err != nil {
|
||||
return nil, newError("failed to create server").Base(err)
|
||||
return nil, errors.New("failed to create server").Base(err)
|
||||
}
|
||||
|
||||
return server, nil
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package toml
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package toml
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/cmdarg"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
|
@ -20,14 +22,14 @@ func init() {
|
|||
case cmdarg.Arg:
|
||||
cf := &conf.Config{}
|
||||
for i, arg := range v {
|
||||
newError("Reading config: ", arg).AtInfo().WriteToLog()
|
||||
errors.LogInfo(context.Background(), "Reading config: ", arg)
|
||||
r, err := confloader.LoadConfig(arg)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config: ", arg).Base(err)
|
||||
return nil, errors.New("failed to read config: ", arg).Base(err)
|
||||
}
|
||||
c, err := serial.DecodeTOMLConfig(r)
|
||||
if err != nil {
|
||||
return nil, newError("failed to decode config: ", arg).Base(err)
|
||||
return nil, errors.New("failed to decode config: ", arg).Base(err)
|
||||
}
|
||||
if i == 0 {
|
||||
// This ensure even if the muti-json parser do not support a setting,
|
||||
|
@ -41,7 +43,7 @@ func init() {
|
|||
case io.Reader:
|
||||
return serial.LoadTOMLConfig(v)
|
||||
default:
|
||||
return nil, newError("unknow type")
|
||||
return nil, errors.New("unknow type")
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package yaml
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package yaml
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/cmdarg"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
|
@ -20,14 +22,14 @@ func init() {
|
|||
case cmdarg.Arg:
|
||||
cf := &conf.Config{}
|
||||
for i, arg := range v {
|
||||
newError("Reading config: ", arg).AtInfo().WriteToLog()
|
||||
errors.LogInfo(context.Background(), "Reading config: ", arg)
|
||||
r, err := confloader.LoadConfig(arg)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config: ", arg).Base(err)
|
||||
return nil, errors.New("failed to read config: ", arg).Base(err)
|
||||
}
|
||||
c, err := serial.DecodeYAMLConfig(r)
|
||||
if err != nil {
|
||||
return nil, newError("failed to decode config: ", arg).Base(err)
|
||||
return nil, errors.New("failed to decode config: ", arg).Base(err)
|
||||
}
|
||||
if i == 0 {
|
||||
// This ensure even if the muti-json parser do not support a setting,
|
||||
|
@ -41,7 +43,7 @@ func init() {
|
|||
case io.Reader:
|
||||
return serial.LoadYAMLConfig(v)
|
||||
default:
|
||||
return nil, newError("unknow type")
|
||||
return nil, errors.New("unknow type")
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue