2019-07-30 18:51:29 +00:00
|
|
|
// Package logger provides logging capabilities.
|
|
|
|
// It is a wrapper around zerolog for logging and lumberjack for log rotation.
|
2019-07-31 06:14:31 +00:00
|
|
|
// Logs are written to the specified log file.
|
|
|
|
// Logging on the console is provided to print initialization info, errors and warnings.
|
|
|
|
// The package provides a request logger to log the HTTP requests for REST API too.
|
2019-07-30 18:51:29 +00:00
|
|
|
// The request logger uses chi.middleware.RequestLogger,
|
|
|
|
// chi.middleware.LogFormatter and chi.middleware.LogEntry to build a structured
|
2019-10-08 08:29:16 +00:00
|
|
|
// logger using zerolog
|
2019-07-20 10:26:52 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-31 06:14:31 +00:00
|
|
|
"os"
|
2020-03-02 23:34:06 +00:00
|
|
|
"path/filepath"
|
2019-08-07 20:46:13 +00:00
|
|
|
"runtime"
|
2019-09-03 21:13:33 +00:00
|
|
|
"sync"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-11 14:20:00 +00:00
|
|
|
dateFormat = "2006-01-02T15:04:05.000" // YYYY-MM-DDTHH:MM:SS.ZZZ
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2019-09-06 13:19:01 +00:00
|
|
|
// LogLevel defines log levels.
|
|
|
|
type LogLevel uint8
|
|
|
|
|
|
|
|
// defines our own log level, just in case we'll change logger in future
|
|
|
|
const (
|
|
|
|
LevelDebug LogLevel = iota
|
|
|
|
LevelInfo
|
|
|
|
LevelWarn
|
|
|
|
LevelError
|
|
|
|
)
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
var (
|
2019-07-31 06:14:31 +00:00
|
|
|
logger zerolog.Logger
|
|
|
|
consoleLogger zerolog.Logger
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetLogger get the configured logger instance
|
2019-07-20 10:26:52 +00:00
|
|
|
func GetLogger() *zerolog.Logger {
|
|
|
|
return &logger
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:06:55 +00:00
|
|
|
// InitLogger configures the logger using the given parameters
|
|
|
|
func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) {
|
2019-07-20 10:26:52 +00:00
|
|
|
zerolog.TimeFieldFormat = dateFormat
|
2020-03-02 23:34:06 +00:00
|
|
|
if len(logFilePath) > 0 && filepath.Clean(logFilePath) != "." {
|
2019-09-03 21:13:33 +00:00
|
|
|
logger = zerolog.New(&lumberjack.Logger{
|
|
|
|
Filename: logFilePath,
|
|
|
|
MaxSize: logMaxSize,
|
|
|
|
MaxBackups: logMaxBackups,
|
|
|
|
MaxAge: logMaxAge,
|
|
|
|
Compress: logCompress,
|
|
|
|
})
|
2019-10-26 16:25:53 +00:00
|
|
|
EnableConsoleLogger(level)
|
2019-09-03 21:13:33 +00:00
|
|
|
} else {
|
|
|
|
logger = zerolog.New(logSyncWrapper{
|
|
|
|
output: os.Stdout,
|
|
|
|
lock: new(sync.Mutex)})
|
|
|
|
consoleLogger = zerolog.Nop()
|
2019-07-31 06:14:31 +00:00
|
|
|
}
|
2019-11-11 14:20:00 +00:00
|
|
|
logger.Level(level)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 16:25:53 +00:00
|
|
|
// DisableLogger disable the main logger.
|
|
|
|
// ConsoleLogger will not be affected
|
|
|
|
func DisableLogger() {
|
|
|
|
logger = zerolog.Nop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableConsoleLogger enables the console logger
|
|
|
|
func EnableConsoleLogger(level zerolog.Level) {
|
|
|
|
consoleOutput := zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stdout,
|
|
|
|
TimeFormat: dateFormat,
|
|
|
|
NoColor: runtime.GOOS == "windows",
|
|
|
|
}
|
|
|
|
consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger().Level(level)
|
|
|
|
}
|
|
|
|
|
2019-09-06 13:19:01 +00:00
|
|
|
// Log logs at the specified level for the specified sender
|
|
|
|
func Log(level LogLevel, sender string, connectionID string, format string, v ...interface{}) {
|
|
|
|
switch level {
|
|
|
|
case LevelDebug:
|
|
|
|
Debug(sender, connectionID, format, v...)
|
|
|
|
case LevelInfo:
|
|
|
|
Info(sender, connectionID, format, v...)
|
|
|
|
case LevelWarn:
|
|
|
|
Warn(sender, connectionID, format, v...)
|
|
|
|
default:
|
|
|
|
Error(sender, connectionID, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Debug logs at debug level for the specified sender
|
2019-09-05 14:21:35 +00:00
|
|
|
func Debug(sender string, connectionID string, format string, v ...interface{}) {
|
2019-11-11 14:20:00 +00:00
|
|
|
logger.Debug().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Info logs at info level for the specified sender
|
2019-09-05 14:21:35 +00:00
|
|
|
func Info(sender string, connectionID string, format string, v ...interface{}) {
|
2019-11-11 14:20:00 +00:00
|
|
|
logger.Info().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Warn logs at warn level for the specified sender
|
2019-09-05 14:21:35 +00:00
|
|
|
func Warn(sender string, connectionID string, format string, v ...interface{}) {
|
2019-11-11 14:20:00 +00:00
|
|
|
logger.Warn().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Error logs at error level for the specified sender
|
2019-09-05 14:21:35 +00:00
|
|
|
func Error(sender string, connectionID string, format string, v ...interface{}) {
|
2019-11-11 14:20:00 +00:00
|
|
|
logger.Error().Timestamp().Str("sender", sender).Str("connection_id", connectionID).Msg(fmt.Sprintf(format, v...))
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 06:14:31 +00:00
|
|
|
// DebugToConsole logs at debug level to stdout
|
|
|
|
func DebugToConsole(format string, v ...interface{}) {
|
|
|
|
consoleLogger.Debug().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfoToConsole logs at info level to stdout
|
|
|
|
func InfoToConsole(format string, v ...interface{}) {
|
|
|
|
consoleLogger.Info().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WarnToConsole logs at info level to stdout
|
|
|
|
func WarnToConsole(format string, v ...interface{}) {
|
|
|
|
consoleLogger.Warn().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorToConsole logs at error level to stdout
|
|
|
|
func ErrorToConsole(format string, v ...interface{}) {
|
|
|
|
consoleLogger.Error().Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
// TransferLog logs an SFTP/SCP upload or download
|
|
|
|
func TransferLog(operation string, path string, elapsed int64, size int64, user string, connectionID string, protocol string) {
|
2019-07-20 10:26:52 +00:00
|
|
|
logger.Info().
|
2019-11-11 14:20:00 +00:00
|
|
|
Timestamp().
|
2019-07-20 10:26:52 +00:00
|
|
|
Str("sender", operation).
|
|
|
|
Int64("elapsed_ms", elapsed).
|
|
|
|
Int64("size_bytes", size).
|
|
|
|
Str("username", user).
|
|
|
|
Str("file_path", path).
|
|
|
|
Str("connection_id", connectionID).
|
2019-08-24 12:41:15 +00:00
|
|
|
Str("protocol", protocol).
|
2019-07-20 10:26:52 +00:00
|
|
|
Msg("")
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:38:39 +00:00
|
|
|
// CommandLog logs an SFTP/SCP/SSH command
|
|
|
|
func CommandLog(command, path, target, user, fileMode, connectionID, protocol string, uid, gid int, atime, mtime, sshCommand string) {
|
2019-07-20 10:26:52 +00:00
|
|
|
logger.Info().
|
2019-11-11 14:20:00 +00:00
|
|
|
Timestamp().
|
2019-07-20 10:26:52 +00:00
|
|
|
Str("sender", command).
|
|
|
|
Str("username", user).
|
|
|
|
Str("file_path", path).
|
|
|
|
Str("target_path", target).
|
2019-11-15 11:15:07 +00:00
|
|
|
Str("filemode", fileMode).
|
|
|
|
Int("uid", uid).
|
|
|
|
Int("gid", gid).
|
2019-11-16 09:23:41 +00:00
|
|
|
Str("access_time", atime).
|
|
|
|
Str("modification_time", atime).
|
2019-11-19 10:38:39 +00:00
|
|
|
Str("ssh_command", sshCommand).
|
2019-07-20 10:26:52 +00:00
|
|
|
Str("connection_id", connectionID).
|
2019-08-24 12:41:15 +00:00
|
|
|
Str("protocol", protocol).
|
2019-07-20 10:26:52 +00:00
|
|
|
Msg("")
|
|
|
|
}
|
2019-11-11 14:20:00 +00:00
|
|
|
|
|
|
|
// ConnectionFailedLog logs failed attempts to initialize a connection.
|
|
|
|
// A connection can fail for an authentication error or other errors such as
|
|
|
|
// a client abort or a time out if the login does not happen in two minutes.
|
|
|
|
// These logs are useful for better integration with Fail2ban and similar tools.
|
|
|
|
func ConnectionFailedLog(user, ip, loginType, errorString string) {
|
|
|
|
logger.Debug().
|
|
|
|
Timestamp().
|
|
|
|
Str("sender", "connection_failed").
|
2019-11-11 18:53:27 +00:00
|
|
|
Str("client_ip", ip).
|
2019-11-11 14:20:00 +00:00
|
|
|
Str("username", user).
|
|
|
|
Str("login_type", loginType).
|
|
|
|
Str("error", errorString).
|
|
|
|
Msg("")
|
|
|
|
}
|