-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrotate.go
120 lines (94 loc) · 2.88 KB
/
logrotate.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"io"
"log"
"os"
)
func initLogging() {
// Set default logging pattern.
log.SetFlags(log.LstdFlags)
// Add C for child and P for parent.
if isChild {
log.SetPrefix("C ")
// Set log output of child to stdout.
log.SetOutput(os.Stdout)
return
}
log.SetPrefix("P ")
// Return if no log file should be written. Logging will still be done to stdout.
if config.LogFile == "" {
return
}
// Open the log file for appending.
f, err := os.OpenFile(config.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
// Do not close the file, because the logger should always be able to write into it!
// defer f.Close()
// Create a writer that writes to the log file and to stdout.
w := io.MultiWriter(f, os.Stdout)
// Modify the output of the default logger.
log.SetOutput(w)
/*
// Log rotation only works, when the server is not in a jail.
// TODO: move logging into parent
if !config.JailProcess {
// Rotate the log files every day.
go func() {
for range time.Tick(24 * time.Hour) {
fileInfo, err := f.Stat()
if err == nil && fileInfo.Size() < 5*1024*1024 {
// Only rotate log files if they are too big.
continue
}
// Remove the oldest log file.
os.Remove(config.LogFile + ".3")
// Closing the current log file is not necessary,
// because os.Rename() closes the file automatically.
// f.Close()
// Rename the log files.
os.Rename(config.LogFile+".2", config.LogFile+".3")
os.Rename(config.LogFile+".1", config.LogFile+".2")
os.Rename(config.LogFile, config.LogFile+".1")
// Create a new log file.
f, err := os.Create(config.LogFile)
if err != nil {
log.Fatal(err)
}
// Create a writer that writes to the log file and to stdout.
w = io.MultiWriter(f, os.Stdout)
// Modify the output of the default logger.
log.SetOutput(w)
}
}()
}
////// OR something like this:
// Rotate the log files every day.
go func() {
for range time.Tick(24 * time.Hour) {
// Check if the log file size exceeds a threshold before rotating
fileInfo, err := f.Stat()
if err != nil || fileInfo.Size() >= 5*1024*1024 {
rotateLogs()
}
}
}()
func rotateLogs() {
// Remove the oldest log file.
os.Remove(config.LogFile + ".3")
os.Rename(config.LogFile+".2", config.LogFile+".3")
os.Rename(config.LogFile+".1", config.LogFile+".2")
os.Rename(config.LogFile, config.LogFile+".1")
// Create a new log file.
f, err := os.Create(config.LogFile)
if err != nil {
log.Fatal(err)
}
// Create a writer that writes to the log file and to stdout.
w := io.MultiWriter(f, os.Stdout)
// Modify the output of the default logger.
log.SetOutput(w)
}
*/
}