Skip to content

Commit

Permalink
Migrate to zap logger (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
prydie committed Aug 15, 2018
1 parent 677de1e commit 2040fd6
Show file tree
Hide file tree
Showing 3,746 changed files with 615,474 additions and 579,696 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
513 changes: 402 additions & 111 deletions Gopkg.lock

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions Gopkg.toml
Expand Up @@ -20,10 +20,21 @@
# name = "github.com/x/y"
# version = "2.4.0"

# See: https://github.com/golang/dep/issues/1799
[[override]]
name = "gopkg.in/fsnotify.v1"
source = "https://github.com/fsnotify/fsnotify.git"

[[override]]
branch = "master"
name = "github.com/docker/distribution"

# glog -> zap
[[override]]
revision = "ad18aa91a01e5a7a4d312d16528a181979a23c62"
name = "github.com/golang/glog"
source = "github.com/prydie/glog"

[[override]]
branch = "release-1.10"
name = "k8s.io/apiextensions-apiserver"
Expand All @@ -32,14 +43,14 @@
revision = "50ae88d24ede7b8bad68e23c805b5d3da5c8abaf"
name = "k8s.io/kube-openapi"

[[constraint]]
branch = "master"
name = "github.com/golang/glog"

[[constraint]]
name = "github.com/spf13/pflag"
version = "1.0.0"

[[constraint]]
name = "go.uber.org/zap"
version = "1.9.0"

[[constraint]]
branch = "release-1.10"
name = "k8s.io/api"
Expand Down
10 changes: 8 additions & 2 deletions cmd/oci-cloud-controller-manager/main.go
Expand Up @@ -21,15 +21,16 @@ import (
"os"
"time"

"github.com/golang/glog"
"github.com/spf13/pflag"
"go.uber.org/zap"

utilflag "k8s.io/apiserver/pkg/util/flag"
"k8s.io/apiserver/pkg/util/logs"
"k8s.io/kubernetes/cmd/cloud-controller-manager/app"
_ "k8s.io/kubernetes/pkg/client/metrics/prometheus" // for client metric registration
_ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration

"github.com/oracle/oci-cloud-controller-manager/pkg/logging"
_ "github.com/oracle/oci-cloud-controller-manager/pkg/oci"
)

Expand All @@ -39,6 +40,10 @@ var build string
func main() {
rand.Seed(time.Now().UTC().UnixNano())

logger := logging.Logger()
defer logger.Sync()
zap.ReplaceGlobals(logger)

command := app.NewCloudControllerManagerCommand()

// TODO: once we switch everything over to Cobra commands, we can go back to calling
Expand All @@ -47,10 +52,11 @@ func main() {
pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
goflag.CommandLine.Parse([]string{})

logs.InitLogs()
defer logs.FlushLogs()

glog.V(1).Infof("oci-cloud-controller-manager version: %s (%s)", version, build)
logger.Sugar().With("version", version, "build", build).Info("oci-cloud-controller-manager")

if err := command.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
Expand Down
7 changes: 4 additions & 3 deletions hack/test-e2e.sh
Expand Up @@ -26,7 +26,10 @@ function run_e2e_tests() {
ginkgo -v -progress \
-focus "\[Canary\]" \
test/e2e \
-- --kubeconfig=${KUBECONFIG} --cloud-config=${CLOUDCONFIG} --delete-namespace=false
-- \
--kubeconfig="${KUBECONFIG}" \
--cloud-config="${CLOUDCONFIG}" \
--delete-namespace=false
}

# Main ************************************************************************
Expand Down Expand Up @@ -57,5 +60,3 @@ fi
run_e2e_tests

exit $?


135 changes: 135 additions & 0 deletions pkg/logging/logging.go
@@ -0,0 +1,135 @@
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logging

import (
"flag"
"os"
"strings"
"sync"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)

var (
lvl = zapcore.InfoLevel
logJSON = false
logfilePath = ""
config *zap.Config
mu sync.Mutex
)

func init() {
flag.Var(&lvl, "log-level", "Adjusts the level of the logs that will be omitted.")
flag.BoolVar(&logJSON, "log-json", logJSON, "Log in json format.")
flag.StringVar(&logfilePath, "logfile-path", "", "If specified, write log messages to a file at this path.")
}

// Options holds the zap logger configuration.
type Options struct {
LogLevel *zapcore.Level
Config *zap.Config
}

// Level gets the current log level.
func Level() *zap.AtomicLevel {
return &config.Level
}

// Logger builds a new logger based on the given flags.
func Logger() *zap.Logger {
mu.Lock()
defer mu.Unlock()

var cfg zap.Config

if !logJSON {
cfg = zap.NewDevelopmentConfig()
} else {
cfg = zap.NewProductionConfig()
}

// Extract log fields from environment variables.
envFields := FieldsFromEnv(os.Environ())

options := []zap.Option{
zap.AddStacktrace(zapcore.FatalLevel),
zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return c.With(envFields)
}),
}

if len(logfilePath) > 0 {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: logfilePath,
MaxSize: 10, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
})
var enc zapcore.Encoder
if logJSON {
enc = zapcore.NewJSONEncoder(cfg.EncoderConfig)
} else {
enc = zapcore.NewConsoleEncoder(cfg.EncoderConfig)
}
core := zapcore.NewCore(enc, w, lvl)
options = append(options, zap.WrapCore(func(zapcore.Core) zapcore.Core {
return core
}))
}

