-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
71 lines (63 loc) · 1.85 KB
/
logging.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
package bigbro
import (
"os"
"time"
)
// Event is something which has happened on a web page.
type Event struct {
// The element which has been triggered.
Target string `json:"target",csv:"target"`
// The name attribute of the element.
Name string `json:"name",csv:"name"`
// The id attribute of the element.
ID string `json:"id",csv:"id"`
// The method which triggered the event.
Method string `json:"method",csv:"method"`
// The web page location on the server.
Location string `json:"location",csv:"location"`
// Any additional information that can be useful.
Comment string `json:"comment",csv:"comment"`
// X position of the event.
X int `json:"x",csv:"x"`
// Y position of the event.
Y int `json:"y",csv:"y"`
// Width of the actors screen.
ScreenWidth int `json:"screenWidth",csv:"screenWidth"`
// Height of the actors screen.
ScreenHeight int `json:"screenHeight",csv:"screenHeight"`
// The time the Event happened.
Time time.Time `json:"time",csv:"time"`
// The actor that caused the Event.
Actor string `json:"actor",csv:"actor"`
}
// Logger is the way in which logs are written to a file.
type Logger struct {
formatter Formatter
}
// NewCSVLogger creates a new CSV logger.
func NewCSVLogger(name string) (Logger, error) {
lf, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return Logger{}, err
}
formatter := CSVFormatter{
w: lf,
}
return Logger{
formatter: formatter,
}, nil
}
// NewElasticsearchLogger creates a new Elasticsearch logger.
func NewElasticsearchLogger(index, version, url string) (Logger, error) {
f, err := NewElasticsearchFormatter(index, version, url)
if err != nil {
return Logger{}, err
}
return Logger{
formatter: f,
}, nil
}
// Log writes an event to the log file using the specified formatter.
func (l Logger) Log(e Event) error {
return l.formatter.Write(e)
}