How to logrotate with systemd?

I'm running a node app using systemd with a unit file. The apps log can be viewed using journalctl -u example.com but the log gets cut at some point, erasing the history (which I need for debugging).

How do I configure log rotation with systemd so that my app logs gets stored in example.com.log.1, example.com.log.2 etc. so I can query the history of my app?

This is my unit file:

ExecStart=/usr/bin/node keystone.js
#Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=example.com
User=user
Group=user
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Solution 1:

Increasing Storage

If you are storing to disk you can increase the amount of space used by changing SystemMaxUse and/or SystemKeepFree. Having more space would allow more entries to be stored.

By default the maximum use limit (SystemMaxUse) is 10% of the filesystem, and the minimum free space (SystemKeepFree) value is 15% - though they are both capped at 4G.

Journal Rotation

The journals should be rotated automatically when they reach the SystemMaxFileSize value, and the number of journals is controlled by SystemMaxFiles. If you prefer time based rotation you can set a MaxFileSec to set the maximum time entries are stored in a single journal.

Note on Storage Location/Type

With journald you can choose to store the journal entries either in memory (Storage=volatile) or on disk (Storage=persistent). The above assumes you are storing journals on disk - if this is not the case "System" will need to be replaced by "Runtime" in the above variables.

Further details are available in man journald.conf.

Alternative - Passing on the journal entries

If you are already familiar with the traditional SYSLOG daemon you can pass the journal events on by setting the ForwardToSyslog option (which forwards the messages immediately) or have the syslog daemon behave like a journal client and fetch the logs itself.