Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit edad9dd

Browse files
author
David Chung
authored
New logging infrastructure (#440)
Signed-off-by: David Chung <[email protected]>
1 parent a390bf2 commit edad9dd

32 files changed

+2022
-41
lines changed

cmd/manager/main.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package main
22

33
import (
4-
goflag "flag"
4+
"flag"
55
"os"
66
"path/filepath"
77
"time"
88

9-
log "github.com/Sirupsen/logrus"
9+
"github.com/Sirupsen/logrus"
1010
"github.com/docker/infrakit/pkg/cli"
1111
"github.com/docker/infrakit/pkg/discovery"
1212
"github.com/docker/infrakit/pkg/leader"
13+
"github.com/docker/infrakit/pkg/log"
1314
"github.com/docker/infrakit/pkg/manager"
1415
metadata_plugin "github.com/docker/infrakit/pkg/plugin/metadata"
1516
group_rpc "github.com/docker/infrakit/pkg/rpc/group"
@@ -43,15 +44,20 @@ func main() {
4344
Short: "Manager",
4445
}
4546

46-
logLevel := cmd.PersistentFlags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
47+
// Log setup
48+
logOptions := &log.ProdDefaults
49+
50+
cmd.PersistentPreRun = func(c *cobra.Command, args []string) {
51+
log.Configure(logOptions)
52+
}
53+
cmd.PersistentFlags().AddFlagSet(cli.Flags(logOptions))
54+
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
55+
4756
pluginName := cmd.PersistentFlags().String("name", "group", "Name of the manager")
4857
backendPlugin := cmd.PersistentFlags().String(
4958
"proxy-for-group",
5059
"group-stateless",
5160
"Name of the group plugin to proxy for.")
52-
cmd.PersistentPreRun = func(c *cobra.Command, args []string) {
53-
cli.SetLogLevel(*logLevel)
54-
}
5561

5662
buildConfig := func() config {
5763
return config{
@@ -66,20 +72,16 @@ func main() {
6672
etcdEnvironment(buildConfig),
6773
)
6874

69-
// glog flag compatibility
70-
cmd.PersistentFlags().AddGoFlagSet(goflag.CommandLine)
71-
goflag.CommandLine.Parse([]string{})
72-
7375
err := cmd.Execute()
7476
if err != nil {
75-
log.Error(err)
77+
logrus.Error(err)
7678
os.Exit(1)
7779
}
7880
}
7981

8082
func runMain(cfg config) error {
8183

82-
log.Infoln("Starting up manager:", cfg)
84+
logrus.Infoln("Starting up manager:", cfg)
8385

8486
mgr, err := manager.NewManager(cfg.plugins, cfg.leader, cfg.snapshot, cfg.pluginName)
8587
if err != nil {
@@ -107,7 +109,7 @@ func runMain(cfg config) error {
107109
metadata_plugin.Put([]string{"leader"}, isLeader, view)
108110
}
109111
} else {
110-
log.Warningln("Cannot check leader for metadata:", err)
112+
logrus.Warningln("Cannot check leader for metadata:", err)
111113
}
112114

113115
// update config
@@ -116,11 +118,11 @@ func runMain(cfg config) error {
116118
metadata_plugin.Put([]string{"configs"}, snapshot, view)
117119
}
118120
} else {
119-
log.Warningln("Cannot load snapshot for metadata:", err)
121+
logrus.Warningln("Cannot load snapshot for metadata:", err)
120122
}
121123

122124
case <-stopSnapshot:
123-
log.Infoln("Snapshot updater stopped")
125+
logrus.Infoln("Snapshot updater stopped")
124126
return
125127
}
126128
}
@@ -132,7 +134,7 @@ func runMain(cfg config) error {
132134

133135
mgr.Stop()
134136
close(stopSnapshot)
135-
log.Infoln("Manager stopped")
137+
logrus.Infoln("Manager stopped")
136138

137139
return err
138140
}

pkg/cli/logging.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
package cli
22

3-
import log "github.com/Sirupsen/logrus"
3+
import (
4+
"github.com/spf13/pflag"
5+
6+
"github.com/Sirupsen/logrus"
7+
"github.com/docker/infrakit/pkg/log"
8+
)
49

510
// DefaultLogLevel is the default log level value.
6-
var DefaultLogLevel = len(log.AllLevels) - 2
11+
var DefaultLogLevel = len(logrus.AllLevels) - 2
712

813
// SetLogLevel adjusts the logrus level.
914
func SetLogLevel(level int) {
10-
if level > len(log.AllLevels)-1 {
11-
level = len(log.AllLevels) - 1
15+
if level > len(logrus.AllLevels)-1 {
16+
level = len(logrus.AllLevels) - 1
1217
} else if level < 0 {
1318
level = 0
1419
}
15-
log.SetLevel(log.AllLevels[level])
20+
logrus.SetLevel(logrus.AllLevels[level])
21+
}
22+
23+
// Flags returns the set of logging flags
24+
func Flags(o *log.Options) *pflag.FlagSet {
25+
f := pflag.NewFlagSet("logging", pflag.ExitOnError)
26+
f.IntVar(&o.Level, "log", o.Level, "log level")
27+
f.BoolVar(&o.Stdout, "log-stdout", o.Stdout, "log to stdout")
28+
f.BoolVar(&o.CallFunc, "log-caller", o.CallFunc, "include caller function")
29+
f.BoolVar(&o.CallStack, "log-stack", o.CallStack, "include caller stack")
30+
f.StringVar(&o.Format, "log-format", o.Format, "log format: logfmt|term|json")
31+
return f
1632
}

pkg/leader/etcd/v3/etcd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66

77
"github.com/coreos/etcd/clientv3"
88
"github.com/docker/infrakit/pkg/leader"
9+
logutil "github.com/docker/infrakit/pkg/log"
910
"github.com/docker/infrakit/pkg/util/etcd/v3"
10-
log "github.com/golang/glog"
1111
"golang.org/x/net/context"
1212
)
1313

14+
var log = logutil.New("module", "etcd/leader")
15+
1416
// NewDetector return an implementation of leader detector
1517
func NewDetector(pollInterval time.Duration, client *etcd.Client) leader.Detector {
1618
return leader.NewPoller(pollInterval, func() (bool, error) {
@@ -25,7 +27,7 @@ func AmILeader(ctx context.Context, client *etcd.Client) (isLeader bool, err err
2527
var statusResp *clientv3.StatusResponse
2628

2729
defer func() {
28-
log.V(100).Infoln("checking status at", endpoint, "resp=", statusResp, "err=", err, "leader=", isLeader)
30+
log.Debug("checking status", "endpoint", endpoint, "resp", statusResp, "err", err, "leader", isLeader)
2931
}()
3032

3133
// get status of node

pkg/leader/etcd/v3/etcd_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/coreos/etcd/clientv3"
88
testutil "github.com/docker/infrakit/pkg/testing"
99
"github.com/docker/infrakit/pkg/util/etcd/v3"
10-
log "github.com/golang/glog"
1110
"github.com/stretchr/testify/require"
1211
"golang.org/x/net/context"
1312
)
@@ -29,7 +28,7 @@ func TestWithRealEtcd(t *testing.T) {
2928
<-time.After(1 * time.Second)
3029
_, err := etcd.LsMembers.Output(ip)
3130
if err == nil {
32-
log.Infoln("etcd running")
31+
log.Info("etcd running")
3332
break
3433
}
3534
}

pkg/log/log.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package log
2+
3+
import (
4+
"flag"
5+
"os"
6+
7+
"github.com/Sirupsen/logrus"
8+
"gopkg.in/inconshreveable/log15.v2"
9+
)
10+
11+
// DefaultLogLevel is the default log level value.
12+
var DefaultLogLevel = len(logrus.AllLevels) - 2
13+
14+
// SetLogLevel adjusts the logrus level.
15+
func SetLogLevel(level int) {
16+
if level > len(logrus.AllLevels)-1 {
17+
level = len(logrus.AllLevels) - 1
18+
} else if level < 0 {
19+
level = 0
20+
}
21+
logrus.SetLevel(logrus.AllLevels[level])
22+
}
23+
24+
// Options capture the logging configuration
25+
type Options struct {
26+
Level int
27+
Stdout bool
28+
Format string
29+
CallFunc bool
30+
CallStack bool
31+
}
32+
33+
// DevDefaults is the default options for development
34+
var DevDefaults = Options{
35+
Level: 5,
36+
Stdout: false,
37+
Format: "json",
38+
CallStack: true,
39+
}
40+
41+
// ProdDefaults is the default options for production
42+
var ProdDefaults = Options{
43+
Level: 4,
44+
Stdout: false,
45+
Format: "term",
46+
CallFunc: true,
47+
}
48+
49+
func init() {
50+
Configure(&DevDefaults)
51+
}
52+
53+
// New returns a logger of given context
54+
func New(ctx ...interface{}) log15.Logger {
55+
return log15.Root().New(ctx...)
56+
}
57+
58+
// Root returns the process's root logger
59+
func Root() log15.Logger {
60+
return log15.Root()
61+
}
62+
63+
// Configure configures the logging
64+
func Configure(options *Options) {
65+
66+
SetLogLevel(options.Level)
67+
68+
var f log15.Format
69+
switch options.Format {
70+
case "term":
71+
f = log15.TerminalFormat()
72+
case "json":
73+
f = log15.JsonFormatEx(true, true)
74+
case "logfmt":
75+
fallthrough
76+
default:
77+
f = log15.LogfmtFormat()
78+
}
79+
80+
var h log15.Handler
81+
if options.Stdout {
82+
h = log15.StreamHandler(os.Stdout, f)
83+
} else {
84+
h = log15.StreamHandler(os.Stderr, f)
85+
}
86+
87+
if options.CallFunc {
88+
h = log15.CallerFuncHandler(h)
89+
}
90+
if options.CallStack {
91+
h = log15.CallerStackHandler("%+v", h)
92+
}
93+
94+
switch options.Level {
95+
case 0:
96+
h = log15.DiscardHandler() // no output
97+
case 1:
98+
h = log15.LvlFilterHandler(log15.LvlCrit, h)
99+
case 2:
100+
h = log15.LvlFilterHandler(log15.LvlError, h)
101+
case 3:
102+
h = log15.LvlFilterHandler(log15.LvlWarn, h)
103+
case 4:
104+
h = log15.LvlFilterHandler(log15.LvlInfo, h)
105+
case 5:
106+
h = log15.LvlFilterHandler(log15.LvlDebug, h)
107+
default:
108+
h = log15.LvlFilterHandler(log15.LvlInfo, h)
109+
}
110+
log15.Root().SetHandler(h)
111+
112+
// Necessary to stop glog from complaining / noisy logs
113+
flag.CommandLine.Parse([]string{})
114+
}

pkg/store/etcd/v3/etcd.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package etcd
22

33
import (
44
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
5+
logutil "github.com/docker/infrakit/pkg/log"
56
"github.com/docker/infrakit/pkg/store"
67
"github.com/docker/infrakit/pkg/types"
78
"github.com/docker/infrakit/pkg/util/etcd/v3"
8-
log "github.com/golang/glog"
99
"golang.org/x/net/context"
1010
)
1111

@@ -15,6 +15,8 @@ const (
1515
DefaultKey = "infrakit/configs/groups.json"
1616
)
1717

18+
var log = logutil.New("module", "etcd/store")
19+
1820
// NewSnapshot returns a snapshot given the client
1921
func NewSnapshot(client *etcd.Client) (store.Snapshot, error) {
2022
return &snapshot{
@@ -42,13 +44,13 @@ func (s *snapshot) Save(obj interface{}) error {
4244
if err != nil {
4345
switch err {
4446
case context.Canceled:
45-
log.Warningf("ctx is canceled by another routine: %v", err)
47+
log.Warn("ctx is canceled by another routine", "err", err)
4648
case context.DeadlineExceeded:
47-
log.Warningf("ctx is attached with a deadline is exceeded: %v", err)
49+
log.Warn("ctx is attached with a deadline is exceeded", "err", err)
4850
case rpctypes.ErrEmptyKey:
49-
log.Warningf("client-side error: %v", err)
51+
log.Warn("client-side error", "err", err)
5052
default:
51-
log.Warningf("bad cluster endpoints, which are not etcd servers: %v", err)
53+
log.Warn("bad cluster endpoints, which are not etcd servers", "err", err)
5254
}
5355
}
5456
return err
@@ -64,23 +66,23 @@ func (s *snapshot) Load(output interface{}) error {
6466
if err != nil {
6567
switch err {
6668
case context.Canceled:
67-
log.Warningf("ctx is canceled by another routine: %v", err)
69+
log.Warn("ctx is canceled by another routine", "err", err)
6870
case context.DeadlineExceeded:
69-
log.Warningf("ctx is attached with a deadline is exceeded: %v", err)
71+
log.Warn("ctx is attached with a deadline is exceeded", "err", err)
7072
case rpctypes.ErrEmptyKey:
71-
log.Warningf("client-side error: %v", err)
73+
log.Warn("client-side error", "err", err)
7274
default:
73-
log.Warningf("bad cluster endpoints, which are not etcd servers: %v", err)
75+
log.Warn("bad cluster endpoints, which are not etcd servers", "err", err)
7476
}
7577
}
7678

7779
if resp == nil {
78-
log.Warningln("response is nil. server down?")
80+
log.Warn("response is nil. server down?")
7981
return nil
8082
}
8183

8284
if resp.Count > 1 {
83-
log.Warningf("more than 1 config %v", resp)
85+
log.Warn("more than 1 config", "resp", resp)
8486
return nil
8587
}
8688

0 commit comments

Comments
 (0)