Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the inveracious logging.Logger to customize. #201

Merged
merged 4 commits into from
Aug 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Using export function, like Info.., delegate the actual logger
louyuting committed Aug 11, 2020
commit c75beec2219c654903df57482a33a86cc045bcab
2 changes: 1 addition & 1 deletion api/tracer.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import (
func TraceError(entry *base.SentinelEntry, err error) {
defer func() {
if e := recover(); e != nil {
logging.GetGlobalLogger().Panicf("Failed to TraceError, panic error: %+v", e)
logging.Panicf("Failed to TraceError, panic error: %+v", e)
return
}
}()
4 changes: 1 addition & 3 deletions core/base/slot_chain.go
Original file line number Diff line number Diff line change
@@ -8,8 +8,6 @@ import (
"github.com/pkg/errors"
)

var logger = logging.GetGlobalLogger()

// StatPrepareSlot is responsible for some preparation before statistic
// For example: init structure and so on
type StatPrepareSlot interface {
@@ -131,7 +129,7 @@ func (sc *SlotChain) Entry(ctx *EntryContext) *TokenResult {
// If happened, need to add TokenResult in EntryContext
defer func() {
if err := recover(); err != nil {
logger.Panicf("Sentinel internal panic in SlotChain, err: %+v", err)
logging.Panicf("Sentinel internal panic in SlotChain, err: %+v", err)
ctx.SetError(errors.Errorf("%+v", err))
return
}
25 changes: 13 additions & 12 deletions core/circuitbreaker/circuit_breaker.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import (

"github.com/alibaba/sentinel-golang/core/base"
sbase "github.com/alibaba/sentinel-golang/core/stat/base"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
)

@@ -318,21 +319,21 @@ func (s *slowRequestLeapArray) ResetBucketTo(bw *sbase.BucketWrap, startTime uin
func (s *slowRequestLeapArray) currentCounter() *slowRequestCounter {
curBucket, err := s.data.CurrentBucket(s)
if err != nil {
logger.Errorf("Failed to get current bucket, current ts=%d, err: %+v.", util.CurrentTimeMillis(), err)
logging.Errorf("Failed to get current bucket, current ts=%d, err: %+v.", util.CurrentTimeMillis(), err)
return nil
}
if curBucket == nil {
logger.Error("Current bucket is nil")
logging.Error("Current bucket is nil")
return nil
}
mb := curBucket.Value.Load()
if mb == nil {
logger.Error("Current bucket atomic Value is nil")
logging.Error("Current bucket atomic Value is nil")
return nil
}
counter, ok := mb.(*slowRequestCounter)
if !ok {
logger.Error("Bucket data type error")
logging.Error("Bucket data type error")
return nil
}
return counter
@@ -344,12 +345,12 @@ func (s *slowRequestLeapArray) allCounter() []*slowRequestCounter {
for _, b := range buckets {
mb := b.Value.Load()
if mb == nil {
logger.Error("Current bucket atomic Value is nil")
logging.Error("Current bucket atomic Value is nil")
continue
}
counter, ok := mb.(*slowRequestCounter)
if !ok {
logger.Error("Bucket data type error")
logging.Error("Bucket data type error")
continue
}
ret = append(ret, counter)
@@ -498,21 +499,21 @@ func (s *errorCounterLeapArray) ResetBucketTo(bw *sbase.BucketWrap, startTime ui
func (s *errorCounterLeapArray) currentCounter() *errorCounter {
curBucket, err := s.data.CurrentBucket(s)
if err != nil {
logger.Errorf("Failed to get current bucket, current ts=%d, err: %+v.", util.CurrentTimeMillis(), err)
logging.Errorf("Failed to get current bucket, current ts=%d, err: %+v.", util.CurrentTimeMillis(), err)
return nil
}
if curBucket == nil {
logger.Error("Current bucket is nil")
logging.Error("Current bucket is nil")
return nil
}
mb := curBucket.Value.Load()
if mb == nil {
logger.Error("Current bucket atomic Value is nil")
logging.Error("Current bucket atomic Value is nil")
return nil
}
counter, ok := mb.(*errorCounter)
if !ok {
logger.Error("Bucket data type error")
logging.Error("Bucket data type error")
return nil
}
return counter
@@ -524,12 +525,12 @@ func (s *errorCounterLeapArray) allCounter() []*errorCounter {
for _, b := range buckets {
mb := b.Value.Load()
if mb == nil {
logger.Error("Current bucket atomic Value is nil")
logging.Error("Current bucket atomic Value is nil")
continue
}
counter, ok := mb.(*errorCounter)
if !ok {
logger.Error("Bucket data type error")
logging.Error("Bucket data type error")
continue
}
ret = append(ret, counter)
3 changes: 2 additions & 1 deletion core/circuitbreaker/rule.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"fmt"

"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
"github.com/pkg/errors"
)
@@ -247,7 +248,7 @@ func NewRule(resource string, strategy Strategy, opts ...RuleOption) Rule {
Threshold: ruleOpts.errorCountThreshold,
}
default:
logger.Errorf("unsupported circuit breaker rule, strategy: %d", strategy)
logging.Errorf("unsupported circuit breaker rule, strategy: %d", strategy)
return nil
}
}
17 changes: 8 additions & 9 deletions core/circuitbreaker/rule_manager.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ import (
type CircuitBreakerGenFunc func(r Rule, reuseStat interface{}) (CircuitBreaker, error)

var (
logger = logging.GetGlobalLogger()
cbGenFuncMap = make(map[Strategy]CircuitBreakerGenFunc)

breakerRules = make(map[string][]Rule)
@@ -34,7 +33,7 @@ func init() {
}
stat, ok := reuseStat.(*slowRequestLeapArray)
if !ok || stat == nil {
logger.Warnf("Expect to generate circuit breaker with reuse statistic, but fail to do type assertion, expect:*slowRequestLeapArray, in fact: %+v", stat)
logging.Warnf("Expect to generate circuit breaker with reuse statistic, but fail to do type assertion, expect:*slowRequestLeapArray, in fact: %+v", stat)
return newSlowRtCircuitBreaker(rtRule)
}
return newSlowRtCircuitBreakerWithStat(rtRule, stat), nil
@@ -50,7 +49,7 @@ func init() {
}
stat, ok := reuseStat.(*errorCounterLeapArray)
if !ok || stat == nil {
logger.Warnf("Expect to generate circuit breaker with reuse statistic, but fail to do type assertion, expect:*errorCounterLeapArray, in fact: %+v", stat)
logging.Warnf("Expect to generate circuit breaker with reuse statistic, but fail to do type assertion, expect:*errorCounterLeapArray, in fact: %+v", stat)
return newErrorRatioCircuitBreaker(errRatioRule)
}
return newErrorRatioCircuitBreakerWithStat(errRatioRule, stat), nil
@@ -66,7 +65,7 @@ func init() {
}
stat, ok := reuseStat.(*errorCounterLeapArray)
if !ok || stat == nil {
logger.Warnf("Expect to generate circuit breaker with reuse statistic, but fail to do type assertion, expect:*errorCounterLeapArray, in fact: %+v", stat)
logging.Warnf("Expect to generate circuit breaker with reuse statistic, but fail to do type assertion, expect:*errorCounterLeapArray, in fact: %+v", stat)
return newErrorCountCircuitBreaker(errCountRule)
}
return newErrorCountCircuitBreakerWithStat(errCountRule, stat), nil
@@ -167,7 +166,7 @@ func onRuleUpdate(rules []Rule) (err error) {
continue
}
if err := rule.IsApplicable(); err != nil {
logger.Warnf("Ignoring invalid circuit breaking rule when loading new rules, rule: %+v, reason: %s", rule, err.Error())
logging.Warnf("Ignoring invalid circuit breaking rule when loading new rules, rule: %+v, reason: %s", rule, err.Error())
continue
}

@@ -193,7 +192,7 @@ func onRuleUpdate(rules []Rule) (err error) {
if r := recover(); r != nil {
return
}
logger.Debugf("Updating circuit breaker rule spends %d ns.", util.CurrentTimeNano()-start)
logging.Debugf("Updating circuit breaker rule spends %d ns.", util.CurrentTimeNano()-start)
logRuleUpdate(newBreakerRules)
}()

@@ -218,7 +217,7 @@ func onRuleUpdate(rules []Rule) (err error) {

generator := cbGenFuncMap[r.BreakerStrategy()]
if generator == nil {
logger.Warnf("Ignoring the rule due to unsupported circuit breaking strategy: %v", r)
logging.Warnf("Ignoring the rule due to unsupported circuit breaking strategy: %v", r)
continue
}

@@ -230,7 +229,7 @@ func onRuleUpdate(rules []Rule) (err error) {
cb, e = generator(r, nil)
}
if cb == nil || e != nil {
logger.Warnf("Ignoring the rule due to bad generated circuit breaker, r: %s, err: %+v", r.String(), e)
logging.Warnf("Ignoring the rule due to bad generated circuit breaker, r: %s, err: %+v", r.String(), e)
continue
}

@@ -272,7 +271,7 @@ func logRuleUpdate(rules map[string][]Rule) {
sb.WriteString(r.String() + ",")
}
sb.WriteString("]")
logger.Info(sb.String())
logging.Info(sb.String())
}

func RegisterStateChangeListeners(listeners ...StateChangeListener) {
9 changes: 2 additions & 7 deletions core/config/config.go
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ func OverrideConfigFromEnvAndInitLog() error {
if err != nil {
return err
}
logging.GetGlobalLogger().Infof("App name resolved: %s", AppName())
logging.Infof("App name resolved: %s", AppName())
return nil
}

@@ -86,7 +86,7 @@ func loadFromYamlFile(filePath string) error {
if err != nil {
return err
}
logging.GetGlobalLogger().Infof("Resolving Sentinel config from file: %s", filePath)
logging.Infof("Resolving Sentinel config from file: %s", filePath)
return checkConfValid(&(globalCfg.Sentinel))
}

@@ -140,11 +140,6 @@ func reconfigureRecordLogger(logBaseDir string, withPid bool) error {
filePath = filePath + ".pid" + strconv.Itoa(os.Getpid())
}

defaultLogger := logging.GetGlobalLogger()
if defaultLogger == nil {
return errors.New("Unexpected state: defaultLogger == nil")
}

fileLogger, err := logging.NewSimpleFileLogger(filePath, "", log.LstdFlags|log.Lshortfile)
if err != nil {
return err
2 changes: 1 addition & 1 deletion core/config/entity.go
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ type SentinelConfig struct {

// LogConfig represent the configuration of logging in Sentinel.
type LogConfig struct {
// logger indicates that using logger to replace default logger.
// logger indicates that using logger to replace default logging.
Logger logging.Logger
// Dir represents the log directory path.
Dir string
17 changes: 6 additions & 11 deletions core/flow/rule_manager.go
Original file line number Diff line number Diff line change
@@ -10,11 +10,6 @@ import (
"github.com/pkg/errors"
)

// const
var (
logger = logging.GetGlobalLogger()
)

// TrafficControllerGenFunc represents the TrafficShapingController generator function of a specific control behavior.
type TrafficControllerGenFunc func(*FlowRule) *TrafficShapingController

@@ -40,9 +35,9 @@ func init() {
func logRuleUpdate(m TrafficControllerMap) {
bs, err := json.Marshal(rulesFrom(m))
if err != nil {
logger.Info("[FlowRuleManager] Flow rules loaded")
logging.Info("[FlowRuleManager] Flow rules loaded")
} else {
logger.Infof("[FlowRuleManager] Flow rules loaded: %s", bs)
logging.Infof("[FlowRuleManager] Flow rules loaded: %s", bs)
}
}

@@ -66,7 +61,7 @@ func onRuleUpdate(rules []*FlowRule) (err error) {
if r := recover(); r != nil {
return
}
logger.Debugf("Updating flow rule spends %d ns.", util.CurrentTimeNano()-start)
logging.Debugf("Updating flow rule spends %d ns.", util.CurrentTimeNano()-start)
logRuleUpdate(m)
}()

@@ -154,20 +149,20 @@ func buildFlowMap(rules []*FlowRule) TrafficControllerMap {

for _, rule := range rules {
if err := IsValidFlowRule(rule); err != nil {
logger.Warnf("Ignoring invalid flow rule: %v, reason: %s", rule, err.Error())
logging.Warnf("Ignoring invalid flow rule: %v, reason: %s", rule, err.Error())
continue
}
if rule.LimitOrigin == "" {
rule.LimitOrigin = LimitOriginDefault
}
generator, supported := tcGenFuncMap[rule.ControlBehavior]
if !supported {
logger.Warnf("Ignoring the rule due to unsupported control behavior: %v", rule)
logging.Warnf("Ignoring the rule due to unsupported control behavior: %v", rule)
continue
}
tsc := generator(rule)
if tsc == nil {
logger.Warnf("Ignoring the rule due to bad generated traffic controller: %v", rule)
logging.Warnf("Ignoring the rule due to bad generated traffic controller: %v", rule)
continue
}

3 changes: 2 additions & 1 deletion core/flow/slot.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (

"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/stat"
"github.com/alibaba/sentinel-golang/logging"
)

// FlowSlot
@@ -22,7 +23,7 @@ func (s *FlowSlot) Check(ctx *base.EntryContext) *base.TokenResult {
// Check rules in order
for _, tc := range tcs {
if tc == nil {
logger.Warnf("nil traffic controller found, res: %s", res)
logging.Warnf("nil traffic controller found, res: %s", res)
continue
}
r := canPassCheck(tc, ctx.StatNode, ctx.Input.AcquireCount)
4 changes: 2 additions & 2 deletions core/hotspot/cache/lru.go
Original file line number Diff line number Diff line change
@@ -193,12 +193,12 @@ func (c *LRU) removeOldest() {
func (c *LRU) removeElement(e *list.Element) {
c.evictList.Remove(e)
if e.Value == nil {
logging.GetGlobalLogger().Errorf("The Value of evictList's Element is nil.")
logging.Errorf("The Value of evictList's Element is nil.")
return
}
kv, ok := e.Value.(*entry)
if !ok {
logging.GetGlobalLogger().Errorf("Fail to assert the Value of evictList's Element as *entry.")
logging.Errorf("Fail to assert the Value of evictList's Element as *entry.")
return
}
delete(c.items, kv.key)
5 changes: 3 additions & 2 deletions core/hotspot/concurrency_stat_slot.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package hotspot

import (
"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
)

@@ -21,7 +22,7 @@ func (c *ConcurrencyStatSlot) OnEntryPassed(ctx *base.EntryContext) {
metric := tc.BoundMetric()
concurrencyPtr, existed := metric.ConcurrencyCounter.Get(arg)
if !existed || concurrencyPtr == nil {
logger.Debugf("Parameter %+v does not exist in ConcurrencyCounter.", arg)
logging.Debugf("Parameter %+v does not exist in ConcurrencyCounter.", arg)
continue
}
util.IncrementAndGetInt64(concurrencyPtr)
@@ -47,7 +48,7 @@ func (c *ConcurrencyStatSlot) OnCompleted(ctx *base.EntryContext) {
metric := tc.BoundMetric()
concurrencyPtr, existed := metric.ConcurrencyCounter.Get(arg)
if !existed || concurrencyPtr == nil {
logger.Debugf("Parameter: %+v does not exist in ConcurrencyCounter.", arg)
logging.Debugf("Parameter: %+v does not exist in ConcurrencyCounter.", arg)
continue
}
util.DecrementAndGetInt64(concurrencyPtr)
Loading