This is an automated email from the ASF dual-hosted git repository.
zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git
The following commit(s) were added to refs/heads/master by this push:
new 0188cf45 [operator] Supplemental Partial Log Library Logic v2
0188cf45 is described below
commit 0188cf456f51ac010729444b1be287ef81d0d811
Author: mfordjody <[email protected]>
AuthorDate: Sat Dec 14 09:39:33 2024 +0800
[operator] Supplemental Partial Log Library Logic v2
---
operator/pkg/util/clog/log/default.go | 7 +++++++
operator/pkg/util/clog/log/scope.go | 32 +++++++++++++++++++++++++-------
operator/pkg/util/clog/log/zap.go | 25 +++++++++++++++++++++++++
3 files changed, 57 insertions(+), 7 deletions(-)
diff --git a/operator/pkg/util/clog/log/default.go
b/operator/pkg/util/clog/log/default.go
new file mode 100644
index 00000000..63243f8f
--- /dev/null
+++ b/operator/pkg/util/clog/log/default.go
@@ -0,0 +1,7 @@
+package log
+
+var defaultScope = registerDefaultScopes()
+
+func registerDefaultScopes() (defaults *Scope) {
+ return registerScope(DefaultScopeName, "Unscoped logging messages.", 1)
+}
diff --git a/operator/pkg/util/clog/log/scope.go
b/operator/pkg/util/clog/log/scope.go
index 336468f2..efb27e7b 100644
--- a/operator/pkg/util/clog/log/scope.go
+++ b/operator/pkg/util/clog/log/scope.go
@@ -3,21 +3,25 @@ package log
import (
"fmt"
"go.uber.org/atomic"
+ "go.uber.org/zap"
"go.uber.org/zap/zapcore"
+ "runtime"
"strings"
"sync"
"time"
)
type Scope struct {
- name string
- nameToEmit string
- outputLevel *atomic.Value
- logCallers *atomic.Value
+ name string
+ nameToEmit string
+ outputLevel *atomic.Value
+ stackTraceLevel *atomic.Value
+ logCallers *atomic.Value
}
var (
- lock sync.RWMutex
+ scopes = make(map[string]*Scope)
+ lock sync.RWMutex
)
func RegisterScope(name string, desc string) *Scope {
@@ -28,13 +32,28 @@ func registerScope(name string, desc string) *Scope {
if strings.ContainsAny(name, ":,.") {
panic(fmt.Sprintf("scope name %s is invalid, it cannot contain
colons, commas, or periods", name))
}
- return nil
+ lock.Lock()
+ defer lock.Unlock()
+ s, ok := scopes[name]
+ if !ok {
+ s = &Scope{
+ name: name,
+ outputLevel: &atomic.Value{},
+ stackTraceLevel: &atomic.Value{},
+ logCallers: &atomic.Value{},
+ }
+ }
+ return s
}
func (s *Scope) GetOutputLevel() Level {
return s.outputLevel.Load().(Level)
}
+func (s *Scope) GetStackTraceLevel() Level {
+ return s.stackTraceLevel.Load().(Level)
+}
+
func (s *Scope) Infof(format string, args ...any) {
if s.GetOutputLevel() >= InfoLevel {
msg := maybeSprintf(format, args)
@@ -87,7 +106,6 @@ func (s *Scope) emitWithTime(level zapcore.Level, msg
string, t time.Time) {
}
} else if len(s.labelKeys) > 0 {
sb := &strings.Builder{}
- // Assume roughly 15 chars per kv pair. Its fine to be off,
this is just an optimization
sb.Grow(len(msg) + 15*len(s.labelKeys))
sb.WriteString(msg)
sb.WriteString("\t")
diff --git a/operator/pkg/util/clog/log/zap.go
b/operator/pkg/util/clog/log/zap.go
new file mode 100644
index 00000000..57f3836f
--- /dev/null
+++ b/operator/pkg/util/clog/log/zap.go
@@ -0,0 +1,25 @@
+package log
+
+import "go.uber.org/zap/zapcore"
+
+var toLevel = map[zapcore.Level]Level{
+ zapcore.FatalLevel: FatalLevel,
+ zapcore.ErrorLevel: ErrorLevel,
+ zapcore.WarnLevel: WarnLevel,
+ zapcore.InfoLevel: InfoLevel,
+ zapcore.DebugLevel: DebugLevel,
+}
+
+const callerSkipOffset = 3
+
+func dumpStack(level zapcore.Level, scope *Scope) bool {
+ thresh := toLevel[level]
+ if scope != defaultScope {
+ thresh = ErrorLevel
+ switch level {
+ case zapcore.FatalLevel:
+ thresh = FatalLevel
+ }
+ }
+ return scope.GetStackTraceLevel() >= thresh
+}