{"record":{"id":"cfd9a91c24d8e61c","repo":"slimtoolkit/slim","slug":"failed-to-set-log-output-destination-to-q-w","errorCode":null,"errorMessage":"failed to set log output destination to %q: %w","messagePattern":"failed to set log output destination to %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/sensor/logger.go","lineNumber":36,"sourceCode":"\tlogFile string,\n) error {\n\tif err := setLogLevel(enableDebug, levelName); err != nil {\n\t\treturn fmt.Errorf(\"failed to set log-level: %v\", err)\n\t}\n\n\tif err := setLogFormat(format); err != nil {\n\t\treturn fmt.Errorf(\"failed to set log format: %v\", err)\n\t}\n\n\tif len(logFile) > 0 {\n\t\t// This touch is not ideal - need to understand how to merge this logic with artifacts.PrepareEnv().\n\t\tif err := fsutil.Touch(logFile); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to set log output destination to %q, touch failed with: %v\", logFile, err)\n\t\t}\n\n\t\tf, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to set log output destination to %q: %w\", logFile, err)\n\t\t}\n\n\t\tlog.SetOutput(f)\n\t}\n\n\treturn nil\n}\n\nfunc setLogFormat(format string) error {\n\tswitch format {\n\tcase \"text\":\n\t\tlog.SetFormatter(&log.TextFormatter{DisableColors: true})\n\tcase \"json\":\n\t\tlog.SetFormatter(&log.JSONFormatter{})\n\tdefault:\n\t\treturn fmt.Errorf(\"unknown log-format %q\", format)\n\t}\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/sensor/logger.go#L18-L54","documentation":"After the touch succeeds, configureLogger opens the log file with os.OpenFile(O_WRONLY|O_CREATE|O_APPEND, 0644) to attach it as logrus output. If the OS refuses to open the file for writing, this wrapped error is returned and the sensor fails to start.","triggerScenarios":"The log file exists but is not writable by the sensor process (wrong owner/mode), the path is a directory, a symlink loop exists, or the fd limit (EMFILE) is reached.","commonSituations":"Log file pre-created by an init step with restrictive 0600 root ownership; SELinux/AppArmor denying write; log rotation tooling replaced the file with a directory or a read-only link.","solutions":["Fix ownership/mode of the log file (chmod 0644 or chown to the sensor's uid).","Ensure the logFile path is a regular writable file, not a directory or broken symlink.","Check 'too many open files' limits if the wrapped error is EMFILE and raise ulimit -n."],"exampleFix":"// before\n-rw------- root root /var/log/sensor/sensor.log\n// after\nchmod 0644 /var/log/sensor/sensor.log && chown sensor:sensor /var/log/sensor/sensor.log","handlingStrategy":"validation","validationCode":"func canAppend(path string) error {\n    fi, err := os.Stat(path)\n    if err != nil { return err }\n    if fi.IsDir() { return fmt.Errorf(\"%s is a directory\", path) }\n    f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0644)\n    if err != nil { return err }\n    return f.Close()\n}","typeGuard":"func isOpenDenied(err error) bool {\n    var pe *fs.PathError\n    return errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrPermission)\n}","tryCatchPattern":"if err := Run(ctx); err != nil && strings.Contains(err.Error(), \"failed to set log output destination\") {\n    fmt.Fprintf(os.Stderr, \"cannot open log file for append: %v\\n\", err)\n    os.Exit(1)\n}","preventionTips":["Set the log file mode/owner so the sensor uid can write (0644 + correct owner).","Avoid log rotation tooling that replaces the file with a directory or read-only link.","Raise the process fd limit if running with many open files.","Fall back to stdout logging if the file cannot be opened, rather than aborting."],"tags":["logging","file-open","permissions","filesystem"],"backgroundTag":"file-permission-denied","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}