| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package log provides logging functionality to notation. |
| 15 |
// Users who want to enable logging option in notation should implement the |
| 16 |
// log.Logger interface and include it in context by calling log.WithLogger. |
| 17 |
// 3rd party loggers that implement log.Logger: github.com/uber-go/zap.SugaredLogger |
| 18 |
// and github.com/sirupsen/logrus.Logger. |
| 19 |
package log |
| 20 |
|
| 21 |
import "context" |
| 22 |
|
| 23 |
type contextKey int |
| 24 |
|
| 25 |
// loggerKey is the associated key type for logger entry in context. |
| 26 |
const loggerKey contextKey = iota |
| 27 |
|
| 28 |
// Discard is a discardLogger that is used to disenable logging in notation. |
| 29 |
var Discard Logger = &discardLogger{} |
| 30 |
|
| 31 |
// Logger is implemented by users and/or 3rd party loggers. |
| 32 |
// For example, github.com/uber-go/zap.SugaredLogger |
| 33 |
// and github.com/sirupsen/logrus.Logger. |
| 34 |
type Logger interface { |
| 35 |
// Debug logs a debug level message. |
| 36 |
Debug(args ...interface{}) |
| 37 |
|
| 38 |
// Debugf logs a debug level message with format. |
| 39 |
Debugf(format string, args ...interface{}) |
| 40 |
|
| 41 |
// Debugln logs a debug level message. Spaces are always added between |
| 42 |
// operands. |
| 43 |
Debugln(args ...interface{}) |
| 44 |
|
| 45 |
// Info logs an info level message. |
| 46 |
Info(args ...interface{}) |
| 47 |
|
| 48 |
// Infof logs an info level message with format. |
| 49 |
Infof(format string, args ...interface{}) |
| 50 |
|
| 51 |
// Infoln logs an info level message. Spaces are always added between |
| 52 |
// operands. |
| 53 |
Infoln(args ...interface{}) |
| 54 |
|
| 55 |
// Warn logs a warn level message. |
| 56 |
Warn(args ...interface{}) |
| 57 |
|
| 58 |
// Warnf logs a warn level message with format. |
| 59 |
Warnf(format string, args ...interface{}) |
| 60 |
|
| 61 |
// Warnln logs a warn level message. Spaces are always added between |
| 62 |
// operands. |
| 63 |
Warnln(args ...interface{}) |
| 64 |
|
| 65 |
// Error logs an error level message. |
| 66 |
Error(args ...interface{}) |
| 67 |
|
| 68 |
// Errorf logs an error level message with format. |
| 69 |
Errorf(format string, args ...interface{}) |
| 70 |
|
| 71 |
// Errorln logs an error level message. Spaces are always added between |
| 72 |
// operands. |
| 73 |
Errorln(args ...interface{}) |
| 74 |
} |
| 75 |
|
| 76 |
// WithLogger is used by callers to set the Logger in the context. |
| 77 |
// It enables logging option in notation. |
| 78 |
func WithLogger(ctx context.Context, logger Logger) context.Context { |
| 79 |
return context.WithValue(ctx, loggerKey, logger) |
| 80 |
} |
| 81 |
|
| 82 |
// GetLogger is used to retrieve the Logger from the context. |
| 83 |
func GetLogger(ctx context.Context) Logger { |
| 84 |
if logger, ok := ctx.Value(loggerKey).(Logger); ok { |
| 85 |
return logger |
| 86 |
} |
| 87 |
return Discard |
| 88 |
} |
| 89 |
|
| 90 |
// discardLogger implements Logger but logs nothing. It is used when user |
| 91 |
// disenabled logging option in notation, i.e. loggerKey is not in the context. |
| 92 |
type discardLogger struct{} |
| 93 |
|
| 94 |
func (dl *discardLogger) Debug(args ...interface{}) { |
| 95 |
} |
| 96 |
|
| 97 |
func (dl *discardLogger) Debugf(format string, args ...interface{}) { |
| 98 |
} |
| 99 |
|
| 100 |
func (dl *discardLogger) Debugln(args ...interface{}) { |
| 101 |
} |
| 102 |
|
| 103 |
func (dl *discardLogger) Info(args ...interface{}) { |
| 104 |
} |
| 105 |
|
| 106 |
func (dl *discardLogger) Infof(format string, args ...interface{}) { |
| 107 |
} |
| 108 |
|
| 109 |
func (dl *discardLogger) Infoln(args ...interface{}) { |
| 110 |
} |
| 111 |
|
| 112 |
func (dl *discardLogger) Warn(args ...interface{}) { |
| 113 |
} |
| 114 |
|
| 115 |
func (dl *discardLogger) Warnf(format string, args ...interface{}) { |
| 116 |
} |
| 117 |
|
| 118 |
func (dl *discardLogger) Warnln(args ...interface{}) { |
| 119 |
} |
| 120 |
|
| 121 |
func (dl *discardLogger) Error(args ...interface{}) { |
| 122 |
} |
| 123 |
|
| 124 |
func (dl *discardLogger) Errorf(format string, args ...interface{}) { |
| 125 |
} |
| 126 |
|
| 127 |
func (dl *discardLogger) Errorln(args ...interface{}) { |
| 128 |
} |