|
| 1 | +// Copyright (C) 2021 Talos, Inc. |
| 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 | +// Provides a transition layer from "github.com/op/go-logging" to |
| 16 | +// "go.uber.org/zap" to simply resolve some reentrancy issues in go-logging. |
| 17 | +// |
| 18 | +// This provides a largely api compatible layer so we can quickly |
| 19 | +// drop in a replacement. |
| 20 | +package logging |
| 21 | + |
| 22 | +import ( |
| 23 | + "go.uber.org/zap" |
| 24 | + "go.uber.org/zap/zapcore" |
| 25 | + "sync" |
| 26 | +) |
| 27 | + |
| 28 | +// Zaplogger is a container to wrap zap logging with the parts of the go-logging API we use |
| 29 | +type ZapLogger struct { |
| 30 | + name string |
| 31 | + sugar *zap.SugaredLogger |
| 32 | + logger *zap.Logger |
| 33 | + config zap.Config |
| 34 | +} |
| 35 | + |
| 36 | +// Mapping of go-logging to zap log levels. It's imperfect but good enough |
| 37 | +const ( |
| 38 | + DEBUG = zapcore.DebugLevel |
| 39 | + INFO = zapcore.InfoLevel |
| 40 | + WARNING = zapcore.WarnLevel |
| 41 | + NOTICE = zapcore.InfoLevel // Map to info |
| 42 | + ERROR = zapcore.ErrorLevel |
| 43 | + CRITICAL = zapcore.DPanicLevel |
| 44 | +) |
| 45 | + |
| 46 | +// The go-logging package creates named loggers and allows them to be |
| 47 | +// accessed by name This is used in aeron to allow parent code to set |
| 48 | +// the logging level of library components so we need to provide a |
| 49 | +// mechanism for that |
| 50 | +var namedLoggers sync.Map // [string]*ZapLogger |
| 51 | + |
| 52 | +// MustGetLogger returns a new logger or panic()s |
| 53 | +func MustGetLogger(name string) *ZapLogger { |
| 54 | + z := new(ZapLogger) |
| 55 | + z.name = name |
| 56 | + |
| 57 | + z.config = aeronLoggingConfig() |
| 58 | + logger, err := z.config.Build() |
| 59 | + if err != nil { |
| 60 | + panic("Failed to make logger") |
| 61 | + } |
| 62 | + z.logger = logger.Named(name) |
| 63 | + z.sugar = z.logger.Sugar() |
| 64 | + |
| 65 | + // Keep a reference |
| 66 | + namedLoggers.Store(name, z) |
| 67 | + |
| 68 | + return z |
| 69 | +} |
| 70 | + |
| 71 | +// newAeronEncoderConfig returns a default opinionated EncoderConfig for aeron |
| 72 | +func newAeronEncoderConfig() zapcore.EncoderConfig { |
| 73 | + return zapcore.EncoderConfig{ |
| 74 | + TimeKey: "ts", |
| 75 | + LevelKey: "level", |
| 76 | + NameKey: "logger", |
| 77 | + CallerKey: "caller", |
| 78 | + FunctionKey: zapcore.OmitKey, |
| 79 | + MessageKey: "msg", |
| 80 | + StacktraceKey: "stacktrace", |
| 81 | + LineEnding: zapcore.DefaultLineEnding, |
| 82 | + EncodeLevel: zapcore.LowercaseLevelEncoder, |
| 83 | + EncodeTime: zapcore.ISO8601TimeEncoder, |
| 84 | + EncodeDuration: zapcore.SecondsDurationEncoder, |
| 85 | + EncodeCaller: zapcore.ShortCallerEncoder, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Default config |
| 90 | +func aeronLoggingConfig() zap.Config { |
| 91 | + return zap.Config{ |
| 92 | + Level: zap.NewAtomicLevelAt(zap.InfoLevel), |
| 93 | + Development: false, |
| 94 | + Sampling: &zap.SamplingConfig{ |
| 95 | + Initial: 100, |
| 96 | + Thereafter: 100, |
| 97 | + }, |
| 98 | + Encoding: "console", |
| 99 | + EncoderConfig: newAeronEncoderConfig(), |
| 100 | + OutputPaths: []string{"stderr"}, |
| 101 | + ErrorOutputPaths: []string{"stderr"}, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// SetConfigAndRebuild so you can replace the default config |
| 106 | +func (z *ZapLogger) SetConfigAndRebuild(c zap.Config) error { |
| 107 | + z.config = c |
| 108 | + logger, err := z.config.Build() |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + z.logger = logger.Named(z.name) |
| 114 | + z.sugar = z.logger.Sugar() |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +// SetLevel on a named logger |
| 119 | +func SetLevel(l zapcore.Level, name string) { |
| 120 | + z, ok := namedLoggers.Load(name) |
| 121 | + if ok { |
| 122 | + zlogger := z.(*ZapLogger) |
| 123 | + zlogger.SetLevel(l) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// GetLevel on a named logger |
| 128 | +func GetLevel(name string) zapcore.Level { |
| 129 | + z, ok := namedLoggers.Load(name) |
| 130 | + if ok { |
| 131 | + zlogger := z.(*ZapLogger) |
| 132 | + return zlogger.GetLevel() |
| 133 | + } |
| 134 | + |
| 135 | + // Bogus return but that's the API we are emulating and shouldn't happen |
| 136 | + return zapcore.InfoLevel |
| 137 | +} |
| 138 | + |
| 139 | +// Sugar returns the internal Sugared logger |
| 140 | +func (z *ZapLogger) Sugar() *zap.SugaredLogger { |
| 141 | + return z.sugar |
| 142 | +} |
| 143 | + |
| 144 | +// SetSugar sets our internal Sugared logger |
| 145 | +func (z *ZapLogger) SetSugar(s *zap.SugaredLogger) { |
| 146 | + z.sugar = s |
| 147 | +} |
| 148 | + |
| 149 | +// Logger returns the internal zap logger |
| 150 | +func (z *ZapLogger) Logger() *zap.Logger { |
| 151 | + return z.logger |
| 152 | +} |
| 153 | + |
| 154 | +// SetLogger sets the internal zap logger |
| 155 | +func (z *ZapLogger) SetLogger(l *zap.Logger) { |
| 156 | + z.logger = l |
| 157 | +} |
| 158 | + |
| 159 | +// SetLevel sets the log level at which we will log |
| 160 | +func (z *ZapLogger) SetLevel(l zapcore.Level) { |
| 161 | + z.config.Level.SetLevel(l) |
| 162 | +} |
| 163 | + |
| 164 | +// GetLevel returns the log level at which we will log |
| 165 | +func (z *ZapLogger) GetLevel() zapcore.Level { |
| 166 | + return z.config.Level.Level() |
| 167 | +} |
| 168 | + |
| 169 | +// IsEnabledFor returns true if logging is enabled for the specified level |
| 170 | +func (z *ZapLogger) IsEnabledFor(level zapcore.Level) bool { |
| 171 | + return level <= z.GetLevel() |
| 172 | +} |
| 173 | + |
| 174 | +// Fatalf logs a formatted string at log level Fatal and will then always exit() |
| 175 | +func (z *ZapLogger) Fatalf(template string, args ...interface{}) { |
| 176 | + z.sugar.Fatalf(template, args) |
| 177 | +} |
| 178 | + |
| 179 | +// Errorf logs a formatted string at log level Error |
| 180 | +func (z *ZapLogger) Errorf(template string, args ...interface{}) { |
| 181 | + z.sugar.Errorf(template, args) |
| 182 | +} |
| 183 | + |
| 184 | +// Warningf logs a formatted string at log level Warning |
| 185 | +func (z *ZapLogger) Warningf(template string, args ...interface{}) { |
| 186 | + z.sugar.Warnf(template, args) |
| 187 | +} |
| 188 | + |
| 189 | +// Infof logs a formatted string at log level Info |
| 190 | +func (z *ZapLogger) Infof(template string, args ...interface{}) { |
| 191 | + z.sugar.Infof(template, args) |
| 192 | +} |
| 193 | + |
| 194 | +// Noticef logs a formatted string at log level *Info* |
| 195 | +func (z *ZapLogger) Noticef(template string, args ...interface{}) { |
| 196 | + z.sugar.Infof(template, args) |
| 197 | +} |
| 198 | + |
| 199 | +// Debugf logs a formatted string at log level Debug |
| 200 | +func (z *ZapLogger) Debugf(template string, args ...interface{}) { |
| 201 | + z.sugar.Debugf(template, args) |
| 202 | +} |
| 203 | + |
| 204 | +// Fatal logs it's arguments at log level Fatal |
| 205 | +func (z *ZapLogger) Fatal(args ...interface{}) { |
| 206 | + z.sugar.Fatal(args) |
| 207 | +} |
| 208 | + |
| 209 | +// Error logs it's arguments at log level Error |
| 210 | +func (z *ZapLogger) Error(args ...interface{}) { |
| 211 | + z.sugar.Error(args) |
| 212 | +} |
| 213 | + |
| 214 | +// Warning logs it's arguments at log level Warning |
| 215 | +func (z *ZapLogger) Warning(args ...interface{}) { |
| 216 | + z.sugar.Warn(args) |
| 217 | +} |
| 218 | + |
| 219 | +// Info logs it's arguments at log level Info |
| 220 | +func (z *ZapLogger) Info(args ...interface{}) { |
| 221 | + z.sugar.Info(args) |
| 222 | +} |
| 223 | + |
| 224 | +// Notice logs it's arguments at log level *Info* |
| 225 | +func (z *ZapLogger) Notice(args ...interface{}) { |
| 226 | + z.sugar.Info(args) |
| 227 | +} |
| 228 | + |
| 229 | +// Debug logs it's arguments at log level Debug |
| 230 | +func (z *ZapLogger) Debug(args ...interface{}) { |
| 231 | + z.sugar.Debug(args) |
| 232 | +} |
0 commit comments