[BUG] Configured FileMode is not preserved after rotation when umask masks bits
Describe the bug
When Logger.FileMode is configured, the configured mode is not preserved after log rotation. The new active log file is created with permissions affected by the process umask rather than the configured FileMode. In practice this means the initial log file may appear to have the expected mode but after the first rotation the newly created active file can lose permissions such as group write.
Environment
- timberjack version: v1.3.9
- Go version: go1.26.1
- OS/Arch: linux/amd64
- Runtime: standalone local process
- Command used to repro:
go run .
Steps to reproduce
- Set process
umaskto0022 - Configure a
timberjack.LoggerwithFileMode: 0o660 - Write enough data to trigger rotation
- Check the mode of the new active log file after rotation
Minimal code (required)
package main
import (
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/DeRuina/timberjack"
)
func main() {
oldUmask := syscall.Umask(0o022)
defer syscall.Umask(oldUmask)
dir, err := os.MkdirTemp("", "timberjack-mode")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
name := filepath.Join(dir, "app.log")
l := &timberjack.Logger{
Filename: name,
MaxSize: 1,
FileMode: 0o660,
}
defer l.Close()
chunk := make([]byte, 1024*1024)
if _, err := l.Write(chunk[:1024*1024-1000]); err != nil {
panic(err)
}
if _, err := l.Write(chunk[:2000]); err != nil {
panic(err)
}
st, err := os.Stat(name)
if err != nil {
panic(err)
}
fmt.Printf("active log mode: %04o\n", st.Mode().Perm())
}
Expected result: active log mode: 0660
Actual result: active log mode: 0640
Additional context
The relevant path appears to be openNew() in timberjack.go. It creates the new active file with os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, finalMode) but does not re-apply FileMode afterward so the process umask still strips bits. Would you be open to making FileMode authoritative after rotation as well so behavior is consistent before and after rotation?
- https://github.com/caddyserver/caddy/issues/7613
thank you @steadytao I will look into it
Issue was fixed and a new release is published. Thanks @steadytao. closing