-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.go
More file actions
48 lines (38 loc) · 1.23 KB
/
Copy pathlogging.go
File metadata and controls
48 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"time"
)
type LogLevel string
const (
LevelInfo LogLevel = "INFO"
LevelWarn LogLevel = "WARN"
LevelError LogLevel = "ERROR"
LevelDebug LogLevel = "DEBUG"
)
type Logger interface {
Info(msg string, args ...interface{})
Warn(msg string, args ...interface{})
Error(msg string, args ...interface{})
Debug(msg string, args ...interface{})
}
// StdoutLogger is a simple logger that writes logs to standard output.
type StdoutLogger struct{}
// log is a helper function that formats the log message with a timestamp and log level.
func (l *StdoutLogger) log(level LogLevel, msg string, args ...interface{}) {
timestamp := time.Now().Format(time.RFC3339)
fmt.Printf("%s [%s] %s \n", timestamp, level, fmt.Sprintf(msg, args...))
}
// Info, Warn, Error, and Debug methods implement the Logger interface for StdoutLogger.
func (l *StdoutLogger) Info(msg string, args ...interface{}) {
l.log(LevelInfo, msg, args...)
}
func (l *StdoutLogger) Warn(msg string, args ...interface{}) {
l.log(LevelWarn, msg, args...)
}
func (l *StdoutLogger) Error(msg string, args ...interface{}) {
l.log(LevelError, msg, args...)
}
func (l *StdoutLogger) Debug(msg string, args ...interface{}) {
l.log(LevelDebug, msg, args...)
}