logger.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/sirupsen/logrus"
  6. )
  7. const (
  8. LogLevel = "LOG"
  9. )
  10. var (
  11. logger = logrus.New()
  12. )
  13. func init() {
  14. envLevel, _ := os.LookupEnv(LogLevel)
  15. if envLevel == "" {
  16. envLevel = "info"
  17. }
  18. level := logLevel(envLevel)
  19. logger.SetLevel(level)
  20. formatter := &logrus.TextFormatter{
  21. FullTimestamp: false,
  22. DisableLevelTruncation: true,
  23. }
  24. logger.SetFormatter(formatter)
  25. }
  26. func logLevel(lvl string) logrus.Level {
  27. switch lvl {
  28. case "trace":
  29. return logrus.TraceLevel
  30. case "debug":
  31. return logrus.DebugLevel
  32. case "info":
  33. return logrus.InfoLevel
  34. case "warn":
  35. return logrus.WarnLevel
  36. case "error":
  37. return logrus.ErrorLevel
  38. case "fatal":
  39. return logrus.FatalLevel
  40. default:
  41. panic(fmt.Sprintf("the specified %s log level is not supported. Use [trace|debug|info|warn|error|fatal]", lvl))
  42. }
  43. }
  44. func Info(values ...interface{}) {
  45. logger.Info(values...)
  46. }
  47. func Infof(fmt string, values ...interface{}) {
  48. logger.Infof(fmt, values...)
  49. }
  50. func Infoln(values ...interface{}) {
  51. logger.Infoln(values...)
  52. }
  53. func Debugf(fmt string, values ...interface{}) {
  54. logger.Debugf(fmt, values...)
  55. }
  56. func Debug(values ...interface{}) {
  57. logger.Debug(values...)
  58. }
  59. func Tracef(fmt string, values ...interface{}) {
  60. logger.Tracef(fmt, values...)
  61. }
  62. func Trace(values ...interface{}) {
  63. logger.Trace(values...)
  64. }
  65. func Warnf(fmt string, values ...interface{}) {
  66. logger.Warnf(fmt, values...)
  67. }
  68. func Fatalf(fmt string, values ...interface{}) {
  69. logger.Fatalf(fmt, values...)
  70. }
  71. func Errorf(fmt string, values ...interface{}) {
  72. logger.Errorf(fmt, values...)
  73. }
  74. func Error(values ...interface{}) {
  75. logger.Error(values...)
  76. }
  77. func Fatal(values ...interface{}) {
  78. logger.Fatal(values...)
  79. }
  80. func IsTrace() bool {
  81. return logger.IsLevelEnabled(logrus.TraceLevel)
  82. }