github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/framework/logger/entry.go (about)

     1  package logger
     2  
     3  import (
     4  	"context"
     5  	"github.com/sirupsen/logrus"
     6  	"github.com/unionj-cloud/go-doudou/framework/buildinfo"
     7  	"github.com/unionj-cloud/go-doudou/framework/internal/config"
     8  	"github.com/unionj-cloud/go-doudou/toolkit/constants"
     9  	"github.com/unionj-cloud/go-doudou/toolkit/stringutils"
    10  	"os"
    11  	"runtime"
    12  	"time"
    13  )
    14  
    15  var (
    16  	entry = New()
    17  )
    18  
    19  func Entry() *logrus.Entry {
    20  	return entry
    21  }
    22  
    23  func CheckDev() bool {
    24  	return stringutils.IsEmpty(os.Getenv("GDD_ENV")) || os.Getenv("GDD_ENV") == "dev"
    25  }
    26  
    27  // Deprecated: move to zerolog
    28  func New() *logrus.Entry {
    29  	hostname, _ := os.Hostname()
    30  	buildTime := buildinfo.BuildTime
    31  	if stringutils.IsNotEmpty(buildinfo.BuildTime) {
    32  		if t, err := time.Parse(constants.FORMAT15, buildinfo.BuildTime); err == nil {
    33  			buildTime = t.Local().Format(constants.FORMAT8)
    34  		}
    35  	}
    36  	if CheckDev() {
    37  		return logrus.NewEntry(logrus.StandardLogger())
    38  	}
    39  	return logrus.StandardLogger().WithFields(logrus.Fields{
    40  		"__meta_service":          config.GddServiceName,
    41  		"__meta_hostname":         hostname,
    42  		"__meta_go_version":       runtime.Version(),
    43  		"__meta_godoudou_version": buildinfo.GddVer,
    44  		"__meta_build_user":       buildinfo.BuildUser,
    45  		"__meta_build_time":       buildTime,
    46  	})
    47  }
    48  
    49  // WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
    50  func WithError(err error) *logrus.Entry {
    51  	return entry.WithField(logrus.ErrorKey, err)
    52  }
    53  
    54  // WithContext creates an entry from the standard logger and adds a context to it.
    55  func WithContext(ctx context.Context) *logrus.Entry {
    56  	return entry.WithContext(ctx)
    57  }
    58  
    59  // WithField creates an entry from the standard logger and adds a field to
    60  // it. If you want multiple fields, use `WithFields`.
    61  //
    62  // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
    63  // or Panic on the Entry it returns.
    64  func WithField(key string, value interface{}) *logrus.Entry {
    65  	return entry.WithField(key, value)
    66  }
    67  
    68  // WithFields creates an entry from the standard logger and adds multiple
    69  // fields to it. This is simply a helper for `WithField`, invoking it
    70  // once for each field.
    71  //
    72  // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
    73  // or Panic on the Entry it returns.
    74  func WithFields(fields logrus.Fields) *logrus.Entry {
    75  	if CheckDev() {
    76  		return entry
    77  	}
    78  	return entry.WithFields(fields)
    79  }
    80  
    81  // WithTime creates an entry from the standard logger and overrides the time of
    82  // logs generated with it.
    83  //
    84  // Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
    85  // or Panic on the Entry it returns.
    86  func WithTime(t time.Time) *logrus.Entry {
    87  	return entry.WithTime(t)
    88  }
    89  
    90  // Trace logs a message at level Trace on the standard logger.
    91  func Trace(args ...interface{}) {
    92  	entry.Trace(args...)
    93  }
    94  
    95  // Debug logs a message at level Debug on the standard logger.
    96  func Debug(args ...interface{}) {
    97  	entry.Debug(args...)
    98  }
    99  
   100  // Print logs a message at level Info on the standard logger.
   101  func Print(args ...interface{}) {
   102  	entry.Print(args...)
   103  }
   104  
   105  // Info logs a message at level Info on the standard logger.
   106  func Info(args ...interface{}) {
   107  	entry.Info(args...)
   108  }
   109  
   110  // Warn logs a message at level Warn on the standard logger.
   111  func Warn(args ...interface{}) {
   112  	entry.Warn(args...)
   113  }
   114  
   115  // Warning logs a message at level Warn on the standard logger.
   116  func Warning(args ...interface{}) {
   117  	entry.Warning(args...)
   118  }
   119  
   120  // Error logs a message at level Error on the standard logger.
   121  func Error(args ...interface{}) {
   122  	entry.Error(args...)
   123  }
   124  
   125  // Panic logs a message at level Panic on the standard logger.
   126  func Panic(args ...interface{}) {
   127  	entry.Panic(args...)
   128  }
   129  
   130  // Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
   131  func Fatal(args ...interface{}) {
   132  	entry.Fatal(args...)
   133  }
   134  
   135  // Tracef logs a message at level Trace on the standard logger.
   136  func Tracef(format string, args ...interface{}) {
   137  	entry.Tracef(format, args...)
   138  }
   139  
   140  // Debugf logs a message at level Debug on the standard logger.
   141  func Debugf(format string, args ...interface{}) {
   142  	entry.Debugf(format, args...)
   143  }
   144  
   145  // Printf logs a message at level Info on the standard logger.
   146  func Printf(format string, args ...interface{}) {
   147  	entry.Printf(format, args...)
   148  }
   149  
   150  // Infof logs a message at level Info on the standard logger.
   151  func Infof(format string, args ...interface{}) {
   152  	entry.Infof(format, args...)
   153  }
   154  
   155  // Warnf logs a message at level Warn on the standard logger.
   156  func Warnf(format string, args ...interface{}) {
   157  	entry.Warnf(format, args...)
   158  }
   159  
   160  // Warningf logs a message at level Warn on the standard logger.
   161  func Warningf(format string, args ...interface{}) {
   162  	entry.Warningf(format, args...)
   163  }
   164  
   165  // Errorf logs a message at level Error on the standard logger.
   166  func Errorf(format string, args ...interface{}) {
   167  	entry.Errorf(format, args...)
   168  }
   169  
   170  // Panicf logs a message at level Panic on the standard logger.
   171  func Panicf(format string, args ...interface{}) {
   172  	entry.Panicf(format, args...)
   173  }
   174  
   175  // Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
   176  func Fatalf(format string, args ...interface{}) {
   177  	entry.Fatalf(format, args...)
   178  }
   179  
   180  // Traceln logs a message at level Trace on the standard logger.
   181  func Traceln(args ...interface{}) {
   182  	entry.Traceln(args...)
   183  }
   184  
   185  // Debugln logs a message at level Debug on the standard logger.
   186  func Debugln(args ...interface{}) {
   187  	entry.Debugln(args...)
   188  }
   189  
   190  // Println logs a message at level Info on the standard logger.
   191  func Println(args ...interface{}) {
   192  	entry.Println(args...)
   193  }
   194  
   195  // Infoln logs a message at level Info on the standard logger.
   196  func Infoln(args ...interface{}) {
   197  	entry.Infoln(args...)
   198  }
   199  
   200  // Warnln logs a message at level Warn on the standard logger.
   201  func Warnln(args ...interface{}) {
   202  	entry.Warnln(args...)
   203  }
   204  
   205  // Warningln logs a message at level Warn on the standard logger.
   206  func Warningln(args ...interface{}) {
   207  	entry.Warningln(args...)
   208  }
   209  
   210  // Errorln logs a message at level Error on the standard logger.
   211  func Errorln(args ...interface{}) {
   212  	entry.Errorln(args...)
   213  }
   214  
   215  // Panicln logs a message at level Panic on the standard logger.
   216  func Panicln(args ...interface{}) {
   217  	entry.Panicln(args...)
   218  }
   219  
   220  // Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
   221  func Fatalln(args ...interface{}) {
   222  	entry.Fatalln(args...)
   223  }