Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / runtime / log / log.go
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 // Package log contains utilities for fetching a new logger
18 // when one is not already available.
19 package log
20
21 import (
22         "io"
23         "os"
24         "time"
25
26         "github.com/go-logr/logr"
27         "github.com/go-logr/zapr"
28         "go.uber.org/zap"
29         "go.uber.org/zap/zapcore"
30 )
31
32 // ZapLogger is a Logger implementation.
33 // If development is true, a Zap development config will be used
34 // (stacktraces on warnings, no sampling), otherwise a Zap production
35 // config will be used (stacktraces on errors, sampling).
36 func ZapLogger(development bool) logr.Logger {
37         return ZapLoggerTo(os.Stderr, development)
38 }
39
40 // ZapLoggerTo returns a new Logger implementation using Zap which logs
41 // to the given destination, instead of stderr.  It otherise behaves like
42 // ZapLogger.
43 func ZapLoggerTo(destWriter io.Writer, development bool) logr.Logger {
44         // this basically mimics New<type>Config, but with a custom sink
45         sink := zapcore.AddSync(destWriter)
46
47         var enc zapcore.Encoder
48         var lvl zap.AtomicLevel
49         var opts []zap.Option
50         if development {
51                 encCfg := zap.NewDevelopmentEncoderConfig()
52                 enc = zapcore.NewConsoleEncoder(encCfg)
53                 lvl = zap.NewAtomicLevelAt(zap.DebugLevel)
54                 opts = append(opts, zap.Development(), zap.AddStacktrace(zap.ErrorLevel))
55         } else {
56                 encCfg := zap.NewProductionEncoderConfig()
57                 enc = zapcore.NewJSONEncoder(encCfg)
58                 lvl = zap.NewAtomicLevelAt(zap.InfoLevel)
59                 opts = append(opts, zap.AddStacktrace(zap.WarnLevel),
60                         zap.WrapCore(func(core zapcore.Core) zapcore.Core {
61                                 return zapcore.NewSampler(core, time.Second, 100, 100)
62                         }))
63         }
64         opts = append(opts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
65         log := zap.New(zapcore.NewCore(&KubeAwareEncoder{Encoder: enc, Verbose: development}, sink, lvl))
66         log = log.WithOptions(opts...)
67         return zapr.NewLogger(log)
68 }
69
70 // SetLogger sets a concrete logging implementation for all deferred Loggers.
71 func SetLogger(l logr.Logger) {
72         Log.Fulfill(l)
73 }
74
75 // Log is the base logger used by kubebuilder.  It delegates
76 // to another logr.Logger.  You *must* call SetLogger to
77 // get any actual logging.
78 var Log = NewDelegatingLogger(NullLogger{})
79
80 // KBLog is a base parent logger.
81 var KBLog logr.Logger
82
83 func init() {
84         KBLog = Log.WithName("kubebuilder")
85 }