-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.go
More file actions
46 lines (39 loc) · 995 Bytes
/
Log.go
File metadata and controls
46 lines (39 loc) · 995 Bytes
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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
"go.mongodb.org/mongo-driver/mongo"
)
type Log struct {
Time int64 `json:"time" bson:"time"`
Level string `json:"level" bson:"level"`
Message string `json:"msg" bson:"msg"`
Peer string `json:"peer" bson:"peer"`
Group string `json:"group" bson:"group"`
PublicAddress string `json:"publicAddress" bson:"publicAddress"`
ExpireAt time.Time `json:"-" bson:"expireAt"`
}
type CustomWriter struct {
W io.Writer
LogsCollection *mongo.Collection
}
func (e CustomWriter) Write(p []byte) (int, error) {
go func() {
var l Log
err := json.Unmarshal(p, &l)
if err != nil {
fmt.Println(err)
return
}
// logs will be removed from db after 2 days
l.ExpireAt = time.Now().Add(time.Hour * 48)
_, err = e.LogsCollection.InsertOne(context.TODO(), l)
if err != nil {
fmt.Println(err)
}
}()
return fmt.Println(string(p))
}