Skip to content

Commit

Permalink
Support configurable max log size with var NB_LOG_MAX_SIZE_MB
Browse files Browse the repository at this point in the history
  • Loading branch information
mlsmaycon committed Sep 12, 2024
1 parent 33c9b2d commit 94bf102
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@ import (
"os"
"path/filepath"
"slices"
"strconv"

log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"

"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)
if err != nil {
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,
Expand All @@ -46,3 +50,16 @@ 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", sizeVar, err)
return defaultLogSize
}

return int(size)
}
return defaultLogSize
}

0 comments on commit 94bf102

Please sign in to comment.