forked from skynetservices/skynet-archive
-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.go
172 lines (156 loc) · 3.86 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
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
package skynet
import (
"bytes"
"encoding/json"
"fmt"
"io"
"labix.org/v2/mgo"
"log"
"os"
"runtime"
"time"
)
// Logger is an interface to any logging utility.
type Logger interface {
// Log the item. Can be anything that is representable as a single JSON object.
Item(item interface{})
// Something has gone horribly wrong - remember what and bomb the program.
Panic(item interface{})
// these functions exists only to catch things that are not transitioned
Println(items ...interface{})
}
func MakeJObj(item interface{}) (jobj map[string]interface{}) {
jobj = map[string]interface{}{
"Time": time.Now(),
fmt.Sprintf("%T", item): item,
}
return
}
// MultiLogger is an implementation of the Logger interface that sends log
// messages out to multiple loggers.
type MultiLogger []Logger
func NewMultiLogger(loggers ...Logger) (ml MultiLogger) {
ml = loggers
return
}
func (ml MultiLogger) Item(item interface{}) {
for _, l := range ml {
l.Item(item)
}
}
func (ml MultiLogger) Println(items ...interface{}) {
for _, l := range ml {
l.Println(items...)
}
}
func (ml MultiLogger) Panic(item interface{}) {
for _, l := range ml {
l.Panic(item)
}
}
// ConsoleLogger is an implementation of the Logger interface that just
// prints to the console.
type ConsoleLogger struct {
l *log.Logger
untransitioned *log.Logger
}
func NewConsoleLogger(name string, w io.Writer) (cl *ConsoleLogger) {
cl = &ConsoleLogger{
l: log.New(w, fmt.Sprintf("%s: ", name), log.LstdFlags),
untransitioned: log.New(w, "fix-me: ", log.LstdFlags),
}
return
}
func (cl *ConsoleLogger) Item(item interface{}) {
switch s := item.(type) {
case fmt.Stringer:
cl.l.Print(s)
case string:
cl.l.Print(s)
case error:
cl.l.Print(s)
default:
jobj := MakeJObj(item)
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.Encode(jobj)
cl.l.Print(buf.String())
}
}
func (cl *ConsoleLogger) Println(items ...interface{}) {
cl.untransitioned.Println(items...)
}
func (cl *ConsoleLogger) Panic(item interface{}) {
cl.l.Panic(item)
}
// MongoLogger will archive log items into the specified mongo database.
// It is best of MongoLogger is given a capped collection. However, it
// can do nothing itself to ensure this, with the current state of mgo.
// The collection must be created as capped before any data is inserted.
type MongoLogger struct {
session *mgo.Session
dbName, colName string
uuid string
untransitioned *log.Logger
}
func NewMongoLogger(addr, dbName, collectionName, uuid string) (ml *MongoLogger, err error) {
ml = &MongoLogger{
dbName: dbName,
colName: collectionName,
untransitioned: log.New(os.Stderr, "fix-me: ", log.LstdFlags),
uuid: uuid,
}
ml.session, err = mgo.Dial(addr)
if err != nil {
ml.session = nil
}
return
}
func (ml *MongoLogger) Item(item interface{}) {
if ml.session == nil {
return
}
jobj := MakeJObj(item)
jobj["uuid"] = ml.uuid
db := ml.session.DB(ml.dbName)
col := db.C(ml.colName)
err := col.Insert(jobj)
if err != nil {
log.Printf("Could not log %v: %v", jobj, err)
}
}
func (ml *MongoLogger) Println(items ...interface{}) {
ml.Item(fmt.Sprint(items...))
ml.untransitioned.Println(items...)
}
func (ml *MongoLogger) Panic(item interface{}) {
if ml.session == nil {
return
}
var strace []string
for skip := 1; ; skip++ {
pc, file, line, ok := runtime.Caller(skip)
if !ok {
break
}
// if file[len(file)-1] == 'c' {
// continue
// }
f := runtime.FuncForPC(pc)
strace = append(strace, fmt.Sprintf("%s:%d %s()\n", file, line, f.Name()))
}
jobj := map[string]interface{}{
"Panic": map[string]interface{}{
"Item": item,
"Trace": strace,
},
"uuid": ml.uuid,
}
db := ml.session.DB(ml.dbName)
col := db.C(ml.colName)
err := col.Insert(jobj)
if err != nil {
log.Printf("Could not log %v: %v", jobj, err)
}
panic(item)
}