mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +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
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/xtls/xray-core/app/observatory"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/features/extension"
|
||||
"github.com/xtls/xray-core/features/outbound"
|
||||
|
@ -24,8 +25,8 @@ type RoundRobinStrategy struct {
|
|||
|
||||
ctx context.Context
|
||||
observatory extension.Observatory
|
||||
mu sync.Mutex
|
||||
index int
|
||||
mu sync.Mutex
|
||||
index int
|
||||
}
|
||||
|
||||
func (s *RoundRobinStrategy) InjectContext(ctx context.Context) {
|
||||
|
@ -95,7 +96,7 @@ func (b *Balancer) PickOutbound() (string, error) {
|
|||
candidates, err := b.SelectOutbounds()
|
||||
if err != nil {
|
||||
if b.fallbackTag != "" {
|
||||
newError("fallback to [", b.fallbackTag, "], due to error: ", err).AtInfo().WriteToLog()
|
||||
errors.LogInfo(context.Background(), "fallback to [", b.fallbackTag, "], due to error: ", err)
|
||||
return b.fallbackTag, nil
|
||||
}
|
||||
return "", err
|
||||
|
@ -108,11 +109,11 @@ func (b *Balancer) PickOutbound() (string, error) {
|
|||
}
|
||||
if tag == "" {
|
||||
if b.fallbackTag != "" {
|
||||
newError("fallback to [", b.fallbackTag, "], due to empty tag returned").AtInfo().WriteToLog()
|
||||
errors.LogInfo(context.Background(), "fallback to [", b.fallbackTag, "], due to empty tag returned")
|
||||
return b.fallbackTag, nil
|
||||
}
|
||||
// will use default handler
|
||||
return "", newError("balancing strategy returns empty tag")
|
||||
return "", errors.New("balancing strategy returns empty tag")
|
||||
}
|
||||
return tag, nil
|
||||
}
|
||||
|
@ -127,7 +128,7 @@ func (b *Balancer) InjectContext(ctx context.Context) {
|
|||
func (b *Balancer) SelectOutbounds() ([]string, error) {
|
||||
hs, ok := b.ohm.(outbound.HandlerSelector)
|
||||
if !ok {
|
||||
return nil, newError("outbound.Manager is not a HandlerSelector")
|
||||
return nil, errors.New("outbound.Manager is not a HandlerSelector")
|
||||
}
|
||||
tags := hs.Select(b.selectors)
|
||||
return tags, nil
|
||||
|
@ -139,13 +140,13 @@ func (r *Router) GetPrincipleTarget(tag string) ([]string, error) {
|
|||
if s, ok := b.strategy.(BalancingPrincipleTarget); ok {
|
||||
candidates, err := b.SelectOutbounds()
|
||||
if err != nil {
|
||||
return nil, newError("unable to select outbounds").Base(err)
|
||||
return nil, errors.New("unable to select outbounds").Base(err)
|
||||
}
|
||||
return s.GetPrincipleTarget(candidates), nil
|
||||
}
|
||||
return nil, newError("unsupported GetPrincipleTarget")
|
||||
return nil, errors.New("unsupported GetPrincipleTarget")
|
||||
}
|
||||
return nil, newError("cannot find tag")
|
||||
return nil, errors.New("cannot find tag")
|
||||
}
|
||||
|
||||
// SetOverrideTarget implements routing.BalancerOverrider
|
||||
|
@ -154,7 +155,7 @@ func (r *Router) SetOverrideTarget(tag, target string) error {
|
|||
b.override.Put(target)
|
||||
return nil
|
||||
}
|
||||
return newError("cannot find tag")
|
||||
return errors.New("cannot find tag")
|
||||
}
|
||||
|
||||
// GetOverrideTarget implements routing.BalancerOverrider
|
||||
|
@ -162,5 +163,5 @@ func (r *Router) GetOverrideTarget(tag string) (string, error) {
|
|||
if b, ok := r.balancers[tag]; ok {
|
||||
return b.override.Get(), nil
|
||||
}
|
||||
return "", newError("cannot find tag")
|
||||
return "", errors.New("cannot find tag")
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package router
|
|||
|
||||
import (
|
||||
sync "sync"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
)
|
||||
|
||||
func (r *Router) OverrideBalancer(balancer string, target string) error {
|
||||
|
@ -13,7 +15,7 @@ func (r *Router) OverrideBalancer(balancer string, target string) error {
|
|||
}
|
||||
}
|
||||
if b == nil {
|
||||
return newError("balancer '", balancer, "' not found")
|
||||
return errors.New("balancer '", balancer, "' not found")
|
||||
}
|
||||
b.override.Put(target)
|
||||
return nil
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/features/routing"
|
||||
"github.com/xtls/xray-core/features/stats"
|
||||
|
@ -38,7 +39,7 @@ func (s *routingServer) GetBalancerInfo(ctx context.Context, request *GetBalance
|
|||
{
|
||||
res, err := pt.GetPrincipleTarget(request.GetTag())
|
||||
if err != nil {
|
||||
newError("unable to obtain principle target").Base(err).AtInfo().WriteToLog()
|
||||
errors.LogInfoInner(ctx, err, "unable to obtain principle target")
|
||||
} else {
|
||||
ret.Balancer.PrincipleTarget = &PrincipleTargetInfo{Tag: res}
|
||||
}
|
||||
|
@ -51,21 +52,21 @@ func (s *routingServer) OverrideBalancerTarget(ctx context.Context, request *Ove
|
|||
if bo, ok := s.router.(routing.BalancerOverrider); ok {
|
||||
return &OverrideBalancerTargetResponse{}, bo.SetOverrideTarget(request.BalancerTag, request.Target)
|
||||
}
|
||||
return nil, newError("unsupported router implementation")
|
||||
return nil, errors.New("unsupported router implementation")
|
||||
}
|
||||
|
||||
func (s *routingServer) AddRule(ctx context.Context, request *AddRuleRequest) (*AddRuleResponse, error) {
|
||||
if bo, ok := s.router.(routing.Router); ok {
|
||||
return &AddRuleResponse{}, bo.AddRule(request.Config, request.ShouldAppend)
|
||||
}
|
||||
return nil, newError("unsupported router implementation")
|
||||
return nil, errors.New("unsupported router implementation")
|
||||
|
||||
}
|
||||
func (s *routingServer) RemoveRule(ctx context.Context, request *RemoveRuleRequest) (*RemoveRuleResponse, error) {
|
||||
if bo, ok := s.router.(routing.Router); ok {
|
||||
return &RemoveRuleResponse{}, bo.RemoveRule(request.RuleTag)
|
||||
}
|
||||
return nil, newError("unsupported router implementation")
|
||||
return nil, errors.New("unsupported router implementation")
|
||||
}
|
||||
|
||||
// NewRoutingServer creates a statistics service with statistics manager.
|
||||
|
@ -78,7 +79,7 @@ func NewRoutingServer(router routing.Router, routingStats stats.Channel) Routing
|
|||
|
||||
func (s *routingServer) TestRoute(ctx context.Context, request *TestRouteRequest) (*RoutingContext, error) {
|
||||
if request.RoutingContext == nil {
|
||||
return nil, newError("Invalid routing request.")
|
||||
return nil, errors.New("Invalid routing request.")
|
||||
}
|
||||
route, err := s.router.PickRoute(AsRoutingContext(request.RoutingContext))
|
||||
if err != nil {
|
||||
|
@ -93,7 +94,7 @@ func (s *routingServer) TestRoute(ctx context.Context, request *TestRouteRequest
|
|||
|
||||
func (s *routingServer) SubscribeRoutingStats(request *SubscribeRoutingStatsRequest, stream RoutingService_SubscribeRoutingStatsServer) error {
|
||||
if s.routingStats == nil {
|
||||
return newError("Routing statistics not enabled.")
|
||||
return errors.New("Routing statistics not enabled.")
|
||||
}
|
||||
genMessage := AsProtobufMessage(request.FieldSelectors)
|
||||
subscriber, err := stats.SubscribeRunnableChannel(s.routingStats)
|
||||
|
@ -105,11 +106,11 @@ func (s *routingServer) SubscribeRoutingStats(request *SubscribeRoutingStatsRequ
|
|||
select {
|
||||
case value, ok := <-subscriber:
|
||||
if !ok {
|
||||
return newError("Upstream closed the subscriber channel.")
|
||||
return errors.New("Upstream closed the subscriber channel.")
|
||||
}
|
||||
route, ok := value.(routing.Route)
|
||||
if !ok {
|
||||
return newError("Upstream sent malformed statistics.")
|
||||
return errors.New("Upstream sent malformed statistics.")
|
||||
}
|
||||
err := stream.Send(genMessage(route))
|
||||
if err != nil {
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package command
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/strmatcher"
|
||||
"github.com/xtls/xray-core/features/routing"
|
||||
|
@ -49,12 +50,12 @@ var matcherTypeMap = map[Domain_Type]strmatcher.Type{
|
|||
func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
|
||||
matcherType, f := matcherTypeMap[domain.Type]
|
||||
if !f {
|
||||
return nil, newError("unsupported domain type", domain.Type)
|
||||
return nil, errors.New("unsupported domain type", domain.Type)
|
||||
}
|
||||
|
||||
matcher, err := matcherType.New(domain.Value)
|
||||
if err != nil {
|
||||
return nil, newError("failed to create domain matcher").Base(err)
|
||||
return nil, errors.New("failed to create domain matcher").Base(err)
|
||||
}
|
||||
|
||||
return matcher, nil
|
||||
|
@ -69,7 +70,7 @@ func NewMphMatcherGroup(domains []*Domain) (*DomainMatcher, error) {
|
|||
for _, d := range domains {
|
||||
matcherType, f := matcherTypeMap[d.Type]
|
||||
if !f {
|
||||
return nil, newError("unsupported domain type", d.Type)
|
||||
return nil, errors.New("unsupported domain type", d.Type)
|
||||
}
|
||||
_, err := g.AddPattern(d.Value, matcherType)
|
||||
if err != nil {
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/features/outbound"
|
||||
"github.com/xtls/xray-core/features/routing"
|
||||
|
@ -36,7 +38,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
|
|||
case "linear":
|
||||
matcher, err := NewDomainMatcher(rr.Domain)
|
||||
if err != nil {
|
||||
return nil, newError("failed to build domain condition").Base(err)
|
||||
return nil, errors.New("failed to build domain condition").Base(err)
|
||||
}
|
||||
conds.Add(matcher)
|
||||
case "mph", "hybrid":
|
||||
|
@ -44,9 +46,9 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
|
|||
default:
|
||||
matcher, err := NewMphMatcherGroup(rr.Domain)
|
||||
if err != nil {
|
||||
return nil, newError("failed to build domain condition with MphDomainMatcher").Base(err)
|
||||
return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
|
||||
}
|
||||
newError("MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)").AtDebug().WriteToLog()
|
||||
errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
|
||||
conds.Add(matcher)
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +118,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
|
|||
}
|
||||
|
||||
if conds.Len() == 0 {
|
||||
return nil, newError("this rule has no effective fields").AtWarning()
|
||||
return nil, errors.New("this rule has no effective fields").AtWarning()
|
||||
}
|
||||
|
||||
return conds, nil
|
||||
|
@ -146,7 +148,7 @@ func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatch
|
|||
}
|
||||
s, ok := i.(*StrategyLeastLoadConfig)
|
||||
if !ok {
|
||||
return nil, newError("not a StrategyLeastLoadConfig").AtError()
|
||||
return nil, errors.New("not a StrategyLeastLoadConfig").AtError()
|
||||
}
|
||||
leastLoadStrategy := NewLeastLoadStrategy(s)
|
||||
return &Balancer{
|
||||
|
@ -165,6 +167,6 @@ func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatch
|
|||
strategy: &RandomStrategy{FallbackTag: br.FallbackTag},
|
||||
}, nil
|
||||
default:
|
||||
return nil, newError("unrecognized balancer type")
|
||||
return nil, errors.New("unrecognized balancer type")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package router
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -7,6 +7,7 @@ import (
|
|||
sync "sync"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/serial"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/features/dns"
|
||||
|
@ -68,7 +69,7 @@ func (r *Router) Init(ctx context.Context, config *Config, d dns.Client, ohm out
|
|||
if len(btag) > 0 {
|
||||
brule, found := r.balancers[btag]
|
||||
if !found {
|
||||
return newError("balancer ", btag, " not found")
|
||||
return errors.New("balancer ", btag, " not found")
|
||||
}
|
||||
rr.Balancer = brule
|
||||
}
|
||||
|
@ -101,7 +102,7 @@ func (r *Router) AddRule(config *serial.TypedMessage, shouldAppend bool) error {
|
|||
if c, ok := inst.(*Config); ok {
|
||||
return r.ReloadRules(c, shouldAppend)
|
||||
}
|
||||
return newError("AddRule: config type error")
|
||||
return errors.New("AddRule: config type error")
|
||||
}
|
||||
|
||||
func (r *Router) ReloadRules(config *Config, shouldAppend bool) error {
|
||||
|
@ -115,7 +116,7 @@ func (r *Router) ReloadRules(config *Config, shouldAppend bool) error {
|
|||
for _, rule := range config.BalancingRule {
|
||||
_, found := r.balancers[rule.Tag]
|
||||
if found {
|
||||
return newError("duplicate balancer tag")
|
||||
return errors.New("duplicate balancer tag")
|
||||
}
|
||||
balancer, err := rule.Build(r.ohm, r.dispatcher)
|
||||
if err != nil {
|
||||
|
@ -127,7 +128,7 @@ func (r *Router) ReloadRules(config *Config, shouldAppend bool) error {
|
|||
|
||||
for _, rule := range config.Rule {
|
||||
if r.RuleExists(rule.GetRuleTag()) {
|
||||
return newError("duplicate ruleTag ", rule.GetRuleTag())
|
||||
return errors.New("duplicate ruleTag ", rule.GetRuleTag())
|
||||
}
|
||||
cond, err := rule.BuildCondition()
|
||||
if err != nil {
|
||||
|
@ -142,7 +143,7 @@ func (r *Router) ReloadRules(config *Config, shouldAppend bool) error {
|
|||
if len(btag) > 0 {
|
||||
brule, found := r.balancers[btag]
|
||||
if !found {
|
||||
return newError("balancer ", btag, " not found")
|
||||
return errors.New("balancer ", btag, " not found")
|
||||
}
|
||||
rr.Balancer = brule
|
||||
}
|
||||
|
@ -178,7 +179,7 @@ func (r *Router) RemoveRule(tag string) error {
|
|||
r.rules = newRules
|
||||
return nil
|
||||
}
|
||||
return newError("empty tag name!")
|
||||
return errors.New("empty tag name!")
|
||||
|
||||
}
|
||||
func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/xtls/xray-core/app/observatory"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/dice"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/features/extension"
|
||||
)
|
||||
|
@ -95,7 +96,7 @@ func (s *LeastLoadStrategy) pickOutbounds(candidates []string) []*node {
|
|||
// with 'balancer.fallbackTag', it means: selects qualified nodes or use the fallback.
|
||||
func (s *LeastLoadStrategy) selectLeastLoad(nodes []*node) []*node {
|
||||
if len(nodes) == 0 {
|
||||
newError("least load: no qualified outbound").AtInfo().WriteToLog()
|
||||
errors.LogInfo(s.ctx, "least load: no qualified outbound")
|
||||
return nil
|
||||
}
|
||||
expected := int(s.settings.Expected)
|
||||
|
@ -123,7 +124,7 @@ func (s *LeastLoadStrategy) selectLeastLoad(nodes []*node) []*node {
|
|||
}
|
||||
// don't continue if find expected selects
|
||||
if count >= expected {
|
||||
newError("applied baseline: ", baseline).AtDebug().WriteToLog()
|
||||
errors.LogDebug(s.ctx, "applied baseline: ", baseline)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +143,7 @@ func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration)
|
|||
}
|
||||
observeResult, err := s.observer.GetObservation(s.ctx)
|
||||
if err != nil {
|
||||
newError("cannot get observation").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(s.ctx, err, "cannot get observation")
|
||||
return make([]*node, 0)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/xtls/xray-core/app/observatory"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/features/extension"
|
||||
)
|
||||
|
@ -32,7 +33,7 @@ func (l *LeastPingStrategy) PickOutbound(strings []string) string {
|
|||
|
||||
observeReport, err := l.observatory.GetObservation(l.ctx)
|
||||
if err != nil {
|
||||
newError("cannot get observe report").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(l.ctx, err, "cannot get observe report")
|
||||
return ""
|
||||
}
|
||||
outboundsList := outboundList(strings)
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
)
|
||||
|
||||
type weightScaler func(value, weight float64) float64
|
||||
|
@ -64,7 +67,7 @@ func (s *WeightManager) findValue(tag string) float64 {
|
|||
}
|
||||
weight, err := strconv.ParseFloat(numStr, 64)
|
||||
if err != nil {
|
||||
newError("unexpected error from ParseFloat: ", err).AtError().WriteToLog()
|
||||
errors.LogError(context.Background(), "unexpected error from ParseFloat: ", err)
|
||||
return s.defaultWeight
|
||||
}
|
||||
return weight
|
||||
|
@ -82,7 +85,7 @@ func (s *WeightManager) getMatch(tag, find string, isRegexp bool) string {
|
|||
}
|
||||
r, err := regexp.Compile(find)
|
||||
if err != nil {
|
||||
newError("invalid regexp: ", find, "err: ", err).AtError().WriteToLog()
|
||||
errors.LogError(context.Background(), "invalid regexp: ", find, "err: ", err)
|
||||
return ""
|
||||
}
|
||||
return r.FindString(tag)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue