grafana/k6 · error

failed to open logfile %s: %w

Error message

failed to open logfile %s: %w

What it means

Raised in openFile when h.fs.OpenFile fails to open/create the log file with write+append+create flags (permissions, read-only fs, path is a directory, etc.). The underlying OS error is wrapped with %w. The at-fault input is the resolved log file path.

Source

Thrown at internal/log/file.go:104

// openFile opens logfile and initializes writers.
func (h *fileHook) openFile(getCwd func() (string, error)) error {
	path := h.path
	if !filepath.IsAbs(path) {
		cwd, err := getCwd()
		if err != nil {
			return fmt.Errorf("'%s' is a relative path but could not determine CWD: %w", path, err)
		}
		path = filepath.Join(cwd, path)
	}

	if _, err := h.fs.Stat(filepath.Dir(path)); errors.Is(err, fs.ErrNotExist) {
		return fmt.Errorf("provided directory '%s' does not exist", filepath.Dir(path))
	}

	file, err := h.fs.OpenFile(path, syscall.O_WRONLY|syscall.O_APPEND|syscall.O_CREAT, 0o600)
	if err != nil {
		return fmt.Errorf("failed to open logfile %s: %w", path, err)
	}

	h.w = file
	h.bw = bufio.NewWriter(file)

	return nil
}

// Listen waits for log lines to flush.
func (h *fileHook) Listen(ctx context.Context) {
	ticker := time.NewTicker(fileHookFlushInterval)
	defer ticker.Stop()

	for {
		select {
		case entry := <-h.loglines:
			if _, err := h.bw.Write(entry); err != nil {
				h.fallbackLogger.WithError(err).Error("failed to write a log message to a logfile")

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Check write permissions on the file/directory and that the path is not an existing directory
  2. Ensure the filesystem is writable (not read-only container fs or disk-full)
  3. Run with a user that has access to the path, or choose a different log location
  4. Read the wrapped OS error for the exact cause
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at internal/log/file.go:104 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/6f44de904fea4201. Report an issue: GitHub.