-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogs.go
More file actions
44 lines (37 loc) · 910 Bytes
/
logs.go
File metadata and controls
44 lines (37 loc) · 910 Bytes
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
package app
import (
"fmt"
"os"
"time"
)
// Logger handles logging operations
type Logger struct {
RootDir string
initialized bool
}
// NewLogger creates a new Logger instance
func NewLogger() *Logger {
return &Logger{}
}
// SetRootDir sets the root directory for log file
func (l *Logger) SetRootDir(path string) {
l.RootDir = path
l.initialized = true
}
// Logger writes messages to a log file only if initialized
func (l *Logger) Logger(messages ...any) {
// Don't write logs if project not initialized
if !l.initialized {
return
}
logPath := "logs.log"
if l.RootDir != "" {
logPath = l.RootDir + "/logs.log"
}
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err == nil {
defer logFile.Close()
timestamp := time.Now().Format("2006-01-02 15:04:05")
logFile.WriteString(fmt.Sprintf("[%s] %v\n", timestamp, fmt.Sprint(messages...)))
}
}