github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/log/log.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/cockroachdb/errors"
    11  	"github.com/getsentry/sentry-go"
    12  	"github.com/projecteru2/core/types"
    13  	"gopkg.in/natefinch/lumberjack.v2"
    14  
    15  	"github.com/rs/zerolog"
    16  )
    17  
    18  var (
    19  	globalLogger zerolog.Logger
    20  	sentryDSN    string
    21  )
    22  
    23  // SetupLog init logger
    24  func SetupLog(ctx context.Context, cfg *types.ServerLogConfig, dsn string) error {
    25  	level, err := zerolog.ParseLevel(strings.ToLower(cfg.Level))
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	var writer io.Writer
    31  	switch {
    32  	case cfg.Filename != "":
    33  		// file log always uses json format
    34  		writer = &lumberjack.Logger{
    35  			Filename:   cfg.Filename,
    36  			MaxBackups: cfg.MaxBackups, // files
    37  			MaxSize:    cfg.MaxSize,    // megabytes
    38  			MaxAge:     cfg.MaxAge,     // days
    39  		}
    40  	case !cfg.UseJSON:
    41  		writer = zerolog.ConsoleWriter{
    42  			Out:        os.Stdout,
    43  			TimeFormat: time.RFC822,
    44  		}
    45  	default:
    46  		writer = os.Stdout
    47  	}
    48  	rslog := zerolog.New(writer).With().Timestamp().Logger().Level(level)
    49  	zerolog.ErrorStackMarshaler = func(err error) any {
    50  		return errors.GetSafeDetails(err).SafeDetails
    51  	}
    52  	globalLogger = rslog
    53  	// Sentry
    54  	if dsn != "" {
    55  		sentryDSN = dsn
    56  		WithFunc("log.SetupLog").Infof(ctx, "sentry %v", sentryDSN)
    57  		_ = sentry.Init(sentry.ClientOptions{Dsn: sentryDSN})
    58  	}
    59  	return nil
    60  }
    61  
    62  // Fatalf forwards to sentry
    63  func Fatalf(ctx context.Context, err error, format string, args ...any) {
    64  	fatalf(ctx, err, format, nil, args...)
    65  }
    66  
    67  // Warnf is Warnf
    68  func Warnf(ctx context.Context, format string, args ...any) {
    69  	warnf(ctx, format, nil, args...)
    70  }
    71  
    72  // Warn is Warn
    73  func Warn(ctx context.Context, args ...any) {
    74  	Warnf(ctx, "%+v", args...)
    75  }
    76  
    77  // Infof is Infof
    78  func Infof(ctx context.Context, format string, args ...any) {
    79  	infof(ctx, format, nil, args...)
    80  }
    81  
    82  // Info is Info
    83  func Info(ctx context.Context, args ...any) {
    84  	Infof(ctx, "%+v", args...)
    85  }
    86  
    87  // Debugf is Debugf
    88  func Debugf(ctx context.Context, format string, args ...any) {
    89  	debugf(ctx, format, nil, args...)
    90  }
    91  
    92  // Debug is Debug
    93  func Debug(ctx context.Context, args ...any) {
    94  	Debugf(ctx, "%+v", args...)
    95  }
    96  
    97  // Errorf forwards to sentry
    98  func Errorf(ctx context.Context, err error, format string, args ...any) {
    99  	errorf(ctx, err, format, nil, args...)
   100  }
   101  
   102  // Error forwards to sentry
   103  func Error(ctx context.Context, err error, args ...any) {
   104  	Errorf(ctx, err, "%+v", args...)
   105  }