Skip to content

Commit 5f4f3b4

Browse files
committed
Fix allot of things
1 parent 75500cb commit 5f4f3b4

4 files changed

Lines changed: 76 additions & 10 deletions

File tree

gecho.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,21 @@ var ParseLogLevel = utils.ParseLevel
4747
var Field = utils.Field
4848
var WithCallerSkip = utils.WithCallerSkip
4949

50-
// Logger type
50+
// Logger config functions
51+
var NewConfig = utils.NewConfig
52+
var WithLogLevel = utils.WithLogLevel
53+
var WithLogFormat = utils.WithLogFormat
54+
var WithColorize = utils.WithColorize
55+
var WithShowCaller = utils.WithShowCaller
56+
var WithTimeFormat = utils.WithTimeFormat
57+
var WithOutput = utils.WithOutput
58+
var WithErrorOutput = utils.WithErrorOutput
59+
var WithDefaultCallerSkip = utils.WithDefaultCallerSkip
60+
61+
// Logger types
5162
type Logger = utils.Logger
63+
type LoggerConfig = utils.Config
64+
type LoggerOptions = utils.LoggerOptions
5265

5366
// Log levels
5467
var (

handlers/handlers.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func (h *Handlers) HandleLogging(next http.Handler, logger *utils.Logger) http.H
5555
)
5656
} else {
5757
logger.Info(
58-
"HTTP request completed",
5958
utils.Field("method", r.Method),
6059
utils.Field("path", r.URL.Path),
6160
utils.Field("status", wrapper.statusCode),

handlers/handlers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ func TestHandleLogging(t *testing.T) {
384384
})
385385

386386
handlers := NewHandlers()
387-
loggingHandler := handlers.HandleLogging(testHandler, utils.NewDefaultLogger())
387+
logger := utils.NewLogger(utils.NewConfig(utils.WithShowCaller(false)))
388+
loggingHandler := handlers.HandleLogging(testHandler, logger)
388389

389390
w := httptest.NewRecorder()
390391
r := httptest.NewRequest(http.MethodGet, "/api/data", nil)

utils/logger.go

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,64 @@ func DefaultConfig() Config {
9393
}
9494
}
9595

96+
type LoggerOptions func(*Config)
97+
98+
func NewConfig(options ...LoggerOptions) Config {
99+
config := DefaultConfig()
100+
for _, option := range options {
101+
option(&config)
102+
}
103+
return config
104+
}
105+
106+
func WithLogLevel(level Level) LoggerOptions {
107+
return func(c *Config) {
108+
c.Level = level
109+
}
110+
}
111+
112+
func WithLogFormat(format Format) LoggerOptions {
113+
return func(c *Config) {
114+
c.Format = format
115+
}
116+
}
117+
118+
func WithColorize(colorize bool) LoggerOptions {
119+
return func(c *Config) {
120+
c.Colorize = colorize
121+
}
122+
}
123+
124+
func WithShowCaller(showCaller bool) LoggerOptions {
125+
return func(c *Config) {
126+
c.ShowCaller = showCaller
127+
}
128+
}
129+
130+
func WithTimeFormat(timeFormat string) LoggerOptions {
131+
return func(c *Config) {
132+
c.TimeFormat = timeFormat
133+
}
134+
}
135+
136+
func WithOutput(output io.Writer) LoggerOptions {
137+
return func(c *Config) {
138+
c.Output = output
139+
}
140+
}
141+
142+
func WithErrorOutput(errorOutput io.Writer) LoggerOptions {
143+
return func(c *Config) {
144+
c.ErrorOutput = errorOutput
145+
}
146+
}
147+
148+
func WithDefaultCallerSkip(callerSkip int) LoggerOptions {
149+
return func(c *Config) {
150+
c.CallerSkip = callerSkip
151+
}
152+
}
153+
96154
// Logger is a structured, thread-safe logger
97155
type Logger struct {
98156
mu sync.Mutex
@@ -123,13 +181,8 @@ func (l *Logger) WithFields(fields map[string]any) *Logger {
123181
l.mu.Lock()
124182
defer l.mu.Unlock()
125183

126-
newFields := make(map[string]any)
127-
for k, v := range l.fields {
128-
newFields[k] = v
129-
}
130-
for k, v := range fields {
131-
newFields[k] = v
132-
}
184+
newFields := maps.Clone(l.fields)
185+
maps.Copy(newFields, fields)
133186

134187
return &Logger{
135188
config: l.config,

0 commit comments

Comments
 (0)