This repository has been archived by the owner on Mar 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
291 lines (260 loc) · 6.48 KB
/
log.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
)
var (
logFile *LogFile
dot = []byte(".")
centerDot = []byte("·")
logVerbosityLevel int32
logToStdout = false
)
// IncLogVerbosity increases log verbosity
// the idea of verbose logging is to provide a way to turn detailed logging
// on a per-request basis. This is an approximate solution: since there is
// no per-gorutine context, we use a shared variable that is increased at request
// beginning and decreased at end. We might get additional logging from other
// gorutines. It's much simpler than an alternative, like passing a logger
// to every function that needs to log
func IncLogVerbosity() {
atomic.AddInt32(&logVerbosityLevel, 1)
}
// DecLogVerbosity decreases log verbosity
func DecLogVerbosity() {
atomic.AddInt32(&logVerbosityLevel, -1)
}
// IsVerboseLogging returns true if verbose logging is turned on
func IsVerboseLogging() bool {
return atomic.LoadInt32(&logVerbosityLevel) > 0
}
// StartVerboseLog is meant to be used like this:
// if StartVerboseLog(r.URL) {
// defer DecLogVerbosity()
// }
func StartVerboseLog(u *url.URL) bool {
// "vl" stands for "verbose logging" and any value other than empty string
// truns it on
if u.Query().Get("vl") != "" {
IncLogVerbosity()
return true
}
return false
}
// StopVerboseLog is for name parity with StartVerboseLog()
func StopVerboseLog() {
DecLogVerbosity()
}
// OpenLogMust opens a log file
func OpenLogMust(fileName string) *LogFile {
path := filepath.Join(getLogDir(), fileName)
fmt.Printf("log file: %s\n", path)
logTmp, err := NewLogFile(path)
if logTmp == nil {
log.Fatalf("OpenLogMust: NewLogFile(%q) failed with %q\n", path, err)
}
return logTmp
}
func dumpStrArr(arr []string) {
for _, s := range arr {
LogInfof("%s\n", s)
}
}
// RemoveOldLogFiles removes old log files
func RemoveOldLogFiles() {
files, err := ioutil.ReadDir(getLogDir())
if err != nil {
return
}
var fileNames []string
for _, fi := range files {
name := fi.Name()
if strings.HasPrefix(name, "log-") && strings.HasSuffix(name, ".txt") {
fileNames = append(fileNames, name)
}
}
sort.Strings(fileNames)
// keep last few log files for easier debugging, delete the oldest
maxToKeep := 10
n := len(fileNames)
//LogInfof("%d log files\n", n)
if n <= maxToKeep {
//LogInfof("not deleting log files because less or equal than maxToKeep (%d)\n", maxToKeep)
return
}
//LogInfof("All log files:\n")
//dumpStrArr(fileNames)
fileNames = fileNames[:n-maxToKeep]
//LogInfof("\nwill delete log files:\n")
//dumpStrArr(fileNames)
for _, name := range fileNames {
path := filepath.Join(getLogDir(), name)
os.Remove(path)
}
}
// OpenLogFiles open error and info log files
func OpenLogFiles() {
t := time.Now()
// create a log file per day to balance size of the log file
// and number of log files.
fileName := t.Format("log-06-01-02-backend.txt")
logFile = OpenLogMust(fileName)
}
// CloseLogFiles closes log files
func CloseLogFiles() {
logFile.Close()
logFile = nil
}
func functionFromPc(pc uintptr) string {
fn := runtime.FuncForPC(pc)
if fn == nil {
return ""
}
name := []byte(fn.Name())
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
if period := bytes.Index(name, dot); period >= 0 {
name = name[period+1:]
}
name = bytes.Replace(name, centerDot, dot, -1)
return string(name)
}
// LogFatalf is like log.Fatalf() but also pre-pends name of the caller,
// so that we don't have to do that manually in every log statement
func LogFatalf(format string, arg ...interface{}) {
s := fmt.Sprintf(format, arg...)
if pc, _, _, ok := runtime.Caller(1); ok {
s = functionFromPc(pc) + ": " + s
}
fmt.Print(s)
logFile.Print(s)
CloseLogFiles()
log.Fatal(s)
}
// LogErrorf is for logging things that are unexpected but not fatal
// Automatically pre-pends name of the function calling the log function
func LogErrorf(format string, arg ...interface{}) {
s := fmt.Sprintf(format, arg...)
if pc, _, _, ok := runtime.Caller(1); ok {
s = functionFromPc(pc) + ": " + s
}
logFile.Print(s)
}
// LogError logs an error
func LogError(s string) {
if pc, _, _, ok := runtime.Caller(1); ok {
s = functionFromPc(pc) + ": " + s
}
logFile.Print(s)
}
// LogInfof is for logging of misc non-error things
func LogInfof(format string, arg ...interface{}) {
s := fmt.Sprintf(format, arg...)
if pc, _, _, ok := runtime.Caller(1); ok {
s = functionFromPc(pc) + ": " + s
}
logFile.Print(s)
}
// LogVerbosef is meant for detailed information that is only enabled on
// a per request basis
func LogVerbosef(format string, arg ...interface{}) {
if !IsVerboseLogging() {
return
}
s := fmt.Sprintf(format, arg...)
if pc, _, _, ok := runtime.Caller(1); ok {
s = functionFromPc(pc) + ": " + s
}
logFile.Print(s)
}
// LogFile describes a log file
type LogFile struct {
sync.Mutex
path string
file *os.File
w io.Writer
useLogger bool // setting this will log like log package: with date and time
logger *log.Logger
}
func (l *LogFile) dirAndBaseName() (string, string) {
dir := filepath.Dir(l.path)
base := filepath.Base(l.path)
return dir, base
}
func (l *LogFile) close() {
if l.file != nil {
l.file.Close()
l.file = nil
}
}
func (l *LogFile) open() (err error) {
flag := os.O_CREATE | os.O_APPEND | os.O_WRONLY
l.file, err = os.OpenFile(l.path, flag, 0644)
if err != nil {
return err
}
_, err = l.file.Stat()
if err != nil {
l.file.Close()
return err
}
l.w = l.file
if l.useLogger {
l.logger = log.New(l.w, "", log.LstdFlags)
}
return err
}
// NewLogFile opens a new log file (creates if doesn't exist, will append if exists)
func NewLogFile(path string) (*LogFile, error) {
res := &LogFile{
path: path,
useLogger: true,
}
if err := res.open(); err != nil {
return nil, err
}
return res, nil
}
// Close closes a log file
func (l *LogFile) Close() {
if l == nil {
return
}
l.Lock()
l.close()
l.Unlock()
}
// Print writes to log file
func (l *LogFile) Print(s string) {
if logToStdout {
fmt.Print(s)
}
if l == nil {
return
}
if l.logger != nil {
l.logger.Print(s)
} else {
l.w.Write([]byte(s))
}
}
// Printf writes to log file
func (l *LogFile) Printf(format string, arg ...interface{}) {
l.Print(fmt.Sprintf(format, arg...))
}