Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

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 show

Fix it fast

Most common cause, 1 of 3

A 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.

Fix
<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>
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Log4j 2.25 and 2.26, Tomcat 9 to 11 from the Apache tarball, tomcat10 and tomcat11 packages on Ubuntu 24.04 and 26.04, systemd
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
grep -n "fileName" /path/to/log4j2.xml
systemctl show tomcat -p WorkingDirectory -p User -p ProtectSystem

Then find the line you see in this list and open its fix:

  1. 1A relative fileName meets a service that starts in /fileName says logs/connectivityserver.log, while the message complains about /logs right under the root
  2. 2The packaged Tomcat service may only write to its own foldersTomcat 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
  3. 3The parent folder is not writable by the service accountAn 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 /logs
  • java.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.

Check
sudo ls -l /proc/$(systemctl show tomcat -p MainPID --value)/cwd

Fix

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.

Fix
<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.

Verify
sudo systemctl restart tomcat
ls -l /opt/tomcat/logs/connectivityserver.log

Confirmed by freedesktop.org

Did this fix it?

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.

Check
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.

Fix
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 tomcat10

Verify

systemd lists the new path for the unit and the log file appears in it.

Verify
systemctl show tomcat10 -p ReadWritePaths
ls -l /opt/myapp/logs/

Confirmed by sources.debian.org

Did this fix it?

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.

Check
namei -l /opt/myapp/logs/connectivityserver.log
systemctl show tomcat -p User

Fix

Create the folder once with the service account as owner, so Log4j only has to open the file.

Fix
sudo install -d -o tomcat -g tomcat -m 750 /opt/myapp/logs

Verify

The service account can write there, and the next start logs without the error.

Verify
sudo -u tomcat touch /opt/myapp/logs/write-test && echo writable

Confirmed by github.com

Did this fix it?

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.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.