-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
101 lines (83 loc) · 2.06 KB
/
logger.go
File metadata and controls
101 lines (83 loc) · 2.06 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"os"
"path/filepath"
"sync"
"time"
)
var (
logFile *os.File
logMutex sync.Mutex
loggerInit bool
)
// InitLogger initializes file logging to folje_log.txt next to the executable
func InitLogger() error {
logMutex.Lock()
defer logMutex.Unlock()
if loggerInit {
return nil
}
// Get executable path
exePath, err := os.Executable()
if err != nil {
// Fall back to current directory
exePath = "."
} else {
exePath = filepath.Dir(exePath)
}
logPath := filepath.Join(exePath, "folje_log.txt")
// Open log file for append, create if doesn't exist
logFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
// Try current directory as fallback
logPath = "folje_log.txt"
logFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open log file: %w", err)
}
}
loggerInit = true
return nil
}
// CloseLogger closes the log file
func CloseLogger() {
logMutex.Lock()
defer logMutex.Unlock()
if logFile != nil {
logFile.Close()
logFile = nil
}
loggerInit = false
}
func logMessage(level string, format string, args ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
timestamp := time.Now().Format("2006-01-02 15:04:05.000")
message := fmt.Sprintf(format, args...)
logLine := fmt.Sprintf("[%s] [%s] %s\n", timestamp, level, message)
// Write to stderr
fmt.Fprint(os.Stderr, logLine)
// Write to file if available
if logFile != nil {
logFile.WriteString(logLine)
logFile.Sync()
}
}
// LogInfo logs an info message
func LogInfo(format string, args ...interface{}) {
logMessage("INFO", format, args...)
}
// LogError logs an error message
func LogError(format string, args ...interface{}) {
logMessage("ERROR", format, args...)
}
// LogDebug logs a debug message
func LogDebug(format string, args ...interface{}) {
logMessage("DEBUG", format, args...)
}
// LogFatal logs a fatal message and exits
func LogFatal(format string, args ...interface{}) {
logMessage("FATAL", format, args...)
os.Exit(1)
}