github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/log/log.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package log
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/sirupsen/logrus"
    24  	prefixed "github.com/x-cray/logrus-prefixed-formatter"
    25  )
    26  
    27  var (
    28  	rootLogger = logrus.NewEntry(logrus.StandardLogger())
    29  
    30  	// L accesses the current logger from the context
    31  	L = loggerFromContext
    32  )
    33  
    34  type (
    35  	ctxLogKey struct{}
    36  )
    37  
    38  // WithLogger adds the specified logger to the context
    39  func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
    40  	return context.WithValue(ctx, ctxLogKey{}, logger)
    41  }
    42  
    43  // WithLogField adds the specified field to the logger in the context
    44  func WithLogField(ctx context.Context, key, value string) context.Context {
    45  	if len(value) > 61 {
    46  		value = value[0:61] + "..."
    47  	}
    48  	return WithLogger(ctx, loggerFromContext(ctx).WithField(key, value))
    49  }
    50  
    51  // LoggerFromContext returns the logger for the current context, or no logger if there is no context
    52  func loggerFromContext(ctx context.Context) *logrus.Entry {
    53  	logger := ctx.Value(ctxLogKey{})
    54  	if logger == nil {
    55  		return rootLogger
    56  	}
    57  	return logger.(*logrus.Entry)
    58  }
    59  
    60  func SetLevel(level string) {
    61  	switch strings.ToLower(level) {
    62  	case "error":
    63  		logrus.SetLevel(logrus.ErrorLevel)
    64  	case "debug":
    65  		logrus.SetLevel(logrus.DebugLevel)
    66  	case "trace":
    67  		logrus.SetLevel(logrus.TraceLevel)
    68  	default:
    69  		logrus.SetLevel(logrus.InfoLevel)
    70  	}
    71  }
    72  
    73  type Formatting struct {
    74  	DisableColor    bool
    75  	ForceColor      bool
    76  	TimestampFormat string
    77  	UTC             bool
    78  }
    79  
    80  type utcFormat struct {
    81  	f logrus.Formatter
    82  }
    83  
    84  func (utc *utcFormat) Format(e *logrus.Entry) ([]byte, error) {
    85  	e.Time = e.Time.UTC()
    86  	return utc.f.Format(e)
    87  }
    88  
    89  func SetFormatting(format Formatting) {
    90  	var formatter logrus.Formatter = &prefixed.TextFormatter{
    91  		DisableColors:   format.DisableColor,
    92  		ForceColors:     format.ForceColor,
    93  		TimestampFormat: format.TimestampFormat,
    94  		DisableSorting:  false,
    95  		ForceFormatting: true,
    96  		FullTimestamp:   true,
    97  	}
    98  	if format.UTC {
    99  		formatter = &utcFormat{f: formatter}
   100  	}
   101  	logrus.SetFormatter(formatter)
   102  }