This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
log.go
164 lines (140 loc) · 3.6 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
// This code is in Public Domain. Take all the code you want, I'll just write more.
package main
// TODO: add an option to log to a file in the format:
// $time E: $msg
// $time N: $msg
// E: is for errors, N: is for notices
// format of $time is TBD (human readable is long, unix timestamp is short
// but not human-readable)
// TODO: gather all errors and email them periodically (e.g. every day) to myself
import (
"fmt"
"net/http"
"time"
"github.com/kjk/u"
)
// TimestampedMsg is a messsage with a timestamp
type TimestampedMsg struct {
Time time.Time
Msg string
}
// CircularMessagesBuf is a circular buffer for messages
type CircularMessagesBuf struct {
Msgs []TimestampedMsg
pos int
full bool
}
// TimeStr formats a log timestamp
func (m *TimestampedMsg) TimeStr() string {
return m.Time.Format("2006-01-02 15:04:05")
}
// TimeSinceStr returns formatted time since log timestamp
func (m *TimestampedMsg) TimeSinceStr() string {
return u.TimeSinceNowAsString(m.Time)
}
// NewCircularMessagesBuf creates a new circular buffer
func NewCircularMessagesBuf(cap int) *CircularMessagesBuf {
return &CircularMessagesBuf{
Msgs: make([]TimestampedMsg, cap, cap),
pos: 0,
full: false,
}
}
// Add adds a message
func (b *CircularMessagesBuf) Add(s string) {
var msg = TimestampedMsg{time.Now(), s}
if b.pos == cap(b.Msgs) {
b.pos = 0
b.full = true
}
b.Msgs[b.pos] = msg
b.pos++
}
// GetOrdered returns ordered messages
func (b *CircularMessagesBuf) GetOrdered() []*TimestampedMsg {
size := b.pos
if b.full {
size = cap(b.Msgs)
}
res := make([]*TimestampedMsg, size, size)
for i := 0; i < size; i++ {
p := b.pos - 1 - i
if p < 0 {
p = cap(b.Msgs) + p
}
res[i] = &b.Msgs[p]
}
return res
}
// ServerLogger describes a logger
type ServerLogger struct {
Errors *CircularMessagesBuf
Notices *CircularMessagesBuf
UseStdout bool
}
// NewServerLogger creates a logger
func NewServerLogger(errorsMax, noticesMax int, useStdout bool) *ServerLogger {
l := &ServerLogger{
Errors: NewCircularMessagesBuf(errorsMax),
Notices: NewCircularMessagesBuf(noticesMax),
UseStdout: useStdout,
}
return l
}
// Error logs an error
func (l *ServerLogger) Error(s string) {
l.Errors.Add(s)
fmt.Printf("Error: %s\n", s)
}
// Errorf logs an error
func (l *ServerLogger) Errorf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
l.Errors.Add(s)
fmt.Printf("Error: %s\n", s)
}
// Notice logs a notice
func (l *ServerLogger) Notice(s string) {
l.Notices.Add(s)
fmt.Printf("%s\n", s)
}
// Noticef logs a notice
func (l *ServerLogger) Noticef(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
l.Notices.Add(s)
fmt.Printf("%s\n", s)
}
// GetErrors returns error messages
func (l *ServerLogger) GetErrors() []*TimestampedMsg {
return l.Errors.GetOrdered()
}
// GetNotices returns notice messages
func (l *ServerLogger) GetNotices() []*TimestampedMsg {
return l.Notices.GetOrdered()
}
// TODO: more compact date printing, e.g.:
// "2012-10-03 13:15:31"
// or even group by day, and say:
// 2012-10-03:
// 13:15:31
// url: /logs
func handleLogs(w http.ResponseWriter, r *http.Request) {
cookie := getSecureCookie(r)
isAdmin := cookie.TwitterUser == "kjk" // only I can see the logs
model := struct {
UserIsAdmin bool
Errors []*TimestampedMsg
Notices []*TimestampedMsg
Header *http.Header
}{
UserIsAdmin: isAdmin,
}
if model.UserIsAdmin {
model.Errors = logger.GetErrors()
model.Notices = logger.GetNotices()
}
if r.FormValue("show") != "" {
model.Header = &r.Header
model.Header.Add("RealIp", getIPAddress(r))
}
ExecTemplate(w, tmplLogs, model)
}