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
|
|
|
|
// logger using zerlog
|
2019-07-20 10:26:52 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-31 06:14:31 +00:00
|
|
|
"os"
|
2019-08-07 20:46:13 +00:00
|
|
|
"runtime"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dateFormat = "2006-01-02T15:04.05.000" // YYYY-MM-DDTHH:MM.SS.ZZZ
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
logger = zerolog.New(&lumberjack.Logger{
|
|
|
|
Filename: logFilePath,
|
|
|
|
MaxSize: logMaxSize,
|
|
|
|
MaxBackups: logMaxBackups,
|
|
|
|
MaxAge: logMaxAge,
|
2019-07-31 12:06:55 +00:00
|
|
|
Compress: logCompress,
|
2019-07-20 10:26:52 +00:00
|
|
|
}).With().Timestamp().Logger().Level(level)
|
2019-07-31 06:14:31 +00:00
|
|
|
|
|
|
|
consoleOutput := zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stdout,
|
|
|
|
TimeFormat: dateFormat,
|
2019-08-07 20:46:13 +00:00
|
|
|
NoColor: runtime.GOOS == "windows",
|
2019-07-31 06:14:31 +00:00
|
|
|
}
|
2019-07-31 12:06:55 +00:00
|
|
|
consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger().Level(level)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Debug logs at debug level for the specified sender
|
2019-07-20 10:26:52 +00:00
|
|
|
func Debug(sender string, format string, v ...interface{}) {
|
|
|
|
logger.Debug().Str("sender", sender).Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Info logs at info level for the specified sender
|
2019-07-20 10:26:52 +00:00
|
|
|
func Info(sender string, format string, v ...interface{}) {
|
|
|
|
logger.Info().Str("sender", sender).Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Warn logs at warn level for the specified sender
|
2019-07-20 10:26:52 +00:00
|
|
|
func Warn(sender string, format string, v ...interface{}) {
|
|
|
|
logger.Warn().Str("sender", sender).Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Error logs at error level for the specified sender
|
2019-07-20 10:26:52 +00:00
|
|
|
func Error(sender string, format string, v ...interface{}) {
|
|
|
|
logger.Error().Str("sender", sender).Msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
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().
|
|
|
|
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-08-24 12:41:15 +00:00
|
|
|
// CommandLog logs an SFTP/SCP command
|
|
|
|
func CommandLog(command string, path string, target string, user string, connectionID string, protocol string) {
|
2019-07-20 10:26:52 +00:00
|
|
|
logger.Info().
|
|
|
|
Str("sender", command).
|
|
|
|
Str("username", user).
|
|
|
|
Str("file_path", path).
|
|
|
|
Str("target_path", target).
|
|
|
|
Str("connection_id", connectionID).
|
2019-08-24 12:41:15 +00:00
|
|
|
Str("protocol", protocol).
|
2019-07-20 10:26:52 +00:00
|
|
|
Msg("")
|
|
|
|
}
|