Skip to content

Commit

Permalink
[ADDED] Ability to set logfile size limit
Browse files Browse the repository at this point in the history
Needed to pull NATS Server 2.1.4, and call the new SetSizeLimit() API

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
  • Loading branch information
kozlovic committed Feb 6, 2020
1 parent 9900512 commit 54205d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 11 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type StanLogger struct {
trace bool
ltime bool
lfile string
fszl int64
log natsd.Logger
}

Expand All @@ -55,6 +56,13 @@ func (s *StanLogger) SetLogger(log Logger, logtime, debug, trace bool, logfile s
s.mu.Unlock()
}

// SetFileSizeLimit sets the size limit for a logfile
func (s *StanLogger) SetFileSizeLimit(limit int64) {
s.mu.Lock()
s.fszl = limit
s.mu.Unlock()
}

// GetLogger returns the logger
func (s *StanLogger) GetLogger() Logger {
s.mu.RLock()
Expand All @@ -80,6 +88,9 @@ func (s *StanLogger) ReopenLogFile() {
}
}
fileLog := natsdLogger.NewFileLogger(s.lfile, s.ltime, s.debug, s.trace, true)
if s.fszl > 0 {
fileLog.SetSizeLimit(s.fszl)
}
s.log = fileLog
s.mu.Unlock()
s.Noticef("File log re-opened")
Expand Down
22 changes: 20 additions & 2 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -176,10 +177,15 @@ func TestLogger(t *testing.T) {
checkLogger("Unable to close logger: dummy error")

// Switch to file log
fname := "test.log"
defer os.Remove(fname)
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unable to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
fname := filepath.Join(tmpDir, "test.log")
fl := natsdLogger.NewFileLogger(fname, true, true, true, true)
logger.SetLogger(fl, true, true, true, fname)
logger.SetFileSizeLimit(1000)
// Reopen and check file content
logger.ReopenLogFile()
buf, err := ioutil.ReadFile(fname)
Expand All @@ -190,6 +196,18 @@ func TestLogger(t *testing.T) {
if !strings.Contains(string(buf), expectedStr) {
t.Fatalf("Expected log to contain %q, got %q", expectedStr, string(buf))
}
// Make sure that the file size limit was applied to the new file
notice := "This is some notice..."
for i := 0; i < 100; i++ {
logger.Noticef(notice)
}
files, err := ioutil.ReadDir(tmpDir)
if err != nil {
t.Fatalf("Unable to read temp dir: %v", err)
}
if len(files) == 1 {
t.Fatalf("Size limit was not applied")
}
logger.Close()
if err := os.Remove(fname); err != nil {
t.Fatalf("Unable to remove log file: %v", err)
Expand Down
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,11 @@ func (s *StanServer) configureLogger() {

if nOpts.LogFile != "" {
newLogger = natsdLogger.NewFileLogger(nOpts.LogFile, nOpts.Logtime, enableDebug, enableTrace, true)
if nOpts.LogSizeLimit > 0 {
if l, ok := newLogger.(*natsdLogger.Logger); ok {
l.SetSizeLimit(nOpts.LogSizeLimit)
}
}
} else if nOpts.RemoteSyslog != "" {
newLogger = natsdLogger.NewRemoteSysLogger(nOpts.RemoteSyslog, enableDebug, enableTrace)
} else if syslog {
Expand Down

0 comments on commit 54205d7

Please sign in to comment.