1 /* 2 Copyright The ORAS Authors. 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 16 package context 17 18 import ( 19 "context" 20 "io" 21 "io/ioutil" 22 23 "github.com/containerd/containerd/log" 24 "github.com/sirupsen/logrus" 25 ) 26 27 // WithLogger returns a new context with the provided logger. 28 // This method wraps github.com/containerd/containerd/log.WithLogger() 29 func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context { 30 return log.WithLogger(ctx, logger) 31 } 32 33 // WithLoggerFromWriter returns a new context with the logger, writting to the provided logger. 34 func WithLoggerFromWriter(ctx context.Context, writer io.Writer) context.Context { 35 logger := logrus.New() 36 logger.Out = writer 37 entry := logrus.NewEntry(logger) 38 return WithLogger(ctx, entry) 39 } 40 41 // WithLoggerDiscarded returns a new context with the logger, writting to nothing. 42 func WithLoggerDiscarded(ctx context.Context) context.Context { 43 return WithLoggerFromWriter(ctx, ioutil.Discard) 44 } 45 46 // GetLogger retrieves the current logger from the context. 47 // This method wraps github.com/containerd/containerd/log.GetLogger() 48 func GetLogger(ctx context.Context) *logrus.Entry { 49 return log.GetLogger(ctx) 50 } 51