-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
52 lines (42 loc) · 1.03 KB
/
handler.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
package handler
import (
"context"
"sync"
"github.com/btccom/go-micro/v2/debug/log"
"github.com/btccom/go-micro/v2/errors"
pb "github.com/btccom/go-micro-platform/v2/service/debug/log/proto"
)
type Log struct {
// per service log
sync.RWMutex
Logs map[string]log.Log
// Ability to create new logger
New func(string) log.Log
}
func (l *Log) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
if len(req.Service) == 0 {
return errors.BadRequest("go.micro.debug.log", "Invalid service name")
}
l.Lock()
defer l.Unlock()
// get the service log
serviceLog, ok := l.Logs[req.Service]
if !ok {
serviceLog = l.New(req.Service)
l.Logs[req.Service] = serviceLog
}
// TODO: specify how many log records to read
records, err := serviceLog.Read()
if err != nil {
return err
}
// append to records
for _, rec := range records {
rsp.Records = append(rsp.Records, &pb.Record{
Timestamp: rec.Timestamp.Unix(),
Metadata: rec.Metadata,
Message: rec.Message.(string),
})
}
return nil
}