github.com/wfusion/gofusion@v1.1.14/log/customlogger/metrics.go (about)

     1  package customlogger
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"strings"
     7  
     8  	"github.com/spf13/cast"
     9  
    10  	"github.com/wfusion/gofusion/common/infra/metrics"
    11  	"github.com/wfusion/gofusion/config"
    12  	"github.com/wfusion/gofusion/log"
    13  )
    14  
    15  var (
    16  	// MetricsLoggerType FIXME: should not be deleted to avoid compiler optimized
    17  	MetricsLoggerType = reflect.TypeOf(metricsLogger{})
    18  	metricsFields     = log.Fields{"component": strings.ToLower(config.ComponentMetrics)}
    19  )
    20  
    21  func DefaultMetricsLogger() metrics.Logger {
    22  	return &metricsLogger{}
    23  }
    24  
    25  type metricsLogger struct {
    26  	log      log.Loggable
    27  	appName  string
    28  	confName string
    29  	enabled  bool
    30  }
    31  
    32  func (m *metricsLogger) Init(log log.Loggable, appName, name string) {
    33  	m.log = log
    34  	m.appName = appName
    35  	m.confName = name
    36  	m.reloadConfig()
    37  }
    38  
    39  func (m *metricsLogger) Printf(format string, args ...any) {
    40  	if !m.isLoggable() {
    41  		return
    42  	}
    43  	m.logger().Info(context.Background(), format, append(args, metricsFields)...)
    44  }
    45  
    46  // Debug logs a message at Debug level.
    47  func (m *metricsLogger) Debug(args ...any) {
    48  	if !m.isLoggable() {
    49  		return
    50  	}
    51  	ctx, format, args := m.parseArgs(args...)
    52  	m.logger().Debug(ctx, format, args...)
    53  }
    54  
    55  // Info logs a message at Info level.
    56  func (m *metricsLogger) Info(args ...any) {
    57  	if !m.isLoggable() {
    58  		return
    59  	}
    60  	ctx, format, args := m.parseArgs(args...)
    61  	m.logger().Info(ctx, format, args...)
    62  }
    63  
    64  // Warn logs a message at Warning level.
    65  func (m *metricsLogger) Warn(args ...any) {
    66  	if !m.isLoggable() {
    67  		return
    68  	}
    69  	ctx, format, args := m.parseArgs(args...)
    70  	m.logger().Warn(ctx, format, args...)
    71  }
    72  
    73  // Error logs a message at Error level.
    74  func (m *metricsLogger) Error(args ...any) {
    75  	if !m.isLoggable() {
    76  		return
    77  	}
    78  	ctx, format, args := m.parseArgs(args...)
    79  	m.logger().Error(ctx, format, args...)
    80  }
    81  
    82  // Fatal logs a message at Fatal level
    83  // and process will exit with status set to 1.
    84  func (m *metricsLogger) Fatal(args ...any) {
    85  	if !m.isLoggable() {
    86  		return
    87  	}
    88  	ctx, format, args := m.parseArgs(args...)
    89  	m.logger().Fatal(ctx, format, args...)
    90  }
    91  
    92  func (m *metricsLogger) logger() log.Loggable {
    93  	if m.log != nil {
    94  		return m.log
    95  	}
    96  	return log.Use(config.DefaultInstanceKey, log.AppName(m.appName))
    97  }
    98  
    99  // parseArgs support (ctx, format, args...) log format
   100  func (m *metricsLogger) parseArgs(args ...any) (ctx context.Context, format string, params []any) {
   101  	var ok bool
   102  
   103  	if len(args) == 0 {
   104  		return context.Background(), "", []any{metricsFields}
   105  	}
   106  	if len(args) == 1 {
   107  		args = append(args, metricsFields)
   108  		return context.Background(), "%+v", args
   109  	}
   110  
   111  	format, ok = args[0].(string)
   112  	if ok {
   113  		params = args[1:]
   114  	} else {
   115  		ctx, _ = args[0].(context.Context)
   116  		format, _ = args[1].(string)
   117  		params = args[2:]
   118  	}
   119  	if format == "" {
   120  		placeholder := make([]string, len(args))
   121  		for i := 0; i < len(args); i++ {
   122  			placeholder[i] = "%+v"
   123  		}
   124  		format = strings.Join(placeholder, " ")
   125  		params = args
   126  	}
   127  
   128  	if ctx == nil {
   129  		ctx = context.Background()
   130  	}
   131  
   132  	params = append(params, metricsFields)
   133  	return
   134  }
   135  
   136  func (m *metricsLogger) isLoggable() bool {
   137  	if m.confName == "" {
   138  		return true
   139  	}
   140  	m.reloadConfig()
   141  	return m.enabled
   142  }
   143  
   144  func (m *metricsLogger) reloadConfig() {
   145  	var cfgs map[string]map[string]any
   146  	_ = config.Use(m.appName).LoadComponentConfig(config.ComponentMetrics, &cfgs)
   147  	if len(cfgs) == 0 {
   148  		return
   149  	}
   150  
   151  	cfg, ok := cfgs[m.confName]
   152  	if !ok {
   153  		return
   154  	}
   155  	enabled := cast.ToBool(cfg["enable_logger"])
   156  	m.enabled = enabled
   157  }