|
| 1 | +// Copyright 2019 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package zap |
| 16 | + |
| 17 | +import ( |
| 18 | + "flag" |
| 19 | + "fmt" |
| 20 | + "strconv" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "go.uber.org/zap" |
| 24 | + "go.uber.org/zap/zapcore" |
| 25 | + "k8s.io/klog" |
| 26 | +) |
| 27 | + |
| 28 | +type encoderConfigFunc func(*zapcore.EncoderConfig) |
| 29 | + |
| 30 | +type encoderValue struct { |
| 31 | + set bool |
| 32 | + newEncoder zapcore.Encoder |
| 33 | + str string |
| 34 | +} |
| 35 | + |
| 36 | +func (v *encoderValue) Set(e string) error { |
| 37 | + v.set = true |
| 38 | + if e == "json" || e == "console" { |
| 39 | + v.newEncoder = newEncoder(e) |
| 40 | + } else { |
| 41 | + return fmt.Errorf("unknown encoder \"%s\"", e) |
| 42 | + } |
| 43 | + v.str = e |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (v encoderValue) String() string { |
| 48 | + return v.str |
| 49 | +} |
| 50 | + |
| 51 | +func (v encoderValue) Type() string { |
| 52 | + return "encoder" |
| 53 | +} |
| 54 | + |
| 55 | +func newEncoder(e string, ecfs ...encoderConfigFunc) zapcore.Encoder { |
| 56 | + if e == "json" { |
| 57 | + encoderConfig := zap.NewProductionEncoderConfig() |
| 58 | + for _, f := range ecfs { |
| 59 | + f(&encoderConfig) |
| 60 | + } |
| 61 | + fmt.Println("HERE WITH JSON") |
| 62 | + return zapcore.NewJSONEncoder(encoderConfig) |
| 63 | + } else { |
| 64 | + encoderConfig := zap.NewDevelopmentEncoderConfig() |
| 65 | + for _, f := range ecfs { |
| 66 | + f(&encoderConfig) |
| 67 | + } |
| 68 | + fmt.Println("HERE WITH CONSOLE") |
| 69 | + return zapcore.NewConsoleEncoder(encoderConfig) |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +type levelValue struct { |
| 75 | + set bool |
| 76 | + level zap.AtomicLevel |
| 77 | +} |
| 78 | + |
| 79 | +func (v *levelValue) Set(l string) error { |
| 80 | + v.set = true |
| 81 | + lower := strings.ToLower(l) |
| 82 | + var lvl int |
| 83 | + switch lower { |
| 84 | + case "debug": |
| 85 | + lvl = -1 |
| 86 | + case "info": |
| 87 | + lvl = 0 |
| 88 | + case "error": |
| 89 | + lvl = 2 |
| 90 | + default: |
| 91 | + i, err := strconv.Atoi(lower) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("invalid log level \"%s\"", l) |
| 94 | + } |
| 95 | + |
| 96 | + if i > 0 { |
| 97 | + lvl = -1 * i |
| 98 | + } else { |
| 99 | + return fmt.Errorf("NO LIKEY log level \"%s\"", l) |
| 100 | + } |
| 101 | + } |
| 102 | + v.level.SetLevel(zapcore.Level(int8(lvl))) |
| 103 | + // If log level is greater than debug, set glog/klog level to that level. |
| 104 | + if lvl < -3 { |
| 105 | + fs := flag.NewFlagSet("", flag.ContinueOnError) |
| 106 | + klog.InitFlags(fs) |
| 107 | + err := fs.Set("v", fmt.Sprintf("%v", -1*lvl)) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + } |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (v levelValue) String() string { |
| 116 | + return v.level.String() |
| 117 | +} |
| 118 | + |
| 119 | +func (v levelValue) Type() string { |
| 120 | + return "level" |
| 121 | +} |
0 commit comments