logger.go 5.9 KB

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