-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelog.go
178 lines (147 loc) · 4.27 KB
/
filelog.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2010, Kyle Lemons <[email protected]>. All rights reserved.
package log4go
import (
"os"
"fmt"
"time"
"sync"
)
// This log writer sends output to a file
type FileLogWriter struct {
// thread safety (TODO: goroutine+chans instead?)
lock *sync.Mutex
// The opened file
filename string
file *os.File
// The logging format
format string
// Rotate at linecount
maxlines int
maxlines_curlines int
// Rotate at size
maxsize int
maxsize_cursize int
// Rotate daily
daily bool
daily_opendate int
// Keep old logfiles (.001, .002, etc)
rotate bool
}
// This is the FileLogWriter's output method
func (flw *FileLogWriter) LogWrite(rec *LogRecord) (n int, err os.Error) {
flw.lock.Lock()
defer flw.lock.Unlock()
// First, check if we've gone over any of our rotate triggers
if flw.Good() {
if (flw.maxlines > 0 && flw.maxlines_curlines >= flw.maxlines) ||
(flw.maxsize > 0 && flw.maxsize_cursize >= flw.maxsize) ||
(flw.daily && time.LocalTime().Day != flw.daily_opendate) {
flw.intRotate()
}
}
// Make sure the writer is (still) good
if !flw.Good() {
return -1, os.NewError("File was not opened successfully")
}
// Perform the write
n,err = flw.file.Write([]byte(FormatLogRecord(flw.format, rec)))
// Update the counts
if err == nil {
flw.maxlines_curlines++
flw.maxsize_cursize += n
}
return n, err
}
func (flw *FileLogWriter) Good() bool {
return flw.file != nil
}
func (flw *FileLogWriter) Close() {
flw.file.Close()
flw.file = nil
}
func NewFileLogWriter(fname string, rotate bool) *FileLogWriter {
flw := new(FileLogWriter)
flw.lock = new(sync.Mutex)
flw.filename = fname
flw.format = "[%D %T] [%L] (%S) %M"
flw.file = nil
flw.rotate = rotate
flw.intRotate() // open the file for the first time
return flw
}
// Request that the logs rotate
func (flw *FileLogWriter) Rotate() {
flw.lock.Lock()
defer flw.lock.Unlock()
flw.intRotate()
}
// If this is called in a threaded context, it MUST be synchronized
func (flw *FileLogWriter) intRotate() {
// Close any log file that may be open
if flw.file != nil {
flw.file.Close()
}
// If we are keeping log files, move it to the next available number
if flw.rotate {
_, err := os.Lstat(flw.filename)
if err == nil { // file exists
//fmt.Fprintf(os.Stderr, "FileLogWriter.intRotate: file %s exists, searching for extension\n", flw.filename)
// Find the next available number
num := 1
fname := ""
for ; err == nil && num < 999; num++ {
fname = flw.filename + fmt.Sprintf(".%03d", num)
_, err = os.Lstat(fname)
}
if err != nil {
// Rename the file to its newfound home
//fmt.Fprintf(os.Stderr, "FileLogWriter.intRotate: Moving %s to %s\n", flw.filename, fname)
os.Rename(flw.filename, fname)
} else {
//fmt.Fprintf(os.Stderr, "FileLogWriter.intRotate: Cannot find free log number to rename %s\n", flw.filename)
}
}
}
// Open the log file
fd, err := os.OpenFile(flw.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
//fmt.Fprintf(os.Stderr, "FileLogWriter.intRotate: %s\n", err)
return
}
flw.file = fd
// Set the daily open date to the current date
flw.daily_opendate = time.LocalTime().Day
// initialize rotation values
flw.maxlines_curlines = 0
flw.maxsize_cursize = 0
}
// Set the logging format (chainable)
func (flw *FileLogWriter) SetFormat(format string) *FileLogWriter {
//fmt.Fprintf(os.Stderr, "FileLogWriter.SetFormat: %v\n", format)
flw.format = format
return flw
}
// Set rotate at linecount (chainable)
func (flw *FileLogWriter) SetRotateLines(maxlines int) *FileLogWriter {
//fmt.Fprintf(os.Stderr, "FileLogWriter.SetRotateLines: %v\n", maxlines)
flw.maxlines = maxlines
return flw
}
// Set rotate at size (chainable)
func (flw *FileLogWriter) SetRotateSize(maxsize int) *FileLogWriter {
//fmt.Fprintf(os.Stderr, "FileLogWriter.SetRotateSize: %v\n", maxsize)
flw.maxsize = maxsize
return flw
}
// Set rotate daily (chainable)
func (flw *FileLogWriter) SetRotateDaily(daily bool) *FileLogWriter {
//fmt.Fprintf(os.Stderr, "FileLogWriter.SetRotateDaily: %v\n", daily)
flw.daily = daily
return flw
}
// Set keep old (chainable)
func (flw *FileLogWriter) SetRotate(rotate bool) *FileLogWriter {
//fmt.Fprintf(os.Stderr, "FileLogWriter.SetRotate: %v\n", rotate)
flw.rotate = rotate
return flw
}