github.com/braveheart12/insolar-09-08-19@v0.8.7/log/zerolog.go (about)

     1  /*
     2   *    Copyright 2019 Insolar
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package log
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"strings"
    24  
    25  	"github.com/pkg/errors"
    26  	"github.com/rs/zerolog"
    27  
    28  	"github.com/insolar/insolar/configuration"
    29  	"github.com/insolar/insolar/core"
    30  )
    31  
    32  func init() {
    33  	zerolog.TimeFieldFormat = timestampFormat
    34  }
    35  
    36  type zerologAdapter struct {
    37  	logger   zerolog.Logger
    38  }
    39  
    40  func newZerologAdapter(cfg configuration.Log) (*zerologAdapter, error) {
    41  	var output io.Writer
    42  	switch strings.ToLower(cfg.Formatter) {
    43  	case "text":
    44  		output = zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true, TimeFormat: timestampFormat}
    45  	case "json":
    46  		output = os.Stderr
    47  	default:
    48  		return nil, errors.New("unknown formatter " + cfg.Formatter)
    49  	}
    50  
    51  	zerolog.CallerSkipFrameCount = 3
    52  	return &zerologAdapter{logger: zerolog.New(output).Level(zerolog.InfoLevel).With().Timestamp().Caller().Logger()}, nil
    53  }
    54  
    55  // WithFields return copy of adapter with predefined fields.
    56  func (z *zerologAdapter) WithFields(fields map[string]interface{}) core.Logger {
    57  	w := z.logger.With()
    58  	for key, value := range fields {
    59  		w = w.Interface(key, value)
    60  	}
    61  	return &zerologAdapter{w.Logger() }
    62  }
    63  
    64  // WithField return copy of adapter with predefined single field.
    65  func (z *zerologAdapter) WithField(key string, value interface{}) core.Logger {
    66  	return &zerologAdapter{z.logger.With().Interface(key, value).Logger() }
    67  }
    68  
    69  // Debug logs a message at level Debug on the stdout.
    70  func (z *zerologAdapter) Debug(args ...interface{}) {
    71  	z.logger.Debug().Msg(fmt.Sprint(args...))
    72  }
    73  
    74  // Debugf formatted logs a message at level Debug on the stdout.
    75  func (z *zerologAdapter) Debugf(format string, args ...interface{}) {
    76  	z.logger.Debug().Msgf(format, args...)
    77  }
    78  
    79  // Info logs a message at level Info on the stdout.
    80  func (z *zerologAdapter) Info(args ...interface{}) {
    81  	z.logger.Info().Msg(fmt.Sprint(args...))
    82  }
    83  
    84  // Infof formatted logs a message at level Info on the stdout.
    85  func (z *zerologAdapter) Infof(format string, args ...interface{}) {
    86  	z.logger.Info().Msgf(format, args...)
    87  }
    88  
    89  // Warn logs a message at level Warn on the stdout.
    90  func (z *zerologAdapter) Warn(args ...interface{}) {
    91  	z.logger.Warn().Msg(fmt.Sprint(args...))
    92  }
    93  
    94  // Warnf formatted logs a message at level Warn on the stdout.
    95  func (z *zerologAdapter) Warnf(format string, args ...interface{}) {
    96  	z.logger.Warn().Msgf(format, args...)
    97  }
    98  
    99  // Error logs a message at level Error on the stdout.
   100  func (z *zerologAdapter) Error(args ...interface{}) {
   101  	z.logger.Error().Msg(fmt.Sprint(args...))
   102  }
   103  
   104  // Errorf formatted logs a message at level Error on the stdout.
   105  func (z *zerologAdapter) Errorf(format string, args ...interface{}) {
   106  	z.logger.Error().Msgf(format, args...)
   107  }
   108  
   109  // Fatal logs a message at level Fatal on the stdout.
   110  func (z *zerologAdapter) Fatal(args ...interface{}) {
   111  	z.logger.Fatal().Msg(fmt.Sprint(args...))
   112  }
   113  
   114  // Fatalf formatted logs a message at level Fatal on the stdout.
   115  func (z *zerologAdapter) Fatalf(format string, args ...interface{}) {
   116  	z.logger.Fatal().Msgf(format, args...)
   117  }
   118  
   119  // Panic logs a message at level Panic on the stdout.
   120  func (z *zerologAdapter) Panic(args ...interface{}) {
   121  	z.logger.Panic().Msg(fmt.Sprint(args...))
   122  }
   123  
   124  // Panicf formatted logs a message at level Panic on the stdout.
   125  func (z zerologAdapter) Panicf(format string, args ...interface{}) {
   126  	z.logger.Panic().Msgf(format, args...)
   127  }
   128  
   129  // SetLevel sets log level
   130  func (z *zerologAdapter) SetLevel(level string) error {
   131  	l, err := zerolog.ParseLevel(strings.ToLower(level))
   132  	if err != nil {
   133  		return errors.Wrap(err, "Failed to parse log level")
   134  	}
   135  
   136  	z.logger = z.logger.Level(l)
   137  	return nil
   138  }
   139  
   140  // SetOutput sets the output destination for the logger.
   141  func (z *zerologAdapter) SetOutput(w io.Writer) {
   142  	z.logger = z.logger.Output(w)
   143  }