Longterm Server Logs With Kinsta

Have you ever attempted to review server logs only to find out it was too late? WordPress sites with Kinsta keep the last few days of server logs. That’s great for fixing an immediate issue, however, it’s not a good long-term solution for troubleshooting. There are paid services like Papertrail, which allow for long-term server log collection. For simple troubleshooting, this feels like overkill. The following is how I enable long-term server logs with Kinsta.

Enabling log term server logs over SSH

Start by SSHing into the WordPress site and create a new folder where we will store the log files.

mkdir ~/private/log-archive/

Next, create a new script ~/private/archive-logs.sh with the following.

cd ~
for file in $( ls logs/*gz ); do
	log_file=$( basename $file )
	if [ ! -f "private/log-archive/$log_file"  ]; then
		echo "Saving $log_file to ~/private/log-archive/"
		cp $file private/log-archive/
		echo "Skiping $log_file"

Grant the script execute permissions.

chmod +x ~/private/archive-logs.sh

Schedule the script to run a few times per day. Run crontab -e and add the following line to the end of your crontab file. Be sure to update the hard-coded path to match your site. You can find that out if you run pwd.

0 */3 * * * /www/sitename_123/private/archive-logs.sh

Downloading and viewing logs

After a few months, your log archive will look something like this.

To package up these files we can do the following:

cd ~/private/
zip -r log-archive.zip log-archive/
mv log-archive.zip ~/public
cd ~/public/
echo "$( wp option get home --skip-themes --skip-plugins )/log-archive.zip"

Click on the link or copy it and open it manually. Not all terminals support link clicking. After you have downloaded log-archive.zip, be sure to remove the zip file so it can’t be downloaded again by someone else.

rm ~/public/log-archive.zip

The easiest way I’ve found to view the files is with VS Code. If you have code loaded in your path then you can unzip the archive, extract the .gz files, and open the log files locally like so:

cd ~/Downloads
unzip log-archive.zip
cd log-archive/
gzip -d *.gz
code .

This will launch VS Code into a directory. From here we can manually look at each log file or search for all log files.

The real power of VS Code comes with complex searching capabilities. You can use regular expressions to find very particular patterns.

Finding 404 errors from user agent Apache.

Depending on your website, these logs can end up taking up a fair amount of disk space. After you uncover the technical issues you’re working with, it would be a good idea to turn off long-term collections.