forked from azer/logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
94 lines (81 loc) · 1.98 KB
/
runtime.go
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
package logger
import (
"fmt"
"os"
)
var (
runtime *Runtime
muted = &OutputSettings{}
verbose = &OutputSettings{
Info: true,
Timer: true,
Error: true,
}
// Variable to hold the log file pointer
logFile *os.File
)
func init() {
runtime = &Runtime{
Writers: []OutputWriter{
NewStandardOutput(os.Stderr),
},
}
// Check for LOG_FILE environment variable
logFilePath := os.Getenv("LOG_FILE")
if logFilePath != "" {
// Try to open the log file
var err error
logFile, err = os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open log file %s: %v\n", logFilePath, err)
// If we can't open the file, continue without file logging
} else {
// Create a new StandardWriter for the file
fileWriter := NewStandardOutput(logFile)
fileWriter.(*StandardWriter).ColorsEnabled = false // Disable colors for file output
// Hook the file writer into the runtime
Hook(fileWriter)
}
}
}
// Add a function to close the log file when the application exits
func CloseLogFile() {
if logFile != nil {
logFile.Close()
}
}
type OutputWriter interface {
Init()
Write(log *Log)
}
type OutputSettings struct {
Info bool
Timer bool
Error bool
}
type Runtime struct {
Writers []OutputWriter
}
func (runtime *Runtime) Log(log *Log) {
if len(runtime.Writers) == 0 {
return
}
// Avoid getting into a loop if there is just one writer
if len(runtime.Writers) == 1 {
runtime.Writers[0].Write(log)
} else {
for _, w := range runtime.Writers {
w.Write(log)
}
}
}
// Add a new writer
func Hook(writer OutputWriter) {
writer.Init()
runtime.Writers = append(runtime.Writers, writer)
}
// Legacy method
func SetOutput(file *os.File) {
writer := NewStandardOutput(file)
runtime.Writers[0] = writer
}