Skip to content

Commit 2a25d8e

Browse files
committed
feat: Add requestID parameter to HTTP log methods and output
1 parent 1a3a102 commit 2a25d8e

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func main() {
4444
gocolorlog.Info("Starting application")
4545
gocolorlog.Infof("Listening on port %d", 8080)
4646
gocolorlog.Warn("Cache miss for key user:123")
47-
gocolorlog.Warnf("Slow query: %s", "SELECT * FROM users")
47+
gocolorlog.Warnf("Slow query: %s", "SELECT * FROM users")
4848
gocolorlog.Error("Failed to connect to DB")
4949
gocolorlog.Errorf("Failed to open file: %s", "config.yaml")
5050

51-
gocolorlog.HTTP(200, "GET", "/api/test", 120*time.Millisecond,"ip-adress", nil)
52-
gocolorlog.HTTP(404, "POST", "/api/notfound", 80*time.Millisecond,"ip-adress", nil)
53-
gocolorlog.HTTP(500, "DELETE", "/api/error", 200*time.Millisecond,"ip-adress", fmt.Errorf("internal server error"))
51+
gocolorlog.HTTP(200, "GET", "/api/test", 120*time.Millisecond,"ip-adress", "",nil)
52+
gocolorlog.HTTP(404, "POST", "/api/notfound", 80*time.Millisecond,"ip-adress","Requsetid", nil)
53+
gocolorlog.HTTP(500, "DELETE", "/api/error", 200*time.Millisecond,"ip-adress", "Requsetid",fmt.Errorf("internal server error"))
5454

5555
gocolorlog.ContextLevel("INFO", "Bootstrap", "Application is running on: %s", "http://localhost:3000")
5656
}
@@ -65,8 +65,8 @@ func main() {
6565
[Log] 12345 - 2025-05-15 12:34:56 [ERROR] [App] Failed to connect to DB
6666
[Log] 12345 - 2025-05-15 12:34:56 [ERROR] [App] Failed to open file: config.yaml
6767
[Log] 62388 - 2025-05-16 21:29:01 HTTP [200] GET | /api/test | 200 | 120ms - 120ms | 192.168.1.1
68-
[Log] 62388 - 2025-05-16 21:29:01 HTTP [404] POST | /api/notfound | 404 | 80ms - 80ms | 192.168.1.1 |
69-
[Log] 62388 - 2025-05-16 21:29:01 HTTP [500] DELETE | /api/error | 500 | 200ms - 200ms | 192.168.1.1 | [Error]: internal server error
68+
[Log] 62388 - 2025-05-16 21:29:01 HTTP [404] POST | /api/notfound | 404 | 80ms - 80ms | RequestID: 71g261g61 |192.168.1.1 |
69+
[Log] 62388 - 2025-05-16 21:29:01 HTTP [500] DELETE | /api/error | 500 | 200ms - 200ms | 192.168.1.1 | RequestID: 71g261g61 | [Error]: internal server error
7070
[Log] 12345 - 2025-05-15 12:34:56 [INFO ] [Bootstrap] Application is running on: http://localhost:3000
7171
```
7272
*(Colors will be visible in a terminal that supports ANSI colors.)*
@@ -85,7 +85,7 @@ type Logger interface {
8585
Warnf(format string, args ...any)
8686
Error(msg string)
8787
Errorf(format string, args ...any)
88-
HTTP(status int, method, path string, latency time.Duration, ip string, err error)
88+
HTTP(status int, method, path string, latency time.Duration, ip string, requestID string, err error)
8989
ContextLevel(level, context, msg string, args ...any)
9090
}
9191
```
@@ -105,7 +105,7 @@ type Logger interface {
105105
```go
106106
gocolorlog.Info("Message")
107107
gocolorlog.Warnf("Warning: %s", "details")
108-
gocolorlog.HTTP(404, "POST", "/api/notfound", 80*time.Millisecond,"ip-adress" nil)
108+
gocolorlog.HTTP(404, "POST", "/api/notfound", 80*time.Millisecond,"ip-adress", "",nil)
109109
gocolorlog.ContextLevel("ERROR", "DB", "Connection error: %v", err)
110110
```
111111

colorlog.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Logger interface {
2525
// Errorf logs a formatted error message.
2626
Errorf(format string, args ...any)
2727
// HTTP logs an HTTP request with status, method, path, latency, and optional error.
28-
HTTP(status int, method, path string, latency time.Duration, ip string, err error)
28+
HTTP(status int, method, path string, latency time.Duration, ip string, requestID string, err error)
2929
// Context logs a message with a custom level and context.
3030
Context(level.Level, string, string, ...any)
3131
}
@@ -59,8 +59,8 @@ func Error(msg string) { defaultLogger.Error(msg) }
5959
func Errorf(format string, args ...any) { defaultLogger.Errorf(format, args...) }
6060

6161
// HTTP logs an HTTP request using the default logger.
62-
func HTTP(status int, method, path string, latency time.Duration, ip string, err error) {
63-
defaultLogger.HTTP(status, method, path, latency, ip, err)
62+
func HTTP(status int, method, path string, latency time.Duration, ip string, requestID string, err error) {
63+
defaultLogger.HTTP(status, method, path, latency, ip, requestID, err)
6464
}
6565

6666
// Context logs a message with a custom level and context using the default logger.

internal/logger/stdlogger.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ func (s *stdLogger) Errorf(format string, args ...any) {
4242
s.Context(level.Error, "App", format, args...)
4343
}
4444

45-
func (s *stdLogger) HTTP(status int, method, path string, latency time.Duration, ip string, err error) {
45+
func (s *stdLogger) HTTP(status int, method, path string, latency time.Duration, ip string, requestID string, err error) {
4646
lvl := level.HTTP
4747
context := fmt.Sprintf("%d", status)
48-
msg := fmt.Sprintf("%s | %s | %d | %dms - %s | %s ", method, path, status, latency.Milliseconds(), latency, ip)
48+
msg := fmt.Sprintf("%s | %s | %d | %dms - %s | %s", method, path, status, latency.Milliseconds(), latency, ip)
49+
if requestID != "" {
50+
msg += fmt.Sprintf(" | RequestID: %s", requestID)
51+
}
4952
if err != nil {
50-
msg += fmt.Sprintf("| %s[Error]: %v %s", color.Red, err, color.Reset)
53+
msg += fmt.Sprintf(" | %s[Error]: %v %s", color.Red, err, color.Reset)
5154
}
5255
// Renkli context için özel fonksiyon
5356
s.ContextWithColor(lvl, context, statusColor(status), "%s", msg)
@@ -79,6 +82,19 @@ func (s *stdLogger) ContextWithColor(lvl level.Level, context, contextColor, msg
7982
col, lvl, color.Reset, contextColor, context, color.Reset, formatted)
8083
}
8184

85+
func (s *stdLogger) ContextLevelWithRequestID(lvl level.Level, context, requestID, msg string, args ...any) {
86+
pid := os.Getpid()
87+
timestamp := time.Now().Format("2006-01-02 15:04:05")
88+
col := colorForLevel(lvl)
89+
formatted := msg
90+
if len(args) > 0 {
91+
formatted = fmt.Sprintf(msg, args...)
92+
}
93+
s.l.Printf("%s[Log]%s %d - %s %s%-5s%s [%s] [%s] %s",
94+
color.Cyan, color.Reset, pid, timestamp,
95+
col, lvl, color.Reset, context, requestID, formatted)
96+
}
97+
8298
func colorForLevel(lvl level.Level) string {
8399
switch lvl {
84100
case level.Info:

main/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ func main() {
1717
gocolorlog.Error("Failed to connect to DB")
1818
gocolorlog.Errorf("Failed to open file: %s", "config.yaml")
1919

20-
gocolorlog.HTTP(200, "GET", "/api/test", 120*time.Millisecond, "192.168.1.1", nil)
21-
gocolorlog.HTTP(404, "POST", "/api/notfound", 80*time.Millisecond, "192.168.1.1", errors.New("not found"))
22-
gocolorlog.HTTP(500, "DELETE", "/api/error", 200*time.Millisecond, "192.168.1.1", errors.New("internal server error"))
20+
gocolorlog.HTTP(200, "GET", "/api/test", 120*time.Millisecond, "192.168.1.1", "", nil)
21+
gocolorlog.HTTP(404, "POST", "/api/notfound", 80*time.Millisecond, "192.168.1.1", "", errors.New("not found"))
22+
gocolorlog.HTTP(500, "DELETE", "/api/error", 200*time.Millisecond, "192.168.1.1", "", errors.New("internal server error"))
2323

2424
gocolorlog.Info("Database connection: Successful")
2525
gocolorlog.ContextLevel("INFO", "Bootstrap", "Application is running on: %s", "http://localhost:3000")
2626
gocolorlog.ContextLevel("INFO", "Bootstrap", "Environment: %s", "development")
27-
gocolorlog.HTTP(201, "POST", "/ai-chat", 6*time.Millisecond, "192.168.1.1", nil)
27+
gocolorlog.HTTP(201, "POST", "/ai-chat", 6*time.Millisecond, "192.168.1.1", "", nil)
2828
gocolorlog.ContextLevel("WARN", "Bootstrap", "Cache miss for key %s", "user:123")
2929
gocolorlog.ContextLevel("ERROR", "Bootstrap", "Failed to connect to DB: %v", fmt.Errorf("timeout"))
3030
}

0 commit comments

Comments
 (0)