|
| 1 | +// Copyright 2016 The Linux Foundation |
| 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 context |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/Sirupsen/logrus" |
| 23 | + "golang.org/x/net/context" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + // G is an alias for GetLogger. |
| 28 | + // |
| 29 | + // We may want to define this locally to a package to get package tagged log |
| 30 | + // messages. |
| 31 | + G = GetLogger |
| 32 | + |
| 33 | + // LogEntry provides a public and standard logger instance. |
| 34 | + LogEntry = logrus.NewEntry(logrus.StandardLogger()) |
| 35 | +) |
| 36 | + |
| 37 | +type ( |
| 38 | + loggerKey struct{} |
| 39 | + |
| 40 | + stderrHook struct{} |
| 41 | +) |
| 42 | + |
| 43 | +// Levels provide the method for logrus.Hook interface |
| 44 | +func (h *stderrHook) Levels() []logrus.Level { |
| 45 | + return []logrus.Level{ |
| 46 | + logrus.PanicLevel, |
| 47 | + logrus.FatalLevel, |
| 48 | + logrus.ErrorLevel, |
| 49 | + logrus.WarnLevel, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Fire provide the method for logrus.Hook interface |
| 54 | +func (h *stderrHook) Fire(entry *logrus.Entry) error { |
| 55 | + log := reflect.ValueOf(*entry.Logger).Interface().(logrus.Logger) |
| 56 | + entry.Logger = &log |
| 57 | + entry.Logger.Out = os.Stderr |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// WithLogger returns a new context with the provided logger. Use in |
| 62 | +// combination with logger.WithField(s) for great effect. |
| 63 | +func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context { |
| 64 | + return context.WithValue(ctx, loggerKey{}, logger) |
| 65 | +} |
| 66 | + |
| 67 | +// GetLogger retrieves the current logger from the context. If no logger is |
| 68 | +// available, the default logger is returned. |
| 69 | +func GetLogger(ctx context.Context) *logrus.Entry { |
| 70 | + logger := ctx.Value(loggerKey{}) |
| 71 | + |
| 72 | + if logger == nil { |
| 73 | + return LogEntry |
| 74 | + } |
| 75 | + |
| 76 | + return logger.(*logrus.Entry) |
| 77 | +} |
| 78 | + |
| 79 | +// EnableDebugMode enables a selectable debug mode. |
| 80 | +func EnableDebugMode(debug bool) error { |
| 81 | + var level string |
| 82 | + if LogEntry == nil { |
| 83 | + return fmt.Errorf("logger entry instance not found") |
| 84 | + } |
| 85 | + if debug { |
| 86 | + level = "debug" |
| 87 | + } else { |
| 88 | + level = "info" |
| 89 | + } |
| 90 | + lvl, err := logrus.ParseLevel(level) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + LogEntry.Logger.Level = lvl |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func init() { |
| 99 | + LogEntry.Logger.Out = os.Stdout |
| 100 | + LogEntry.Logger.Hooks.Add(&stderrHook{}) |
| 101 | +} |
0 commit comments