Log4j 2 cannot create its log file when Tomcat runs as a systemd service
Your error
ERROR Unable to create file logs/connectivityserver.log java.io.IOException: Could not create directory /logs1 more line it can showFix it fast
Most common cause, 1 of 3A relative fileName meets a service that starts in /
Make the path absolute. catalina.sh hands catalina.base to the JVM as a system property, and Log4j's sys lookup reads system properties, so the file lands in Tomcat's logs folder wherever the service starts.
<RollingFile name="app"
fileName="${sys:catalina.base}/logs/connectivityserver.log"
filePattern="${sys:catalina.base}/logs/connectivityserver.%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d %p %c - %m%n"/>
<TimeBasedTriggeringPolicy/>
</RollingFile>Short answer
ERROR Unable to create file logs/app.log java.io.IOException: Could not create directory /logs means Log4j resolved a relative fileName against the JVM's working directory, which for a systemd service is /. Give the appender an absolute path such as ${sys:catalina.base}/logs/app.log. Under the Debian and Ubuntu Tomcat packages, write inside Tomcat's own logs folder or widen the sandbox.
Which cause do you have?
Compare the directory in the message with the fileName in log4j2.xml, then ask systemd where the service starts and as which user. A folder directly under / means a relative path; any other folder means a sandbox or an owner problem.
grep -n "fileName" /path/to/log4j2.xml
systemctl show tomcat -p WorkingDirectory -p User -p ProtectSystemThen find the line you see in this list and open its fix:
- 1A relative fileName meets a service that starts in /
fileName says logs/connectivityserver.log, while the message complains about /logs right under the root - 2The packaged Tomcat service may only write to its own folders
Tomcat came from apt (tomcat10 or tomcat11) and the folder in the message is absolute and outside /var/log/tomcat10 (or /var/log/tomcat11), for example /opt/myapp/logs - 3The parent folder is not writable by the service account
An absolute path such as /opt/myapp/logs, no sandbox in the unit, and User=tomcat (or another non-root user)
What does the error look like?
ERROR Unable to create file logs/connectivityserver.log java.io.IOException: Could not create directory /logsjava.io.IOException: Could not create directory /opt/myapp/logs
Cause 1 of 3
A relative fileName meets a service that starts in /
You seefileName says logs/connectivityserver.log, while the message complains about /logs right under the root
Check
Log4j makes the path absolute before it creates the parent folders, and Java resolves a relative path against the JVM's current directory. A systemd system service without WorkingDirectory= starts in the root directory, so logs/app.log turns into /logs/app.log, which the tomcat account may not create. Tarball installs with a hand-written unit file hit this, since catalina.sh never changes directory.
sudo ls -l /proc/$(systemctl show tomcat -p MainPID --value)/cwdFix
Make the path absolute. catalina.sh hands catalina.base to the JVM as a system property, and Log4j's sys lookup reads system properties, so the file lands in Tomcat's logs folder wherever the service starts.
<RollingFile name="app"
fileName="${sys:catalina.base}/logs/connectivityserver.log"
filePattern="${sys:catalina.base}/logs/connectivityserver.%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d %p %c - %m%n"/>
<TimeBasedTriggeringPolicy/>
</RollingFile>Verify
After a restart, Log4j prints no status error and the file exists under CATALINA_BASE.
sudo systemctl restart tomcat
ls -l /opt/tomcat/logs/connectivityserver.logConfirmed by freedesktop.org
Cause 2 of 3
The packaged Tomcat service may only write to its own folders
You seeTomcat came from apt (tomcat10 or tomcat11) and the folder in the message is absolute and outside /var/log/tomcat10 (or /var/log/tomcat11), for example /opt/myapp/logs
Check
The Debian and Ubuntu packages start Tomcat with ProtectSystem=strict, which makes the whole file system read-only for the service, and open only conf/Catalina, logs, webapps and work again for writing. Folders elsewhere cannot be created whatever their permissions are. A relative logs/ path does work here: the start script changes to /var/lib/tomcat10 first, and its logs entry links to /var/log/tomcat10.
systemctl cat tomcat10 | grep -E 'ProtectSystem|ReadWritePaths'Fix
Log under ${sys:catalina.base}/logs, or allow the extra folder with a drop-in file as the package README describes. Create the folder for the tomcat account first.
sudo install -d -o tomcat -g tomcat /opt/myapp/logs
sudo mkdir -p /etc/systemd/system/tomcat10.service.d
printf '[Service]\nReadWritePaths=/opt/myapp/logs/\n' | sudo tee /etc/systemd/system/tomcat10.service.d/override.conf
sudo systemctl daemon-reload
sudo systemctl restart tomcat10Verify
systemd lists the new path for the unit and the log file appears in it.
systemctl show tomcat10 -p ReadWritePaths
ls -l /opt/myapp/logs/Confirmed by sources.debian.org
Cause 3 of 3
The parent folder is not writable by the service account
You seeAn absolute path such as /opt/myapp/logs, no sandbox in the unit, and User=tomcat (or another non-root user)
Check
Before opening the file, Log4j creates each missing folder on the path and reports any failure as Could not create directory. If /opt/myapp belongs to root with mode 755, a service running as tomcat cannot add logs inside it.
namei -l /opt/myapp/logs/connectivityserver.log
systemctl show tomcat -p UserFix
Create the folder once with the service account as owner, so Log4j only has to open the file.
sudo install -d -o tomcat -g tomcat -m 750 /opt/myapp/logsVerify
The service account can write there, and the next start logs without the error.
sudo -u tomcat touch /opt/myapp/logs/write-test && echo writableConfirmed by github.com
How do I stop it coming back?
- Never ship a relative fileName in log4j2.xml; build every path from ${sys:catalina.base} or an explicit property the service sets.
- Set WorkingDirectory= in any unit file you write for Tomcat or a plain java -jar service, so relative paths stop depending on luck.
- Create log folders in the install script with the service account as owner, and add them to ReadWritePaths in the same change when the unit is sandboxed.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.