forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
82 lines (69 loc) · 1.4 KB
/
logger.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
package server
import (
"bytes"
"log"
"os"
"sync"
"text/template"
"time"
)
type Logger struct {
File *os.File
Format string
sync.Mutex
template *template.Template
skip bool
}
var L *Logger
type logItem struct {
RequestIP string
Time time.Time
X int
Y int
Z int
}
const DefaultLogFormat = "{{.Time}}:{{.RequestIP}} —— Tile:{{.Z}}/{{.X}}/{{.Y}}"
func (l *Logger) initTemplate() {
if l == nil || l.template != nil || l.skip {
return
}
if l.Format == "" {
l.Format = DefaultLogFormat
}
l.Lock()
// setup our server log template
l.template = template.New("logfile")
if _, err := l.template.Parse(l.Format); err != nil {
log.Printf("Could not parse log template(%v) disabling logging. Error: %v", l.Format, err)
l.skip = true
}
l.Unlock()
}
func (l *Logger) Log(item logItem) {
l.initTemplate()
if l == nil || l.File == nil || l.skip {
return
}
if item.Time.IsZero() {
item.Time = time.Now()
}
var lstr string
lbuf := bytes.NewBufferString(lstr)
if err := l.template.Execute(lbuf, item); err != nil {
// Don't care about the error.
log.Println("Error writing to log file; disabling logging.", err)
l.Lock()
l.skip = true
l.Unlock()
return
}
b := lbuf.Bytes()
// If there is no new line, let's add it.
if b[len(b)-1] != '\n' {
b = append(b, '\n')
}
// Don't care about the error.
l.Lock()
l.File.Write(b)
l.Unlock()
}