fslog_instance.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package fslog
  2. import (
  3. "go.uber.org/zap"
  4. "go.uber.org/zap/zapcore"
  5. "os"
  6. "path/filepath"
  7. "runtime/debug"
  8. "strings"
  9. "sync"
  10. )
  11. const (
  12. LoggerLevelInfo = "info"
  13. LoggerLevelError = "error"
  14. LoggerLevelDebug = "debug"
  15. LoggerLevelWarn = "Warn"
  16. )
  17. var LoggerLever = LoggerLevelDebug
  18. func chooseLoggerLever() zapcore.Level {
  19. switch LoggerLever {
  20. case LoggerLevelInfo:
  21. return zapcore.InfoLevel
  22. case LoggerLevelError:
  23. return zapcore.ErrorLevel
  24. case LoggerLevelDebug:
  25. return zapcore.DebugLevel
  26. case LoggerLevelWarn:
  27. return zapcore.WarnLevel
  28. default:
  29. return zapcore.DebugLevel
  30. }
  31. }
  32. var logger *zap.Logger
  33. var initLoggerOnce sync.Once
  34. const logDir = "logs"
  35. func lazyInitialize() {
  36. initLoggerOnce.Do(func() {
  37. if !pathExists(logDir) {
  38. err := os.MkdirAll(logDir, os.ModeDir|os.ModePerm)
  39. if err != nil {
  40. panic(err)
  41. }
  42. }
  43. logFilePath := filepath.Join(logDir, "log")
  44. file, err := os.Create(logFilePath)
  45. if err != nil {
  46. panic(err)
  47. }
  48. developmentEncoderConfig := zap.NewDevelopmentEncoderConfig()
  49. encoder := zapcore.NewConsoleEncoder(developmentEncoderConfig)
  50. core := zapcore.NewTee(
  51. zapcore.NewCore(encoder, zapcore.AddSync(file), chooseLoggerLever()),
  52. zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), chooseLoggerLever()),
  53. )
  54. logger = zap.New(core, zap.AddCaller())
  55. })
  56. }
  57. func Info(serviceName string, funcName string, callName string, message string) {
  58. lazyInitialize()
  59. callerFields := getCallerInfoForLog(serviceName, funcName, callName)
  60. logger.Info(message, callerFields...)
  61. }
  62. func Error(serviceName string, funcName string, callName string, message string) {
  63. lazyInitialize()
  64. callerFields := getCallerInfoForLog(serviceName, funcName, callName)
  65. logger.Error(message+"\n"+string(debug.Stack()), callerFields...)
  66. }
  67. func Debug(serviceName string, funcName string, callName string, message string) {
  68. lazyInitialize()
  69. callerFields := getCallerInfoForLog(serviceName, funcName, callName)
  70. logger.Debug(message, callerFields...)
  71. }
  72. func Warn(serviceName string, funcName string, callName string, message string) {
  73. lazyInitialize()
  74. callerFields := getCallerInfoForLog(serviceName, funcName, callName)
  75. logger.Warn(message, callerFields...)
  76. }
  77. func getCallerInfoForLog(serviceName string, funcName string, callName string) []zap.Field {
  78. fields := make([]zap.Field, 0)
  79. if strings.Trim(serviceName, " ") != "" {
  80. fields = append(fields, zap.String("service", serviceName))
  81. }
  82. if strings.Trim(funcName, " ") != "" {
  83. fields = append(fields, zap.String("function", funcName))
  84. }
  85. if strings.Trim(callName, " ") != "" {
  86. fields = append(fields, zap.String("call", callName))
  87. }
  88. return fields
  89. }
  90. func pathExists(path string) bool {
  91. _, err := os.Stat(path)
  92. if err == nil {
  93. return true
  94. }
  95. if os.IsNotExist(err) {
  96. return false
  97. }
  98. return false
  99. }