Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Log file size limit not honored after re-open signal #1438

Merged
merged 1 commit into from Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/log.go
Expand Up @@ -148,6 +148,9 @@ func (s *Server) ReOpenLogFile() {
fileLog := srvlog.NewFileLogger(opts.LogFile,
opts.Logtime, opts.Debug, opts.Trace, true)
s.SetLogger(fileLog, opts.Debug, opts.Trace)
if opts.LogSizeLimit > 0 {
fileLog.SetSizeLimit(opts.LogSizeLimit)
}
s.Noticef("File log re-opened")
}
}
Expand Down
60 changes: 60 additions & 0 deletions server/log_test.go
Expand Up @@ -184,6 +184,66 @@ func TestReOpenLogFile(t *testing.T) {
}
}

func TestFileLoggerSizeLimitAndReopen(t *testing.T) {
s := &Server{opts: &Options{}}
defer s.SetLogger(nil, false, false)

tmpDir, err := ioutil.TempDir("", "nats-server")
if err != nil {
t.Fatal("Could not create tmp dir")
}
defer os.RemoveAll(tmpDir)
file, err := ioutil.TempFile(tmpDir, "log_")
if err != nil {
t.Fatalf("Could not create the temp file: %v", err)
}
file.Close()

// Set a File log
s.opts.LogFile = file.Name()
s.opts.Logtime = true
s.opts.LogSizeLimit = 1000
s.ConfigureLogger()

// Add a trace
s.Noticef("this is a notice")

// Do a re-open...
s.ReOpenLogFile()

// Content should indicate that we have re-opened the log
buf, err := ioutil.ReadFile(s.opts.LogFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
if strings.HasSuffix(string(buf), "File log-reopened") {
t.Fatalf("File should indicate that file log was re-opened, got: %v", string(buf))
}

// Now make sure that the limit is still honored.
txt := make([]byte, 800)
for i := 0; i < len(txt); i++ {
txt[i] = 'A'
}
s.Noticef(string(txt))
for i := 0; i < len(txt); i++ {
txt[i] = 'B'
}
s.Noticef(string(txt))

buf, err = ioutil.ReadFile(s.opts.LogFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
sbuf := string(buf)
if strings.Contains(sbuf, "AAAAA") || strings.Contains(sbuf, "BBBBB") {
t.Fatalf("Looks like file was not rotated: %s", sbuf)
}
if !strings.Contains(sbuf, "Rotated log, backup saved") {
t.Fatalf("File should have been rotated, was not: %s", sbuf)
}
}

func TestNoPasswordsFromConnectTrace(t *testing.T) {
opts := DefaultOptions()
opts.NoLog = false
Expand Down