# Use logrotate to manage the logfile # # usage: ynh_use_logrotate [logfile] # | arg: logfile - absolute path of logfile # # If no argument provided, a standard directory will be use. /var/log/${app} # You can provide a path with the directory only or with the logfile. # /parentdir/logdir/ # /parentdir/logdir/logfile.log # # It's possible to use this helper several times, each config will added to same logrotate config file. ynh_use_logrotate () { if [ "$#" -gt 0 ]; then if [ "$(echo ${1##*.})" == "log" ]; then # Keep only the extension to check if it's a logfile logfile=$1 # In this case, focus logrotate on the logfile else logfile=$1/.log # Else, uses the directory and all logfile into it. fi else logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log fi cat > ./${app}-logrotate << EOF # Build a config file for logrotate $logfile { # Rotate if the logfile exceeds 100Mo size 100M # Keep 12 old log maximum rotate 12 # Compress the logs with gzip compress # Compress the log at the next cycle. So keep always 2 non compressed logs delaycompress # Copy and truncate the log to allow to continue write on it. Instead of move the log. copytruncate # Do not do an error if the log is missing missingok # Not rotate if the log is empty notifempty # Keep old logs in the same dir noolddir } EOF sudo mkdir -p $(dirname "$logfile") # Create the log directory, if not exist cat ${app}-logrotate | sudo tee -a /etc/logrotate.d/$app > /dev/null # Append this config to the others for this app. If a config file already exist } # Remove the app's logrotate config. # # usage: ynh_remove_logrotate ynh_remove_logrotate () { if [ -e "/etc/logrotate.d/$app" ]; then sudo rm "/etc/logrotate.d/$app" fi }