-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap.go
56 lines (45 loc) · 1.38 KB
/
zap.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
package zap
import (
"context"
"strings"
"time"
stime "github.com/alexfalkowski/go-service/time"
"github.com/alexfalkowski/migrieren/client"
"github.com/alexfalkowski/migrieren/client/task"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Client for zap.
type Client struct {
logger *zap.Logger
cfg *client.Config
task.Task
}
// NewClient for zap.
func NewClient(logger *zap.Logger, cfg *client.Config, task task.Task) *Client {
return &Client{logger: logger, cfg: cfg, Task: task}
}
// Perform logger for client.
func (c *Client) Perform(ctx context.Context) ([]string, error) {
start := time.Now().UTC()
logs, err := c.Task.Perform(ctx)
fields := []zapcore.Field{
zap.Int64("client.duration", stime.ToMilliseconds(time.Since(start))),
zap.String("client.start_time", start.Format(time.RFC3339)),
zap.String("client.database", c.cfg.Database),
zap.Uint64("client.version", c.cfg.Version),
zap.String("span.kind", "client"),
zap.String("component", "client"),
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String("client.deadline", d.UTC().Format(time.RFC3339)))
}
if err != nil {
fields = append(fields, zap.Error(err))
c.logger.Error("finished call with error", fields...)
return nil, err
}
fields = append(fields, zap.String("client.logs", strings.Join(logs, ",")))
c.logger.Info("finished call with success", fields...)
return logs, nil
}