caddyserver/caddy · error

unable to stat log file to see if we need to set permissions

Error message

unable to stat log file to see if we need to set permissions: %v

What it means

FileWriter.OpenWriter wraps file.Stat failing after opening/creating the log file: without the stat, an already-existing file's permissions cannot be corrected to the configured mode, so opening the writer aborts.

Solutions

  1. Check that the log file path is accessible and the process has permission to stat it.
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at modules/logging/filewriter.go:235 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of caddyserver/caddy@50e54ee279 (2026-08-15). Data as JSON: /api/errors/0c11017be2a097ef. Report an issue: GitHub.

Appendix: source

Thrown at modules/logging/filewriter.go:235

		if err := os.MkdirAll(dirPath, dm); err != nil {
			return nil, err
		}
	}

	// create/open the file
	file, err := os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, modeIfCreating)
	if err != nil {
		return nil, err
	}
	info, err := file.Stat()
	if roll {
		file.Close() // timberjack will reopen it on its own
	}

	// Ensure already existing files have the right mode, since OpenFile will not set the mode in such case.
	if configuredMode := os.FileMode(fw.Mode); configuredMode != 0 {
		if err != nil {
			return nil, fmt.Errorf("unable to stat log file to see if we need to set permissions: %v", err)
		}
		// only chmod if the configured mode is different
		if info.Mode()&os.ModePerm != configuredMode&os.ModePerm {
			if err = os.Chmod(fw.Filename, configuredMode); err != nil {
				return nil, err
			}
		}
	}

	// if not rolling, then the plain file handle is all we need
	if !roll {
		return file, nil
	}

	// otherwise, return a rolling log
	if fw.RollSizeMB == 0 {
		fw.RollSizeMB = 100
	}

View on GitHub (pinned to 50e54ee279)