Skip to content

Commit e42ff12

Browse files
author
Bai
committed
refactor(log): 重构日志轮转功能并移除追踪服务提供者
- 将变量名 safeLumberjackLogger 简化为 sLumberjackLogger - 移除代码中对 OpenTelemetry 追踪服务提供者的支持 - 新增 GetLogFormat 函数用于获取日志格式类型 - 添加示例代码演示追踪ID和跨度ID的生成方法
1 parent 2a0998a commit e42ff12

File tree

3 files changed

+76
-19
lines changed

3 files changed

+76
-19
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
// GenerateTraceID creates a new 16-byte trace ID as a hex string
10+
func GenerateTraceID() string {
11+
rand.Seed(time.Now().UnixNano())
12+
bytes := make([]byte, 16)
13+
rand.Read(bytes)
14+
15+
// Convert to hex string
16+
hexStr := fmt.Sprintf("%x", bytes)
17+
return hexStr
18+
}
19+
20+
// GenerateSpanID creates a new 8-byte span ID as a hex string
21+
func GenerateSpanID() string {
22+
rand.Seed(time.Now().UnixNano())
23+
bytes := make([]byte, 8)
24+
rand.Read(bytes)
25+
26+
// Convert to hex string
27+
hexStr := fmt.Sprintf("%x", bytes)
28+
return hexStr
29+
}
30+
31+
func main() {
32+
fmt.Println("TraceID and SpanID Examples:")
33+
fmt.Println("=============================")
34+
35+
for i := 0; i < 5; i++ {
36+
traceID := GenerateTraceID()
37+
spanID := GenerateSpanID()
38+
39+
fmt.Printf("Example %d:\n", i+1)
40+
fmt.Printf(" TraceID: %s\n", traceID)
41+
fmt.Printf(" SpanID: %s\n", spanID)
42+
fmt.Println()
43+
}
44+
45+
// Show the typical format requirements
46+
fmt.Println("Format Specifications:")
47+
fmt.Println("======================")
48+
fmt.Println("- TraceID: 32-character lowercase hex string (16 bytes)")
49+
fmt.Println("- SpanID: 16-character lowercase hex string (8 bytes)")
50+
}

rotate.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,31 @@ func NewRotatingLogger(config *RotateConfig) *RotatingLogger {
104104

105105
// NewRotatingLoggerWithFormat creates a new logger with rotation capabilities and specified format
106106
func NewRotatingLoggerWithFormat(config *RotateConfig, format FormatType) *RotatingLogger {
107-
safeLumberjackLogger := newSafeLumberjackLogger(config)
107+
sLumberjackLogger := newSafeLumberjackLogger(config)
108108

109109
// Create a new ZLogger with safe lumberjack writer and specified format
110-
zLogger := New(WithOutput(safeLumberjackLogger), WithFormat(format))
110+
zLogger := New(WithOutput(sLumberjackLogger), WithFormat(format))
111111

112112
return &RotatingLogger{
113113
baseLogger: zLogger,
114-
writer: safeLumberjackLogger,
114+
writer: sLumberjackLogger,
115115
config: config,
116116
}
117117
}
118118

119119
// WithRotation is an option function that configures the logger with rotation
120120
func WithRotation(config *RotateConfig) Option {
121-
safeLumberjackLogger := newSafeLumberjackLogger(config)
121+
sLumberjackLogger := newSafeLumberjackLogger(config)
122122

123-
return WithOutput(safeLumberjackLogger)
123+
return WithOutput(sLumberjackLogger)
124124
}
125125

126126
// WithRotationAndFormat is an option function that configures the logger with rotation and format
127127
func WithRotationAndFormat(rotationConfig *RotateConfig, format FormatType) Option {
128128
return func(c *config) {
129-
safeLumberjackLogger := newSafeLumberjackLogger(rotationConfig)
129+
sLumberjackLogger := newSafeLumberjackLogger(rotationConfig)
130130

131-
c.output = safeLumberjackLogger
131+
c.output = sLumberjackLogger
132132
c.format = format
133133
}
134134
}

zlog.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ const (
7979
JSONFormat
8080
)
8181

82+
func GetLogFormat(f string) FormatType {
83+
if f == "json" {
84+
return JSONFormat
85+
}
86+
return ConsoleFormat
87+
}
88+
8289
// ZLogger implements the FullLogger interface using zerolog
8390
type ZLogger struct {
8491
logger zerolog.Logger
@@ -95,9 +102,9 @@ func New(options ...Option) *ZLogger {
95102
cfg := &config{
96103
output: os.Stdout,
97104
level: hertzlog.LevelInfo,
98-
tp: trace.NewNoopTracerProvider(),
99105
format: ConsoleFormat, // Default to console format
100106
loggerEnrichers: []func(zerolog.Logger) zerolog.Logger{},
107+
//tp: trace.NewNoopTracerProvider(),
101108
}
102109

103110
for _, opt := range options {
@@ -148,8 +155,8 @@ func New(options ...Option) *ZLogger {
148155
return &ZLogger{
149156
logger: zlogger,
150157
level: cfg.level,
151-
tp: cfg.tp,
152158
format: cfg.format,
159+
//tp: cfg.tp,
153160
}
154161
}
155162

@@ -160,7 +167,7 @@ type Option func(*config)
160167
type config struct {
161168
output io.Writer
162169
level hertzlog.Level
163-
tp trace.TracerProvider
170+
//tp trace.TracerProvider
164171
format FormatType
165172
// Functions to customize the base logger after initial setup
166173
loggerEnrichers []func(zerolog.Logger) zerolog.Logger
@@ -181,15 +188,15 @@ func WithLevel(level hertzlog.Level) Option {
181188
}
182189

183190
// WithTraceProvider sets the OpenTelemetry trace provider for the logger
184-
func WithTraceProvider(tp trace.TracerProvider) Option {
185-
return func(c *config) {
186-
if tp != nil {
187-
c.tp = tp
188-
} else {
189-
c.tp = trace.NewNoopTracerProvider()
190-
}
191-
}
192-
}
191+
//func WithTraceProvider(tp trace.TracerProvider) Option {
192+
// return func(c *config) {
193+
// if tp != nil {
194+
// c.tp = tp
195+
// } else {
196+
// c.tp = trace.NewNoopTracerProvider()
197+
// }
198+
// }
199+
//}
193200

194201
// WithFormat sets the output format for the logger (Console or JSON)
195202
func WithFormat(format FormatType) Option {

0 commit comments

Comments
 (0)