Skip to content

Commit ed555c3

Browse files
committed
feat: Add Debug and Fatal logging methods to the logger interface and implementation
1 parent 0d29ff3 commit ed555c3

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import (
4141
)
4242

4343
func main() {
44+
gocolorlog.Debug("Starting debug mode")
45+
gocolorlog.Debugf("Debug info: %s", "application initialization")
4446
gocolorlog.Info("Starting application")
4547
gocolorlog.Infof("Listening on port %d", 8080)
4648
gocolorlog.Warn("Cache miss for key user:123")
@@ -53,11 +55,16 @@ func main() {
5355
gocolorlog.HTTP(500, "DELETE", "/api/error", 200*time.Millisecond,"ip-adress", "Requsetid",fmt.Errorf("internal server error"))
5456

5557
gocolorlog.ContextLevel("INFO", "Bootstrap", "Application is running on: %s", "http://localhost:3000")
58+
59+
gocolorlog.Fatal("Critical error system shutdown")
60+
gocolorlog.Fatalf("Critical error: %v", fmt.Errorf("system failure"))
5661
}
5762
```
5863

5964
**Sample Output:**
6065
```
66+
[Log] 16639 - 2025-05-25 00:44:02 DEBUG [App] Starting debug mode
67+
[Log] 16639 - 2025-05-25 00:44:02 DEBUG [App] Debug info: application initialization
6168
[Log] 12345 - 2025-05-15 12:34:56 [INFO ] [App] Starting application
6269
[Log] 12345 - 2025-05-15 12:34:56 [INFO ] [App] Listening on port 8080
6370
[Log] 12345 - 2025-05-15 12:34:56 [WARN ] [App] Cache miss for key user:123
@@ -68,6 +75,8 @@ func main() {
6875
[Log] 62388 - 2025-05-16 21:29:01 HTTP [404] POST | /api/notfound | 80ms - 80ms | RequestID: 71g261g61 |192.168.1.1 |
6976
[Log] 62388 - 2025-05-16 21:29:01 HTTP [500] DELETE | /api/error | 200ms - 200ms | 192.168.1.1 | RequestID: 71g261g61 | [Error]: internal server error
7077
[Log] 12345 - 2025-05-15 12:34:56 [INFO ] [Bootstrap] Application is running on: http://localhost:3000
78+
79+
[Log] 16639 - 2025-05-25 00:44:02 FATAL [App] Critical error occurred - shutting down
7180
```
7281
*(Colors will be visible in a terminal that supports ANSI colors.)*
7382

@@ -79,6 +88,8 @@ func main() {
7988

8089
```go
8190
type Logger interface {
91+
Debug(msg string)
92+
Debugf(format string, args ...any)
8293
Info(msg string)
8394
Infof(format string, args ...any)
8495
Warn(msg string)
@@ -87,16 +98,20 @@ type Logger interface {
8798
Errorf(format string, args ...any)
8899
HTTP(status int, method, path string, latency time.Duration, ip string, requestID string, err error)
89100
ContextLevel(level, context, msg string, args ...any)
101+
Fatal(msg string)
102+
Fatalf(format string, args ...any)
90103
}
91104
```
92105

93106
#### Method Descriptions
94107

108+
- **Debug / Debugf**: Debug mod log
95109
- **Info / Infof**: Log informational messages.
96110
- **Warn / Warnf**: Log warnings.
97111
- **Error / Errorf**: Log errors.
98112
- **HTTP**: Log HTTP requests with status, method, path, latency, and optional error.
99113
- **ContextLevel**: Log with a custom level and context.
114+
- **Fatal / Fatalf**: Log critical errors and exit the application.
100115

101116
---
102117

colorlog.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
// Logger is the extensible logging interface for gocolorlog.
1313
// You can implement this interface to customize logging behavior.
1414
type Logger interface {
15+
// Debug logs a debug message.
16+
Debug(msg string)
17+
// Debugf logs a formatted debug message.
18+
Debugf(format string, args ...any)
1519
// Info logs an informational message.
1620
Info(msg string)
1721
// Infof logs a formatted informational message.
@@ -24,6 +28,10 @@ type Logger interface {
2428
Error(msg string)
2529
// Errorf logs a formatted error message.
2630
Errorf(format string, args ...any)
31+
// Fatal logs a fatal error message and exits the application.
32+
Fatal(msg string)
33+
// Fatalf logs a formatted fatal error message and exits the application.
34+
Fatalf(format string, args ...any)
2735
// HTTP logs an HTTP request with status, method, path, latency, and optional error.
2836
HTTP(status int, method, path string, latency time.Duration, ip string, requestID string, err error)
2937
// Context logs a message with a custom level and context.
@@ -40,6 +48,22 @@ func SetLogger(l Logger) {
4048
}
4149
}
4250

51+
// Debug logs a debug message using the default logger.
52+
func Debug(msg string) { defaultLogger.Debug(msg) }
53+
54+
// Debugf logs a formatted debug message using the default logger.
55+
func Debugf(format string, args ...any) { defaultLogger.Debugf(format, args...) }
56+
57+
// Fatal logs a fatal error message using the default logger and exits the application.
58+
func Fatal(msg string) {
59+
defaultLogger.Fatal(msg)
60+
}
61+
62+
// Fatalf logs a formatted fatal error message using the default logger and exits the application.
63+
func Fatalf(format string, args ...any) {
64+
defaultLogger.Fatalf(format, args...)
65+
}
66+
4367
// Info logs an informational message using the default logger.
4468
func Info(msg string) { defaultLogger.Info(msg) }
4569

internal/level/level.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ const (
1111
Warn Level = "WARN" // WARN
1212
Error Level = "ERROR" // ERROR
1313
HTTP Level = "HTTP" // HTTP
14+
Debug Level = "DEBUG" // DEBUG
15+
Fatal Level = "FATAL" // FATAL
1416
)

internal/logger/stdlogger.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ func NewStdLogger() *stdLogger {
1818
return &stdLogger{l: log.New(os.Stdout, "", 0)}
1919
}
2020

21+
func (s *stdLogger) Debug(msg string) {
22+
s.Context(level.Debug, "App", "%s", msg)
23+
}
24+
func (s *stdLogger) Debugf(format string, args ...any) {
25+
s.Context(level.Debug, "App", format, args...)
26+
}
27+
2128
func (s *stdLogger) Info(msg string) {
2229
s.Context(level.Info, "App", "%s", msg)
2330
}
@@ -37,11 +44,19 @@ func (s *stdLogger) Warnf(format string, args ...any) {
3744
func (s *stdLogger) Error(msg string) {
3845
s.Context(level.Error, "App", "%s", msg)
3946
}
40-
4147
func (s *stdLogger) Errorf(format string, args ...any) {
4248
s.Context(level.Error, "App", format, args...)
4349
}
4450

51+
func (s *stdLogger) Fatal(msg string) {
52+
s.Context(level.Fatal, "App", "%s", msg)
53+
os.Exit(1)
54+
}
55+
func (s *stdLogger) Fatalf(format string, args ...any) {
56+
s.Context(level.Fatal, "App", format, args...)
57+
os.Exit(1)
58+
}
59+
4560
func (s *stdLogger) HTTP(status int, method, path string, latency time.Duration, ip string, requestID string, err error) {
4661
lvl := level.HTTP
4762
context := fmt.Sprintf("%d", status)
@@ -105,6 +120,10 @@ func colorForLevel(lvl level.Level) string {
105120
return color.BrightRed
106121
case level.HTTP:
107122
return color.BrightCyan
123+
case level.Debug:
124+
return color.BrightBlue
125+
case level.Fatal:
126+
return color.BgRed + color.White
108127
default:
109128
return color.White
110129
}

main/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import (
99
)
1010

1111
func main() {
12-
// Varsayılan renkli logger ile kullanım:
12+
13+
gocolorlog.Debug("Starting debug mode")
14+
gocolorlog.Debugf("Debug info: %s", "application initialization")
15+
1316
gocolorlog.Info("Starting application")
1417
gocolorlog.Infof("Listening on port %d", 8080)
18+
1519
gocolorlog.Warn("Cache miss for key user:123")
1620
gocolorlog.Warnf("Slow query: %s", "SELECT * FROM users")
21+
1722
gocolorlog.Error("Failed to connect to DB")
1823
gocolorlog.Errorf("Failed to open file: %s", "config.yaml")
1924

@@ -22,9 +27,12 @@ func main() {
2227
gocolorlog.HTTP(500, "DELETE", "/api/error", 200*time.Millisecond, "192.168.1.1", "", errors.New("internal server error"))
2328

2429
gocolorlog.Info("Database connection: Successful")
30+
gocolorlog.ContextLevel("DEBUG", "Bootstrap", "Loading configuration from: %s", "/etc/app/config.yaml")
2531
gocolorlog.ContextLevel("INFO", "Bootstrap", "Application is running on: %s", "http://localhost:3000")
2632
gocolorlog.ContextLevel("INFO", "Bootstrap", "Environment: %s", "development")
2733
gocolorlog.HTTP(201, "POST", "/ai-chat", 6*time.Millisecond, "192.168.1.1", "", nil)
2834
gocolorlog.ContextLevel("WARN", "Bootstrap", "Cache miss for key %s", "user:123")
2935
gocolorlog.ContextLevel("ERROR", "Bootstrap", "Failed to connect to DB: %v", fmt.Errorf("timeout"))
36+
37+
gocolorlog.Fatal("Critical error occurred - shutting down")
3038
}

0 commit comments

Comments
 (0)