Select Page

A better way to archive old Alfresco logs on Linux- Part 3 of 3

In the first article of this series, we shared the advantages of using an external approach to automatically deleting aged log files in Alfresco. The typical approach, changing the logging settings, can be problematic because it is more difficult to deploy and manage in complex environments. In the second article, we discussed how to implement a better approach in Windows. This article will review the same technique for Linux.

This article covers rollover (log rotation) and rolloff (log deletion after age) of Alfresco related logs in Linux. This example is based on Alfresco ACS 6.2, but it also works well with previous versions.

Steps to Roll Off Alfresco Logs

It is easy to configure roll off one time for current and future deployments. This is assuming that the location of the logs remains the same, with one configuration file and one script, and without mucking around with logging configurations. We can use two Linux mechanisms, depending on the kind of log:

  • Rolled logs.

    Most of the logs are rolled over every day. For example, alfresco.log is rolled daily to a file with the date at the end, like alfresco.log.2021-01-01. We can roll off those files using a script under /etc/cron.daily. We will explain the details in a little bit.

  • Non-rolled logs.

    The file catalina.out will continue growing forever, without ever being rolled over. For this kind of log, logrotate is our friend. Configurations under /etc/logrotate.d are run once daily by the logrotate script under /etc/cron.daily.

Step 1:  Configure logrotate

First, let’s roll off non-rolled log catalina.out. Create a configuration file /etc/logrotate.d/alfresco with the following contents:

/opt/alfresco/tomcat/logs/catalina.out {
 copytruncate daily
 maxage 14
 missingok
}

Our file configures the following:

  • The full path to catalina.out
  • copytruncate performs rollover by copying the contents of catalina.out to a new dated file then truncating the catalina.out file
  • daily rolls the log daily (surprise!)
  • maxage specifies the number days before rolled over logs are deleted
  • missingok suppresses error messages when the log file doesn’t exist

There are options to compress, mail to, run a script, post, create, and more. See man logrotate for parameters and more details.

Step 2:  Create a script to remove aged logs

Scripts under the /etc/cron.daily directory run scripts once daily (another surprise!). Putting our script here works well to remove those logs that have already rolled over. Logrotate isn’t friendly with rolloff of files that it doesn’t rollover.

Create a file /etc/cron.daily/alfresco with the content below to rolloff logs older than 14 days. 

Remember to set ALFRESCO_LOGS, ALFRESCO_LOG_DIR and SHARE_LOG_DIR depending on the location of your log files.

#!/bin/sh

ALFRESCO_LOGS=/opt/alfresco/tomcat/logs

# This will work for ACS 6.2 
# Other ACS versions may put the alfresco and share logs at the Alfresco home directory
ALFRESCO_LOG_DIR=/opt/alfresco/tomcat/logs
SHARE_LOG_DIR=/opt/alfresco/tomcat/logs

find ${ALFRESCO_LOGS}/. -maxdepth 1 -type f -regextype posix-egrep -regex ".*host-manager.[0-9]{4}-[0-9]{2}-[0-9]{2}.log" -mtime +14 -delete
find ${ALFRESCO_LOGS}/. -maxdepth 1 -type f -regextype posix-egrep -regex ".*localhost.[0-9]{4}-[0-9]{2}-[0-9]{2}.log" -mtime +14 -delete
find ${ALFRESCO_LOGS}/. -maxdepth 1 -type f -regextype posix-egrep -regex ".*manager.[0-9]{4}-[0-9]{2}-[0-9]{2}.log" -mtime +14 -delete
find ${ALFRESCO_LOG_DIR}/. -maxdepth 1 -type f -regextype posix-egrep -regex ".*alfresco.log.[0-9]{4}-[0-9]{2}-[0-9]{2}" -mtime +14 -delete
find ${ALFRESCO_SHARE_DIR}/. -maxdepth 1 -type f -regextype posix-egrep -regex ".*share.log.[0-9]{4}-[0-9]{2}-[0-9]{2}" -mtime +14 -delete
exit 0

Note that we supply the following arguments to the unix find command:

  • The directory. Update the ALFRESCO_LOGS, ALFRESCO_LOG_DIR and SHARE_LOG_DIR to match your installation
  • maxdepth, (maximum folder depth) to 1, this will not descend into child folders
  • type only files will be returned (-type f), with no directories
  • regextype posix-egrep, so the filenames will match the -regex pattern specified
  • mtime +14 only looks at files older than 14 days
  • delete deletes the files found

A Tip About the Location of the Logs

If you are annoyed by all those alfresco.log and share.log files getting dumped to tomcat/bin/, or the Alfresco home directory, this last tip is for you! If you choose to install ACS 6.x using the distribution zip, those log files will be sent to the working directory when you start Alfresco.

Sending alfresco.log and share.log to tomcat/logs/ is easy. Change the service unit file, or startup script, to set the working directory before starting. For example, if you use a startup script, you could add a line with the cd command before starting tomcat, as shown below ($CATALINA_HOME is the tomcat directory):

cd $CATALINA_HOME/logs
$CATALINA_HOME/bin/startup.sh

Conclusions

It is much easier to manage the numerous logs produced by an Alfresco environment using external tools. These articles have shown you a few scripts and utilities that will get the job done.

Our next iteration of articles will be about managing logs in containerized environments. On top of orchestrating the containers, you have to learn how to handle the logs that they produce. We will discuss a strategy that concentrates them in one place. This allows you to analyze them easily, and then roll them off when they are no longer needed.

We hope that you found this article helpful. If you are dealing with something a bit more complicated, please consider using our support and consulting services. Thanks!

 

Pin It on Pinterest

Sharing is caring

Share this post with your friends!