[misc] Support configurable max log size with var NB_LOG_MAX_SIZE_MB (#2592)

* Support configurable max log size with var NB_LOG_MAX_SIZE_MB

* add better logs
This commit is contained in:
Maycon Santos 2024-09-12 19:56:55 +02:00 committed by GitHub
parent ab892b8cf9
commit f6d57e7a96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"slices"
"strconv"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
@ -12,6 +13,8 @@ import (
"github.com/netbirdio/netbird/formatter"
)
const defaultLogSize = 5
// InitLog parses and sets log-level input
func InitLog(logLevel string, logPath string) error {
level, err := log.ParseLevel(logLevel)
@ -19,13 +22,14 @@ func InitLog(logLevel string, logPath string) error {
log.Errorf("Failed parsing log-level %s: %s", logLevel, err)
return err
}
customOutputs := []string{"console", "syslog"};
customOutputs := []string{"console", "syslog"}
if logPath != "" && !slices.Contains(customOutputs, logPath) {
maxLogSize := getLogMaxSize()
lumberjackLogger := &lumberjack.Logger{
// Log file absolute path, os agnostic
Filename: filepath.ToSlash(logPath),
MaxSize: 5, // MB
MaxSize: maxLogSize, // MB
MaxBackups: 10,
MaxAge: 30, // days
Compress: true,
@ -46,3 +50,18 @@ func InitLog(logLevel string, logPath string) error {
log.SetLevel(level)
return nil
}
func getLogMaxSize() int {
if sizeVar, ok := os.LookupEnv("NB_LOG_MAX_SIZE_MB"); ok {
size, err := strconv.ParseInt(sizeVar, 10, 64)
if err != nil {
log.Errorf("Failed parsing log-size %s: %s. Should be just an integer", sizeVar, err)
return defaultLogSize
}
log.Infof("Setting log file max size to %d MB", size)
return int(size)
}
return defaultLogSize
}