summaryrefslogtreecommitdiff
path: root/internal/utils/log/logger.go
diff options
context:
space:
mode:
authorKyren223 <ulmliad223@gmail.com>2024-10-05 01:16:29 +0300
committerKyren223 <ulmliad223@gmail.com>2024-10-05 01:16:29 +0300
commit0929041a9ccce566411e9a36713cb4125d72a1ed (patch)
tree946752c70ade73f8600b99c5ba8767268bfb5703 /internal/utils/log/logger.go
parentddb2878cc2f7c8bcc89108726ecb2dba7c861539 (diff)
feat: implement logging system
Diffstat (limited to 'internal/utils/log/logger.go')
-rw-r--r--internal/utils/log/logger.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/utils/log/logger.go b/internal/utils/log/logger.go
new file mode 100644
index 0000000..735cb9e
--- /dev/null
+++ b/internal/utils/log/logger.go
@@ -0,0 +1,55 @@
+package log
+
+import (
+ "fmt"
+ "go/format"
+ "io"
+ "os"
+ "sync"
+ "time"
+)
+
+type Logger struct {
+ name string
+ Level
+ writer io.Writer
+ mu sync.Mutex
+ color bool
+}
+
+func NewLogger(name string, writer io.Writer, ansi bool) *Logger {
+ color := ansi && (writer == os.Stdout || writer == os.Stderr)
+ return &Logger{name, LevelInfo, writer, sync.Mutex{}, color}
+}
+
+func (l *Logger) Log(level Level, message string, a ...any) error {
+ if l.Level > level {
+ return nil
+ }
+
+ timestamp := time.Now().Format(time.TimeOnly)
+ severity := level.String()
+
+ // TODO: add support for ANSI coloring
+ l.mu.Lock()
+ _, err := fmt.Fprintf(l.writer, "[%s] [%s/%s]: %s\n", timestamp, l.name, severity, message, a)
+ l.mu.Unlock()
+ return err
+}
+
+func (l *Logger) Debug(message string, a ...any) error {
+ return l.Log(LevelDebug, message, a)
+}
+
+func (l *Logger) Info(message string, a ...any) error {
+ return l.Log(LevelInfo, message, a)
+}
+
+func (l *Logger) Warn(message string, a ...any) error {
+ return l.Log(LevelWarn, message, a)
+}
+
+func (l *Logger) Error(message string, a ...any) error {
+ return l.Log(LevelError, message, a)
+}
+