Skip to content

Commit b3b99d9

Browse files
committed
make log file on linux auto. allow dir instead of file for log file
1 parent 1b83050 commit b3b99d9

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

init/systemd/after-install.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ if [ "${OS}" = "Linux" ]; then
1111
useradd --system --user-group --no-create-home --home-dir /tmp --shell /bin/false unpackerr
1212
elif [ "${OS}" = "OpenBSD" ]; then
1313
id unpackerr >/dev/null 2>&1 || \
14-
useradd -g =uid -d /tmp -s /bin/false unpackerr
14+
useradd -g =uid -d /tmp -s /bin/false unpackerr
1515
elif [ "${OS}" = "FreeBSD" ]; then
1616
id unpackerr >/dev/null 2>&1 || \
1717
pw useradd unpackerr -d /tmp -w no -s /bin/false
1818
else
1919
echo "Unknown OS: ${OS}, please add system user unpackerr manually."
2020
fi
2121

22+
if [ ! -d "/var/log/unpackerr" ]; then
23+
mkdir /var/log/unpackerr
24+
chown unpackerr: /var/log/unapckerr
25+
chmod 0755 /var/log/unpackerr
26+
fi
27+
2228
if [ -x "/bin/systemctl" ]; then
2329
# Reload and restart - this starts the application as user nobody.
2430
/bin/systemctl daemon-reload

init/systemd/unpackerr.service

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ After=network-online.target
1212
ExecStart=/usr/bin/unpackerr $DAEMON_OPTS
1313
EnvironmentFile=-/etc/default/unpackerr
1414
EnvironmentFile=-/etc/sysconfig/unpackerr
15+
Environment=UN_LOG_FILE=/var/log/unpackerr/unpackerr.log
16+
Environment=UN_WEBSERVER_LOG_FILE=/var/log/unpackerr/http.log
1517
Restart=always
1618
RestartSec=10
1719
SyslogIdentifier=unpackerr
1820
Type=simple
1921
WorkingDirectory=/tmp
2022

2123
# These should be set correctly for your environment.
24+
# If you change User, fix ownership on /var/log/unpackerr too.
2225
UMask=0002
2326
User=unpackerr
2427
Group=unpackerr

pkg/unpackerr/logs.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"log"
77
"os"
8+
"path/filepath"
89
"runtime"
910
"strconv"
1011
"time"
@@ -138,10 +139,7 @@ func (u *Unpackerr) setupLogging() {
138139
u.Logger.Error.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
139140
}
140141

141-
if logFile, err := homedir.Expand(u.Config.LogFile); err == nil {
142-
u.Config.LogFile = logFile
143-
}
144-
142+
u.Config.LogFile = getLogFilePath(u.Config.LogFile, "unpackerr.log")
145143
fileMode, _ := strconv.ParseUint(u.LogFileMode, bits8, base32)
146144
rotate := &rotatorr.Config{
147145
Filepath: u.Config.LogFile, // log file name.
@@ -175,6 +173,21 @@ func (u *Unpackerr) setupLogging() {
175173
}
176174
}
177175

176+
// getLogFilePath takes in a path and a base name. In case the path is a directory, they are joined.
177+
func getLogFilePath(logFile, base string) string {
178+
output := logFile
179+
180+
if expanded, err := homedir.Expand(logFile); err == nil {
181+
output = expanded
182+
}
183+
184+
if stat, err := os.Stat(output); err != nil && stat.IsDir() {
185+
output = filepath.Join(output, base)
186+
}
187+
188+
return output
189+
}
190+
178191
func (u *Unpackerr) updateLogOutput(writer io.Writer, errors io.Writer) {
179192
if u.Webserver != nil && u.Webserver.LogFile != "" {
180193
u.setupHTTPLogging()
@@ -193,9 +206,7 @@ func (u *Unpackerr) updateLogOutput(writer io.Writer, errors io.Writer) {
193206
}
194207

195208
func (u *Unpackerr) setupHTTPLogging() {
196-
if logFile, err := homedir.Expand(u.Webserver.LogFile); err == nil {
197-
u.Webserver.LogFile = logFile
198-
}
209+
u.Webserver.LogFile = getLogFilePath(u.Webserver.LogFile, "http.log")
199210

200211
rotate := &rotatorr.Config{
201212
Filepath: u.Webserver.LogFile, // log file name.

0 commit comments

Comments
 (0)