if config == nil {
config = &cfg
config.Level.SetLevel(lvl)
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
}

logger, err := config.Build(
// We handle this via errors package for 99% of the stuff so only
// enable this at the fatal/panic level.
options...,
)
if err != nil {
panic(err)
}

return logger
}

// FieldsFromEnv extracts log fields from environment variables.
// If an environment variable starts with LOG_FIELD_, the suffix is extracted
// and split on =. The first part is used for the name and the second for the
// value.
// For example, LOG_FIELD_foo=bar would result in a field named "foo" with the
// value "bar".
func FieldsFromEnv(env []string) []zapcore.Field {
const logfieldPrefix = "LOG_FIELD_"

fields := []zapcore.Field{}
for _, s := range env {
if !strings.HasPrefix(s, logfieldPrefix) || len(s) < (len(logfieldPrefix)+1) {
continue
}
s = s[len(logfieldPrefix):]
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
continue
}
fields = append(fields, zap.String(parts[0], parts[1]))
}
return fields
}
81 changes: 81 additions & 0 deletions pkg/logging/logging_test.go
@@ -0,0 +1,81 @@
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logging

import (
"reflect"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestFieldsFromEnv(t *testing.T) {
testCases := map[string]struct {
env []string
fields []zapcore.Field
}{
"single": {
env: []string{"LOG_FIELD_foo=bar"},
fields: []zapcore.Field{
zap.String("foo", "bar"),
},
},
"multiple": {
env: []string{
"LOG_FIELD_foo=bar",
"LOG_FIELD_bar=baz",
},
fields: []zapcore.Field{
zap.String("foo", "bar"),
zap.String("bar", "baz"),
},
},
"handles_equals_in_value": {
env: []string{
"LOG_FIELD_foo=a=b",
},
fields: []zapcore.Field{
zap.String("foo", "a=b"),
},
},
"handles_empty_value": {
env: []string{
"LOG_FIELD_foo=",
},
fields: []zapcore.Field{
zap.String("foo", ""),
},
},
"ignores_non_log_field": {
env: []string{
"LOG_FIELD_foo=bar",
"NOT_A_FIELD=1",
},
fields: []zapcore.Field{
zap.String("foo", "bar"),
},
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
fields := FieldsFromEnv(tc.env)
if !reflect.DeepEqual(fields, tc.fields) {
t.Errorf("Got incorrect fields:\nexpected=%+v\nactual=%+v", tc.fields, fields)
}
})
}
}

0 comments on commit 2040fd6

Please sign in to comment.