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