{"record":{"id":"85b772559e9d5cf8","repo":"kataras/iris","slug":"err","errorCode":null,"errorMessage":"err","messagePattern":"err","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"middleware/accesslog/accesslog.go","lineNumber":342,"sourceCode":"// It panics on error.\nfunc File(path string) *AccessLog {\n\tf := mustOpenFile(path)\n\treturn New(bufio.NewReadWriter(bufio.NewReader(f), bufio.NewWriter(f)))\n}\n\n// FileUnbuffered same as File but it does not buffer the data,\n// it flushes the loggers contents as soon as possible.\nfunc FileUnbuffered(path string) *AccessLog {\n\tf := mustOpenFile(path)\n\treturn New(f)\n}\n\nfunc mustOpenFile(path string) *os.File {\n\t// Note: we add os.RDWR in order to be able to read from it,\n\t// some formatters (e.g. CSV) needs that.\n\tf, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn f\n}\n\n// Broker creates or returns the broker.\n// Use its `NewListener` and `CloseListener`\n// to listen and unlisten for incoming logs.\n//\n// Should be called before serve-time.\nfunc (ac *AccessLog) Broker() *Broker {\n\tac.mu.Lock()\n\tif ac.broker == nil {\n\t\tac.broker = newBroker()\n\t}\n\tac.mu.Unlock()\n\n\treturn ac.broker","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/middleware/accesslog/accesslog.go#L324-L360","documentation":"AccessLog's mustOpenFile panics if os.OpenFile fails to open (or create) the log file with read-write, append and 0600 permissions (middleware/accesslog/accesslog.go:342). The original os error is the panic value, so the message text equals the OS error string.","triggerScenarios":"Calling accesslog.New(f) with f := accesslog.File(path) or FileUnbuffered(path) where the directory does not exist, the process lacks write permission, or the path is a directory.","commonSituations":"Logging to a path under a non-existent directory (e.g. ./logs/app.log without a logs dir), running in a read-only container/filesystem, or a permission-denied path under restricted credentials.","solutions":["Read the wrapped OS error in the panic output and fix the root cause (create the directory with os.MkdirAll, fix permissions).","Create parent directories before calling accesslog.File: os.MkdirAll(filepath.Dir(path), 0755).","Open the file yourself with os.OpenFile and pass the *os.File to accesslog.New instead of using the must-style helper.","Run the process with a user that has write access to the log path."],"exampleFix":"// before\nac := accesslog.New(accesslog.File(\"./logs/app.log\")) // panics if ./logs missing\n\n// after\nos.MkdirAll(\"./logs\", 0755)\nf, err := os.OpenFile(\"./logs/app.log\", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)\nif err != nil { log.Fatal(err) }\nac := accesslog.New(f)","handlingStrategy":"fallback","validationCode":"// Pre-check the log path is writable\nif err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {\n    return err\n}\nf, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)\nif err != nil {\n    return fmt.Errorf(\"log file unavailable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"// Wrap accesslog.File with recover + fallback to stdout\nfunc safeFile(path string) (f *os.File) {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Printf(\"cannot open %s: %v; using stdout\", path, r)\n            f = os.Stdout\n        }\n    }()\n    return accesslog.File(path)\n}","preventionTips":["Create parent directories before opening log files.","Open files yourself and pass the handle to accesslog.New for explicit error handling.","Verify the process user has write permission on the log path, especially in containers.","Avoid logging to read-only filesystems; mount writable volumes."],"tags":["go","filesystem","panic","logging"],"backgroundTag":"file-open-failed","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}