log.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package common
  2. import (
  3. "fmt"
  4. stdlog "log"
  5. "os"
  6. "runtime"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. var logCallerIgnoreFuncs = map[string]struct{}{
  12. "github.com/labring/aiproxy/core/middleware.logColor": {},
  13. }
  14. func InitLog(l *log.Logger, debug bool) {
  15. gin.ForceConsoleColor()
  16. if debug {
  17. l.SetLevel(log.DebugLevel)
  18. l.SetReportCaller(true)
  19. gin.SetMode(gin.DebugMode)
  20. } else {
  21. l.SetLevel(log.InfoLevel)
  22. l.SetReportCaller(false)
  23. gin.SetMode(gin.ReleaseMode)
  24. }
  25. l.SetOutput(os.Stdout)
  26. stdlog.SetOutput(l.Writer())
  27. l.SetFormatter(&log.TextFormatter{
  28. ForceColors: true,
  29. DisableColors: false,
  30. ForceQuote: debug,
  31. DisableQuote: !debug,
  32. DisableSorting: false,
  33. FullTimestamp: true,
  34. TimestampFormat: time.DateTime,
  35. QuoteEmptyFields: true,
  36. CallerPrettyfier: func(f *runtime.Frame) (function, file string) {
  37. if _, ok := logCallerIgnoreFuncs[f.Function]; ok {
  38. return "", ""
  39. }
  40. return f.Function, fmt.Sprintf("%s:%d", f.File, f.Line)
  41. },
  42. })
  43. if NeedColor() {
  44. gin.ForceConsoleColor()
  45. }
  46. }