Managing Log Files with Logrotate on Debian10

Managing Log Files with Logrotate on Debian10

- 5 mins

What is Logrotate?

Prerequisites

Getting started with Logrotate

logrotate --version

logrotate

Configuring log rotation for a custom application

    #!/bin/bash

logfile="/var/log/logify/log_records.log"

# Function to generate a random log record

generate_log_record() {
local loglevel=("INFO" "WARNING" "ERROR")
local services=("web" "database" "app" "network")
local timestamps=$(date +"%Y-%m-%d %H:%M:%S")
    local random_level=${loglevel[$RANDOM % ${#loglevel[@]}]}
    local random_service=${services[$RANDOM % ${#services[@]}]}
local message="This is a sample log record for ${random_service} service."

    echo "${timestamps} [${random_level}] ${message}"

}

# Main loop to write log records every second

while true; do
log_record=$(generate_log_record)
    echo "${log_record}" >> "${logfile}"
sleep 1
done
chmod +x logify.sh

logrotate2

mkdir /var/log/logify
./logify.sh &
cat /var/log/logify/log_records.log

logrotate3

Creating a standard Logrotate configuration

    vi /etc/logrotate.d/logify
    
/var/log/logify/\*.log
{
hourly
missingok
rotate 7
compress
notifempty
}

logrotate4

logrotate /etc/logrotate.conf --debug
logrotate -f /etc/logrotate.d/logify
ls /var/log/logify/

logrotate6

cat /var/lib/logrotate/status | grep 'logify'

logrotate7



:metal: :metal: :metal: :metal: :metal: :metal: :metal: :metal:


Guneycan Sanli.


Guneycan Sanli

Guneycan Sanli

A person who like learning, music, travelling and sports.

comments powered by Disqus