{"record":{"id":"b187d2453e2aad25","repo":"nats-io/nats-server","slug":"can-set-log-max-number-of-files-only-for-file-logg","errorCode":null,"errorMessage":"can set log max number of files only for file logger","messagePattern":"can set log max number of files only for file logger","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logger/log.go","lineNumber":315,"sourceCode":"// and the current log is truncated.\nfunc (l *Logger) SetSizeLimit(limit int64) error {\n\tl.Lock()\n\tif l.fl == nil {\n\t\tl.Unlock()\n\t\treturn fmt.Errorf(\"can set log size limit only for file logger\")\n\t}\n\tfl := l.fl\n\tl.Unlock()\n\tfl.setLimit(limit)\n\treturn nil\n}\n\n// SetMaxNumFiles sets the number of archived log files that will be retained\nfunc (l *Logger) SetMaxNumFiles(max int) error {\n\tl.Lock()\n\tif l.fl == nil {\n\t\tl.Unlock()\n\t\treturn fmt.Errorf(\"can set log max number of files only for file logger\")\n\t}\n\tfl := l.fl\n\tl.Unlock()\n\tfl.setMaxNumFiles(max)\n\treturn nil\n}\n\n// NewTestLogger creates a logger with output directed to Stderr with a prefix.\n// Useful for tracing in tests when multiple servers are in the same pid\nfunc NewTestLogger(prefix string, time bool) *Logger {\n\tflags := 0\n\tif time {\n\t\tflags = log.LstdFlags | log.Lmicroseconds\n\t}\n\tl := &Logger{\n\t\tlogger: log.New(os.Stderr, prefix, flags),\n\t\tdebug:  true,\n\t\ttrace:  true,","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/logger/log.go#L297-L333","documentation":"Logger.SetMaxNumFiles sets how many archived rotation files are retained, but like SetSizeLimit it operates on the underlying file logger. When l.fl is nil (no file sink attached), the call fails with this error.","triggerScenarios":"Calling SetMaxNumFiles on a logger not opened against a file — SetFileName/OpenFile never called, or file logging disabled in configuration.","commonSituations":"Rotation settings applied globally to console loggers in dev environments; initialization order where SetMaxNumFiles precedes SetFileName; conditional file-logging setups.","solutions":["Configure the file logger (SetFileName/OpenFile) before calling SetMaxNumFiles","Gate rotation-config calls behind the same flag that enables file logging","No-op or skip when the logger has no file sink"],"exampleFix":"// before\nlogger.SetMaxNumFiles(10) // panics-less error: no file logger\n// after\nif err := logger.SetFileName(\"/var/log/app.log\"); err == nil {\n    logger.SetMaxNumFiles(10)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := logger.SetMaxNumFiles(max); err != nil {\n    if strings.Contains(err.Error(), \"only for file logger\") {\n        return nil // rotation retention not applicable without file sink\n    }\n    return err\n}","preventionTips":["Ensure SetFileName/OpenFile runs before SetMaxNumFiles","Skip retention config when file logging is disabled","Centralize logger setup to enforce call order"],"tags":["logging","configuration","file-rotation"],"backgroundTag":"file-logger-not-configured","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}