trpc.group/trpc-go/trpc-go@v1.0.3/log/logger.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package log
    15  
    16  import (
    17  	"io"
    18  )
    19  
    20  // Level is the log level.
    21  type Level int
    22  
    23  // Enums log level constants.
    24  const (
    25  	LevelNil Level = iota
    26  	LevelTrace
    27  	LevelDebug
    28  	LevelInfo
    29  	LevelWarn
    30  	LevelError
    31  	LevelFatal
    32  )
    33  
    34  // String turns the LogLevel to string.
    35  func (lv *Level) String() string {
    36  	return LevelStrings[*lv]
    37  }
    38  
    39  // LevelStrings is the map from log level to its string representation.
    40  var LevelStrings = map[Level]string{
    41  	LevelTrace: "trace",
    42  	LevelDebug: "debug",
    43  	LevelInfo:  "info",
    44  	LevelWarn:  "warn",
    45  	LevelError: "error",
    46  	LevelFatal: "fatal",
    47  }
    48  
    49  // LevelNames is the map from string to log level.
    50  var LevelNames = map[string]Level{
    51  	"trace": LevelTrace,
    52  	"debug": LevelDebug,
    53  	"info":  LevelInfo,
    54  	"warn":  LevelWarn,
    55  	"error": LevelError,
    56  	"fatal": LevelFatal,
    57  }
    58  
    59  // LoggerOptions is the log options.
    60  type LoggerOptions struct {
    61  	LogLevel Level
    62  	Pattern  string
    63  	Writer   io.Writer
    64  }
    65  
    66  // LoggerOption modifies the LoggerOptions.
    67  type LoggerOption func(*LoggerOptions)
    68  
    69  // Field is the user defined log field.
    70  type Field struct {
    71  	Key   string
    72  	Value interface{}
    73  }
    74  
    75  // Logger is the underlying logging work for tRPC framework.
    76  type Logger interface {
    77  	// Trace logs to TRACE log. Arguments are handled in the manner of fmt.Println.
    78  	Trace(args ...interface{})
    79  	// Tracef logs to TRACE log. Arguments are handled in the manner of fmt.Printf.
    80  	Tracef(format string, args ...interface{})
    81  	// Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Println.
    82  	Debug(args ...interface{})
    83  	// Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
    84  	Debugf(format string, args ...interface{})
    85  	// Info logs to INFO log. Arguments are handled in the manner of fmt.Println.
    86  	Info(args ...interface{})
    87  	// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
    88  	Infof(format string, args ...interface{})
    89  	// Warn logs to WARNING log. Arguments are handled in the manner of fmt.Println.
    90  	Warn(args ...interface{})
    91  	// Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
    92  	Warnf(format string, args ...interface{})
    93  	// Error logs to ERROR log. Arguments are handled in the manner of fmt.Println.
    94  	Error(args ...interface{})
    95  	// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
    96  	Errorf(format string, args ...interface{})
    97  	// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Println.
    98  	// All Fatal logs will exit by calling os.Exit(1).
    99  	// Implementations may also call os.Exit() with a non-zero exit code.
   100  	Fatal(args ...interface{})
   101  	// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
   102  	Fatalf(format string, args ...interface{})
   103  
   104  	// Sync calls the underlying Core's Sync method, flushing any buffered log entries.
   105  	// Applications should take care to call Sync before exiting.
   106  	Sync() error
   107  
   108  	// SetLevel sets the output log level.
   109  	SetLevel(output string, level Level)
   110  	// GetLevel gets the output log level.
   111  	GetLevel(output string) Level
   112  
   113  	// With adds user defined fields to Logger. Fields support multiple values.
   114  	With(fields ...Field) Logger
   115  }
   116  
   117  // OptionLogger defines logger with additional options.
   118  type OptionLogger interface {
   119  	WithOptions(opts ...Option) Logger
   120  }