{"record":{"id":"e32a614a7b706969","repo":"nats-io/nats-server","slug":"can-set-log-size-limit-only-for-file-logger","errorCode":null,"errorMessage":"can set log size limit only for file logger","messagePattern":"can set log size limit only for file logger","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logger/log.go","lineNumber":302,"sourceCode":"func (l *fileLogger) close() error {\n\tl.Lock()\n\tif l.closed {\n\t\tl.Unlock()\n\t\treturn nil\n\t}\n\tl.closed = true\n\tl.Unlock()\n\treturn l.f.Close()\n}\n\n// SetSizeLimit sets the size of a logfile after which a backup\n// is created with the file name + \"year.month.day.hour.min.sec.nanosec\"\n// 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","sourceCodeStart":284,"sourceCodeEnd":320,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/logger/log.go#L284-L320","documentation":"Logger.SetSizeLimit applies a size cap to rotating file-backed loggers. The limit lives on the file logger (l.fl); if the Logger was not created/opened as a file logger (SetFileName/OpenFile never called), fl is nil and the method returns this error instead.","triggerScenarios":"Calling SetSizeLimit on a Logger instance used only for stderr/stdout (no file sink configured) — e.g. logger created with a default constructor without SetFileName before calling.","commonSituations":"Apps configuring rotation limits on a logger whose file target was set conditionally (e.g. file logging disabled in dev); ordering bugs where SetSizeLimit is called before the file logger is opened.","solutions":["Call SetFileName/OpenFile (configure the file logger) before invoking SetSizeLimit","Skip SetSizeLimit when file logging is disabled, or return silently in that branch","Check that configuration actually enables file output before applying size limits"],"exampleFix":"// before\nlogger.SetSizeLimit(10 * 1024 * 1024) // fl == nil, errors\n// after\nif err := logger.SetFileName(\"/var/log/app.log\"); err == nil {\n    logger.SetSizeLimit(10 * 1024 * 1024)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := logger.SetSizeLimit(limit); err != nil {\n    if strings.Contains(err.Error(), \"only for file logger\") {\n        // console logger: size limit not applicable, skip\n        return nil\n    }\n    return err\n}","preventionTips":["Call SetFileName before any rotation settings","Guard rotation config behind the file-logging-enabled flag","Initialize logger config in one ordered function"],"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"}