logger.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Package logger provides logging capabilities.
  2. // It is a wrapper around zerolog for logging and lumberjack for log rotation.
  3. // Logs are written to the specified log file.
  4. // Logging on the console is provided to print initialization info, errors and warnings.
  5. // The package provides a request logger to log the HTTP requests for REST API too.
  6. // The request logger uses chi.middleware.RequestLogger,
  7. // chi.middleware.LogFormatter and chi.middleware.LogEntry to build a structured
  8. // logger using zerolog
  9. package logger
  10. import (
  11. "fmt"
  12. "os"
  13. "runtime"
  14. "sync"
  15. "github.com/rs/zerolog"
  16. lumberjack "gopkg.in/natefinch/lumberjack.v2"
  17. )
  18. const (
  19. dateFormat = "2006-01-02T15:04:05.000" // YYYY-MM-DDTHH:MM:SS.ZZZ
  20. )
  21. // LogLevel defines log levels.
  22. type LogLevel uint8
  23. // defines our own log level, just in case we'll change logger in future
  24. const (
  25. LevelDebug LogLevel = iota
  26. LevelInfo
  27. LevelWarn
  28. LevelError
  29. )
  30. var (
  31. logger zerolog.Logger
  32. consoleLogger zerolog.Logger
  33. )
  34. // GetLogger get the configured logger instance
  35. func GetLogger() *zerolog.Logger {
  36. return &logger
  37. }
  38. // InitLogger configures the logger using the given parameters
  39. func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) {
  40. zerolog.TimeFieldFormat = dateFormat
  41. if len(logFilePath) > 0 {
  42. logger = zerolog.New(&lumberjack.Logger{
  43. Filename: logFilePath,
  44. MaxSize: logMaxSize,
  45. MaxBackups: logMaxBackups,
  46. MaxAge: logMaxAge,
  47. Compress: logCompress,
  48. })
  49. EnableConsoleLogger(level)
  50. } else {
  51. logger = zerolog.New(logSyncWrapper{
  52. output: os.Stdout,
  53. lock: new(sync.Mutex)})
  54. consoleLogger = zerolog.Nop()
  55. }
  56. logger.Level(level)
  57. }
  58. // DisableLogger disable the main logger.
  59. // ConsoleLogger will not be affected
  60. func DisableLogger() {
  61. logger = zerolog.Nop()
  62. }
  63. // EnableConsoleLogger enables the console logger
  64. func EnableConsoleLogger(level zerolog.Level) {
  65. consoleOutput := zerolog.ConsoleWriter{
  66. Out: os.Stdout,
  67. TimeFormat: dateFormat,
  68. NoColor: runtime.GOOS == "windows",
  69. }
  70. consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger().Level(level)
  71. }
  72. // Log logs at the specified level for the specified sender
  73. func Log(level LogLevel, sender string, connectionID string, format string, v ...interface{}) {
  74. switch level {
  75. case LevelDebug:
  76. Debug(sender, connectionID, format, v...)
  77. case LevelInfo:
  78. Info(sender, connectionID, format, v...)
  79. case LevelWarn:
  80. Warn(sender, connectionID, format, v...)
  81. default:
  82. Error(sender, connectionID, format, v...)
  83. }
  84. }
  85. // Debug logs at debug level for the specified sender
  86. func Debug(sender string, connectionID string, format string, v ...interface{}) {
  87. logger.Debug().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
  88. }
  89. // Info logs at info level for the specified sender
  90. func Info(sender string, connectionID string, format string, v ...interface{}) {
  91. logger.Info().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
  92. }
  93. // Warn logs at warn level for the specified sender
  94. func Warn(sender string, connectionID string, format string, v ...interface{}) {
  95. logger.Warn().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
  96. }
  97. // Error logs at error level for the specified sender
  98. func Error(sender string, connectionID string, format string, v ...interface{}) {
  99. logger.Error().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
  100. }
  101. // DebugToConsole logs at debug level to stdout
  102. func DebugToConsole(format string, v ...interface{}) {
  103. consoleLogger.Debug().Msg(fmt.Sprintf(format, v...))
  104. }
  105. // InfoToConsole logs at info level to stdout
  106. func InfoToConsole(format string, v ...interface{}) {
  107. consoleLogger.Info().Msg(fmt.Sprintf(format, v...))
  108. }
  109. // WarnToConsole logs at info level to stdout
  110. func WarnToConsole(format string, v ...interface{}) {
  111. consoleLogger.Warn().Msg(fmt.Sprintf(format, v...))
  112. }
  113. // ErrorToConsole logs at error level to stdout
  114. func ErrorToConsole(format string, v ...interface{}) {
  115. consoleLogger.Error().Msg(fmt.Sprintf(format, v...))
  116. }
  117. // TransferLog logs an SFTP/SCP upload or download
  118. func TransferLog(operation string, path string, elapsed int64, size int64, user string, connectionID string, protocol string) {
  119. logger.Info().
  120. Timestamp().
  121. Str("sender", operation).
  122. Int64("elapsed_ms", elapsed).
  123. Int64("size_bytes", size).
  124. Str("username", user).
  125. Str("file_path", path).
  126. Str("connection_id", connectionID).
  127. Str("protocol", protocol).
  128. Msg("")
  129. }
  130. // CommandLog logs an SFTP/SCP command
  131. func CommandLog(command string, path string, target string, user string, connectionID string, protocol string) {
  132. logger.Info().
  133. Timestamp().
  134. Str("sender", command).
  135. Str("username", user).
  136. Str("file_path", path).
  137. Str("target_path", target).
  138. Str("connection_id", connectionID).
  139. Str("protocol", protocol).
  140. Msg("")
  141. }
  142. // ConnectionFailedLog logs failed attempts to initialize a connection.
  143. // A connection can fail for an authentication error or other errors such as
  144. // a client abort or a time out if the login does not happen in two minutes.
  145. // These logs are useful for better integration with Fail2ban and similar tools.
  146. func ConnectionFailedLog(user, ip, loginType, errorString string) {
  147. logger.Debug().
  148. Timestamp().
  149. Str("sender", "connection_failed").
  150. Str("client_ip", ip).
  151. Str("username", user).
  152. Str("login_type", loginType).
  153. Str("error", errorString).
  154. Msg("")
  155. }