Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Changes proposed in this pull request: NATS Server 2.9 has `logfile_size_limit` option which allows the operator to set an optional byte limit on the NATS Server log file which when met causes a "rotation" such that the current log file is renamed (original file name appended with a time stamp to nanosecond accuracy) and a new log file is instantiated. This PR is a new `logfile_max_num` companion option (alias `log_max_num`) which allows the operator to designate that the server should prune the **total number of log files** -- the currently active log file plus backups -- to the maximum setting. A max value of `0` (the implicit default) or a negative number has meaning of unlimited log files (no maximum) as this is an opt-in feature. A max value of `1` is effectively a truncate-only logging pattern as any backup made at rotation will subsequently be purged. A max value of `2` will maintain the active log file plus the latest backup. And so on... > The currently active log file is never purged. Only backups are purged. When enabled, backup log deletion is evaluated inline after each successful rotation event. To be considered for log deletion, backup log files MUST adhere to the file naming format used in log rotation as well as agree with the current `logfile` name and location. Any other files or sub-directories in the log directory will be ignored. E.g. if an operator makes a manual copy of the log file to `logfile.bak` that file will not be evaluated as a backup. ### Typical use case: This feature is useful in a constrained hosting environment for NATS Server, for example an embedded, edge-compute, or IoT device scenario, in which _more featureful_ platform or operating system log management features do not exist or the complexity is not required.
- Loading branch information
Showing
4 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nats-io/nats-server/v2/server" | ||
) | ||
|
||
func RunServerWithLogging(opts *server.Options) *server.Server { | ||
if opts == nil { | ||
opts = &DefaultTestOptions | ||
} | ||
opts.NoLog = false | ||
opts.Cluster.PoolSize = -1 | ||
opts.Cluster.Compression.Mode = server.CompressionOff | ||
opts.LeafNode.Compression.Mode = server.CompressionOff | ||
s, err := server.NewServer(opts) | ||
if err != nil || s == nil { | ||
panic(fmt.Sprintf("No NATS Server object returned: %v", err)) | ||
} | ||
s.ConfigureLogger() | ||
go s.Start() | ||
if !s.ReadyForConnections(10 * time.Second) { | ||
panic("Unable to start NATS Server in Go Routine") | ||
} | ||
return s | ||
} | ||
|
||
func TestLogMaxArchives(t *testing.T) { | ||
// With logfile_size_limit set to small 100 characters, plain startup rotates 8 times | ||
for _, test := range []struct { | ||
name string | ||
config string | ||
totEntriesExpected int | ||
}{ | ||
{ | ||
"Default implicit, no max logs, expect 0 purged logs", | ||
` | ||
port: -1 | ||
log_file: %s | ||
logfile_size_limit: 100 | ||
`, | ||
9, | ||
}, | ||
{ | ||
"Default explicit, no max logs, expect 0 purged logs", | ||
` | ||
port: -1 | ||
log_file: %s | ||
logfile_size_limit: 100 | ||
logfile_max_num: 0 | ||
`, | ||
9, | ||
}, | ||
{ | ||
"Default explicit - negative val, no max logs, expect 0 purged logs", | ||
` | ||
port: -1 | ||
log_file: %s | ||
logfile_size_limit: 100 | ||
logfile_max_num: -42 | ||
`, | ||
9, | ||
}, | ||
{ | ||
"1-max num, expect 8 purged logs", | ||
` | ||
port: -1 | ||
log_file: %s | ||
logfile_size_limit: 100 | ||
logfile_max_num: 1 | ||
`, | ||
1, | ||
}, | ||
{ | ||
"5-max num, expect 4 purged logs; use opt alias", | ||
` | ||
port: -1 | ||
log_file: %s | ||
log_size_limit: 100 | ||
log_max_num: 5 | ||
`, | ||
5, | ||
}, | ||
{ | ||
"100-max num, expect 0 purged logs", | ||
` | ||
port: -1 | ||
log_file: %s | ||
logfile_size_limit: 100 | ||
logfile_max_num: 100 | ||
`, | ||
9, | ||
}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
d, err := os.MkdirTemp("", "logtest") | ||
if err != nil { | ||
t.Fatalf("Error creating temp dir: %v", err) | ||
} | ||
content := fmt.Sprintf(test.config, filepath.Join(d, "nats-server.log")) | ||
// server config does not like plain windows backslash | ||
if runtime.GOOS == "windows" { | ||
content = filepath.ToSlash(content) | ||
} | ||
opts, err := server.ProcessConfigFile(createConfFile(t, []byte(content))) | ||
if err != nil { | ||
t.Fatalf("Error processing config file: %v", err) | ||
} | ||
s := RunServerWithLogging(opts) | ||
if s == nil { | ||
t.Fatalf("No NATS Server object returned") | ||
} | ||
s.Shutdown() | ||
// Windows filesystem can be a little pokey on the flush, so wait a bit after shutdown... | ||
time.Sleep(500 * time.Millisecond) | ||
entries, err := os.ReadDir(d) | ||
if err != nil { | ||
t.Fatalf("Error reading dir: %v", err) | ||
} | ||
if len(entries) != test.totEntriesExpected { | ||
t.Fatalf("Expected %d log files, got %d", test.totEntriesExpected, len(entries)) | ||
} | ||
}) | ||
} | ||
} |