github.com/lingyao2333/mo-zero@v1.4.1/core/logc/logs.go (about)

     1  package logc
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/lingyao2333/mo-zero/core/logx"
     8  )
     9  
    10  type (
    11  	LogConf  = logx.LogConf
    12  	LogField = logx.LogField
    13  )
    14  
    15  // AddGlobalFields adds global fields.
    16  func AddGlobalFields(fields ...LogField) {
    17  	logx.AddGlobalFields(fields...)
    18  }
    19  
    20  // Alert alerts v in alert level, and the message is written to error log.
    21  func Alert(_ context.Context, v string) {
    22  	logx.Alert(v)
    23  }
    24  
    25  // Close closes the logging.
    26  func Close() error {
    27  	return logx.Close()
    28  }
    29  
    30  // Error writes v into error log.
    31  func Error(ctx context.Context, v ...interface{}) {
    32  	getLogger(ctx).Error(v...)
    33  }
    34  
    35  // Errorf writes v with format into error log.
    36  func Errorf(ctx context.Context, format string, v ...interface{}) {
    37  	getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
    38  }
    39  
    40  // Errorv writes v into error log with json content.
    41  // No call stack attached, because not elegant to pack the messages.
    42  func Errorv(ctx context.Context, v interface{}) {
    43  	getLogger(ctx).Errorv(v)
    44  }
    45  
    46  // Errorw writes msg along with fields into error log.
    47  func Errorw(ctx context.Context, msg string, fields ...LogField) {
    48  	getLogger(ctx).Errorw(msg, fields...)
    49  }
    50  
    51  // Field returns a LogField for the given key and value.
    52  func Field(key string, value interface{}) LogField {
    53  	return logx.Field(key, value)
    54  }
    55  
    56  // Info writes v into access log.
    57  func Info(ctx context.Context, v ...interface{}) {
    58  	getLogger(ctx).Info(v...)
    59  }
    60  
    61  // Infof writes v with format into access log.
    62  func Infof(ctx context.Context, format string, v ...interface{}) {
    63  	getLogger(ctx).Infof(format, v...)
    64  }
    65  
    66  // Infov writes v into access log with json content.
    67  func Infov(ctx context.Context, v interface{}) {
    68  	getLogger(ctx).Infov(v)
    69  }
    70  
    71  // Infow writes msg along with fields into access log.
    72  func Infow(ctx context.Context, msg string, fields ...LogField) {
    73  	getLogger(ctx).Infow(msg, fields...)
    74  }
    75  
    76  // Must checks if err is nil, otherwise logs the error and exits.
    77  func Must(err error) {
    78  	logx.Must(err)
    79  }
    80  
    81  // MustSetup sets up logging with given config c. It exits on error.
    82  func MustSetup(c logx.LogConf) {
    83  	logx.MustSetup(c)
    84  }
    85  
    86  // SetLevel sets the logging level. It can be used to suppress some logs.
    87  func SetLevel(level uint32) {
    88  	logx.SetLevel(level)
    89  }
    90  
    91  // SetUp sets up the logx. If already set up, just return nil.
    92  // we allow SetUp to be called multiple times, because for example
    93  // we need to allow different service frameworks to initialize logx respectively.
    94  // the same logic for SetUp
    95  func SetUp(c LogConf) error {
    96  	return logx.SetUp(c)
    97  }
    98  
    99  // Slow writes v into slow log.
   100  func Slow(ctx context.Context, v ...interface{}) {
   101  	getLogger(ctx).Slow(v...)
   102  }
   103  
   104  // Slowf writes v with format into slow log.
   105  func Slowf(ctx context.Context, format string, v ...interface{}) {
   106  	getLogger(ctx).Slowf(format, v...)
   107  }
   108  
   109  // Slowv writes v into slow log with json content.
   110  func Slowv(ctx context.Context, v interface{}) {
   111  	getLogger(ctx).Slowv(v)
   112  }
   113  
   114  // Sloww writes msg along with fields into slow log.
   115  func Sloww(ctx context.Context, msg string, fields ...LogField) {
   116  	getLogger(ctx).Sloww(msg, fields...)
   117  }
   118  
   119  // getLogger returns the logx.Logger with the given ctx and correct caller.
   120  func getLogger(ctx context.Context) logx.Logger {
   121  	return logx.WithContext(ctx).WithCallerSkip(1)
   122  }