gitee.com/larksuite/oapi-sdk-go/v3@v3.0.3/core/logger.go (about)

     1  /*
     2   * MIT License
     3   *
     4   * Copyright (c) 2022 Lark Technologies Pte. Ltd.
     5   *
     6   * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
     7   *
     8   * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
     9   *
    10   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    11   */
    12  
    13  package larkcore
    14  
    15  import (
    16  	"context"
    17  	"log"
    18  	"os"
    19  )
    20  
    21  type LogLevel int
    22  
    23  const (
    24  	LogLevelDebug LogLevel = 1
    25  	LogLevelInfo  LogLevel = 2
    26  	LogLevelWarn  LogLevel = 3
    27  	LogLevelError LogLevel = 4
    28  )
    29  
    30  type loggerProxy struct {
    31  	LogLevel LogLevel
    32  	Logger   Logger
    33  }
    34  
    35  func newLoggerProxy(logLevel LogLevel, logger Logger) *loggerProxy {
    36  	return &loggerProxy{
    37  		LogLevel: logLevel,
    38  		Logger:   logger,
    39  	}
    40  }
    41  
    42  func (p *loggerProxy) Debug(ctx context.Context, args ...interface{}) {
    43  	if p.Logger != nil && p.LogLevel <= LogLevelDebug {
    44  		p.Logger.Debug(ctx, args...)
    45  	}
    46  }
    47  
    48  func (p *loggerProxy) Info(ctx context.Context, args ...interface{}) {
    49  	if p.Logger != nil && p.LogLevel <= LogLevelInfo {
    50  		p.Logger.Info(ctx, args...)
    51  	}
    52  }
    53  
    54  func (p *loggerProxy) Warn(ctx context.Context, args ...interface{}) {
    55  	if p.Logger != nil && p.LogLevel <= LogLevelWarn {
    56  		p.Logger.Warn(ctx, args...)
    57  	}
    58  }
    59  
    60  func (p *loggerProxy) Error(ctx context.Context, args ...interface{}) {
    61  	if p.Logger != nil && p.LogLevel <= LogLevelError {
    62  		p.Logger.Error(ctx, args...)
    63  	}
    64  }
    65  
    66  type Logger interface {
    67  	Debug(context.Context, ...interface{})
    68  	Info(context.Context, ...interface{})
    69  	Warn(context.Context, ...interface{})
    70  	Error(context.Context, ...interface{})
    71  }
    72  
    73  func NewLogger(config *Config) {
    74  	if config.Logger != nil {
    75  		logLevel := LogLevelInfo
    76  		if config.LogLevel != 0 {
    77  			logLevel = config.LogLevel
    78  		}
    79  		logger := newLoggerProxy(logLevel, config.Logger)
    80  		config.Logger = logger
    81  	} else {
    82  		logLevel := LogLevelInfo
    83  		if config.LogLevel != 0 {
    84  			logLevel = config.LogLevel
    85  		}
    86  		logger := newLoggerProxy(logLevel, defaultLogger{
    87  			logger: log.New(os.Stdout, "", log.LstdFlags),
    88  		})
    89  		config.Logger = logger
    90  	}
    91  }
    92  
    93  func NewEventLogger() Logger {
    94  	logger := defaultLogger{
    95  		logger: log.New(os.Stdout, "", log.LstdFlags),
    96  	}
    97  	return logger
    98  }
    99  
   100  type defaultLogger struct {
   101  	logger *log.Logger
   102  }
   103  
   104  func (l defaultLogger) Debug(ctx context.Context, args ...interface{}) {
   105  	l.logger.Printf("[Debug] %v", args)
   106  }
   107  
   108  func (l defaultLogger) Info(ctx context.Context, args ...interface{}) {
   109  	l.logger.Printf("[Info] %v", args)
   110  }
   111  
   112  func (l defaultLogger) Warn(ctx context.Context, args ...interface{}) {
   113  	l.logger.Printf("[Warn] %v", args)
   114  }
   115  
   116  func (l defaultLogger) Error(ctx context.Context, args ...interface{}) {
   117  	l.logger.Printf("[Error] %v", args)
   118  }