Skip to content

Commit

Permalink
Merge 54205d7 into 9900512
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovic committed Feb 6, 2020
2 parents 9900512 + 54205d7 commit 88f0151
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
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
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
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 88f0151

Please sign in to comment.