-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
77 lines (65 loc) · 1.49 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
72
73
74
75
76
77
package main
import (
"encoding/json"
"io"
"log"
"os"
"time"
"github.com/rtlong/web-spider/spider"
)
type Logger interface {
PrintResult(*spider.Result)
Fatal(string)
SetOutput(io.Writer)
}
type PlaintextLogger struct {
Log *log.Logger
}
func (l *PlaintextLogger) PrintResult(r *spider.Result) {
if r.Error.Error != nil {
l.Log.Printf("| ERR %s: %s\n", r.Job, r.Error.String())
} else {
ms := int64(r.Duration / time.Millisecond)
l.Log.Printf("| %3d %s [%dms]\n", r.Response.StatusCode, r.Job.String(), ms)
}
}
func (l *PlaintextLogger) Fatal(message string) {
l.Log.Fatalf("%s\n", message)
}
func (l *PlaintextLogger) SetOutput(w io.Writer) {
l.Log = log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds)
}
type JSONEvent struct {
Time time.Time
Method string
URL string
ResponseStatus int
Duration time.Duration
Result *spider.Result
}
type JSONLogger struct {
Encoder *json.Encoder
}
func (l JSONLogger) PrintResult(r *spider.Result) {
err := l.Encoder.Encode(JSONEvent{
Method: r.Job.Method,
URL: r.Job.URL.String(),
ResponseStatus: r.Response.StatusCode,
Duration: r.Duration,
Time: r.Time,
Result: r,
})
if err != nil {
log.Fatal(err)
}
}
func (l JSONLogger) Fatal(message string) {
err := l.Encoder.Encode(map[string]string{"Error": message})
if err != nil {
log.Fatal(err)
}
os.Exit(1)
}
func (l *JSONLogger) SetOutput(w io.Writer) {
l.Encoder = json.NewEncoder(w)
}