Log - logrotate app

> Code - (Programming|Computer) Language > Code - Debugging (Bug)

1 - About

logrotate is a linux service that allows:

Each log file may be handled daily, weekly, monthly, or when it grows too large.

Advertising

3 - Command options

Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
  -l, --log=STRING          Log file
  --version                 Display version information

Help options:
  -?, --help                Show this help message
  --usage                   Display brief usage message

4 - State file

4.1 - Syntax

logrotate reads everything about the log files it should be handling from the series of configuration files specified on the command line.

sample logrotate configuration file

# global options
# logs are compressed after they are rotated.
compress

"/var/log/httpd/access.log" /var/log/httpd/error.log {
rotate 5
mail [email protected]
size 100k
# coul be weekly or monthly
sharedscripts
postrotate
   /usr/bin/killall -HUP httpd
endscript
}

where:

  • Deleted files property:
    • rotate 5 specify that the file will go through five rotations before being removed.
    • mail [email protected] - mail log file (uncompressed) to [email protected] before delete
    • compress, nocompress
    • create 0600 root root
  • Rotate trigger:
    • size 100k specifiy that log file is rotated whenever it grows over 100k in size
    • weekly define the rotation period (one by week)
    • monthly define a monthly period (one by month)
  • Script:
    • postrotate and endscript define a postrotate script code of block that will be run once once for each rotated log before the old version of the log has been compressed.
    • sharedscripts means that the postrotate script will only be run once (after the old logs have been compressed)

See logrotate for more syntax element

Advertising

4.1 - Comment

On a line basis where the first character is a #

4.2 - Default

Default state file.

/etc/logrotate.conf
# rotate log files weekly
weekly
 
# keep 4 weeks worth of backlogs
rotate 4
 
# create new (empty) log files after rotating old ones
create
 
# use date as a suffix of the rotated file
dateext
 
# uncomment this if you want your log files compressed
#compress
 
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
 
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}
 
/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

4.3 - Error

if errors occur for more than one file, the log files are not compressed.

5 - Management

5.1 - Creation

5.1.1 - Ansible

with the following Ansible task

- name: Create logrotate entry for /var/log/myapp.log
  template: src=templates/logrotate_myapp.j2 dest=/etc/logrotate.d/myapp owner=root group=root mode=0644
logrotate_myapp.j2
/var/log/myapp.log {
  rotate 7
  daily
  compress
  missingok
  notifempty
}
Advertising

6 - Documentation / Reference

code/debugging/logrotate.txt · Last modified: 2019/03/05 14:51 by gerardnico