Skip to content

Commit 150ac47

Browse files
authored
Merge pull request #579 from Unpackerr/dn2_stdout_when_err
Logs fallback to stdout
2 parents 145b685 + 246e0a8 commit 150ac47

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

pkg/unpackerr/logs.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,30 @@ func (u *Unpackerr) setupLogging() {
153153
}
154154

155155
if u.Config.LogFile != "" {
156-
u.rotatorr = rotatorr.NewMust(rotate)
156+
var err error
157+
158+
u.rotatorr, err = rotatorr.New(rotate)
159+
if err != nil {
160+
// Fall back to stdout so we don't hammer the filesystem with failed open attempts.
161+
u.rotatorr = nil
162+
_, _ = os.Stdout.WriteString("[Unpackerr] Log file unavailable (check path and permissions!!), " +
163+
"logging to stdout only: " + err.Error() + "\n")
164+
}
157165
}
158166

159167
stderr := os.Stdout
160168
if u.ErrorStdErr {
161169
stderr = os.Stderr
162170
}
163171

172+
useLogFile := u.Config.LogFile != "" && u.rotatorr != nil
173+
164174
switch { // only use MultiWriter if we have > 1 writer.
165-
case !u.Config.Quiet && u.Config.LogFile != "":
175+
case !u.Config.Quiet && useLogFile:
166176
u.updateLogOutput(io.MultiWriter(u.rotatorr, os.Stdout), io.MultiWriter(u.rotatorr, stderr))
167-
case !u.Config.Quiet && u.Config.LogFile == "":
177+
case !u.Config.Quiet && !useLogFile:
168178
u.updateLogOutput(os.Stdout, stderr)
169-
case u.Config.LogFile == "":
179+
case !useLogFile:
170180
u.updateLogOutput(io.Discard, io.Discard) // default is "nothing"
171181
default:
172182
u.updateLogOutput(u.rotatorr, u.rotatorr)

0 commit comments

Comments
 (0)