{"record":{"id":"5dd74dadbe646dc9","repo":"owasp-amass/amass","slug":"failed-to-open-the-log-file-v","errorCode":null,"errorMessage":"failed to open the log file: %v","messagePattern":"failed to open the log file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tools/log.go","lineNumber":49,"sourceCode":"\t}\n\n\treturn errors.New(\"logger handler is not enabled\")\n}\n\nfunc NewFileLogger(dir, logfile string) (*slog.Logger, error) {\n\tif logfile == \"\" {\n\t\treturn nil, fmt.Errorf(\"no log file specified\")\n\t}\n\n\tif dir != \"\" {\n\t\tif err := os.MkdirAll(dir, 0640); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create the log directory: %v\", err)\n\t\t}\n\t}\n\n\tf, err := os.OpenFile(filepath.Join(dir, logfile), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to open the log file: %v\", err)\n\t}\n\n\treturn slog.New(slog.NewJSONHandler(f, nil)), nil\n}\n\nfunc NewSyslogLogger() (*slog.Logger, error) {\n\tport := os.Getenv(\"SYSLOG_PORT\")\n\thost := strings.ToLower(os.Getenv(\"SYSLOG_HOST\"))\n\ttransport := strings.ToLower(os.Getenv(\"SYSLOG_TRANSPORT\"))\n\n\tif host == \"\" {\n\t\treturn nil, fmt.Errorf(\"no syslog host specified\")\n\t}\n\tif port == \"\" {\n\t\tport = \"514\"\n\t}\n\tif transport == \"\" {\n\t\ttransport = \"udp\"","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/owasp-amass/amass/blob/79299dce87b0085db0f2f4ef3e9c52cccb49f514/internal/tools/log.go#L31-L67","documentation":"NewFileLogger failed at os.OpenFile on filepath.Join(dir, logfile) with append/create/write flags: commonly a permission error on the target path, a path that is a directory, or an unusable log directory (the dir itself was just created with MkdirAll, so that step succeeded). The os error is wrapped with %v, losing errors.Is matching.","triggerScenarios":"The joined path (dir/logfile) is unwritable, the path exists as a directory, the file is owned by another user without group write, the path exceeds NAME_MAX, or O_NOFOLLOW-style symlinks/permission issues (e.g. sticky /tmp misuse).","commonSituations":"Rotated log file replaced by a root-owned file; log file name accidentally equal to an existing directory; writing into /var/log without privileges; SELinux/AppArmor denials in hardened environments.","solutions":["Check the exact target path (filepath.Join(dir, logfile)) with ls -ld and fix ownership/permissions or pick another path","Ensure the target is not a directory and remove/rename it if so","Run the process as a user with write access to the directory, or pre-create the log file with suitable mode","Review audit logs (SELinux AVC / AppArmor) if permissions look correct but opening still fails"],"exampleFix":"// before\nf, _ := os.OpenFile(\"/var/log/amass.log\", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) // EACCES\n// after\nsudo touch /var/log/amass.log && sudo chown $USER /var/log/amass.log\n// or use a user-writable path: os.OpenFile(filepath.Join(home, \"amass.log\"), ...)","handlingStrategy":"try-catch","validationCode":"target := filepath.Join(dir, logfile)\nif st, err := os.Stat(target); err == nil && st.IsDir() {\n\treturn fmt.Errorf(\"log path %s is a directory\", target)\n}\nif dir != \"\" {\n\tprobe := filepath.Join(dir, \".wtest\")\n\tif err := os.WriteFile(probe, nil, 0600); err != nil {\n\t\treturn fmt.Errorf(\"log dir not writable: %v\", err)\n\t}\n\tos.Remove(probe)\n}","typeGuard":null,"tryCatchPattern":"logger, err := tools.NewFileLogger(dir, logfile)\nif err != nil {\n\tif strings.Contains(err.Error(), \"failed to open the log file\") {\n\t\t// fall back to stderr logging\n\t\tlogger = slog.New(slog.NewJSONHandler(os.Stderr, nil))\n\t} else { return err }\n}","preventionTips":["Check ls -ld on the exact joined path when configuring log destinations","Never reuse a directory name as the log file name","Fix log rotation so rotated files keep the writing user as owner","Review SELinux/AppArmor policies in hardened environments before writing to /var/log"],"tags":["go","logging","file-open","permissions"],"backgroundTag":"file-open-failed","analyzedSha":"79299dce87b0085db0f2f4ef3e9c52cccb49f514","analyzedAt":"2026-09-06T08:22:48.198Z","contentChangedAt":"2026-09-06T08:22:48.198Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}