Logrotate

luna·2022년 12월 14일

설치

$ sudo apt install logrotate

vi /etc/logrotate.conf

## 로그 파일을 순환시킬 기간에 대한 설정 (daily, weekly, monthly, yearly)
# see "man logrotate" for details
# rotate log files weekly
weekly

# use the adm group by default, since this is the owning group
# of /var/log/syslog.
su root adm

## 순환된 로그 파일을 보관할 기간에 대한 설정 (위에서 설정한 기간 단위 설정)
# keep 4 weeks worth of backlogs
rotate 4

## 로그  파일 순환 후 새로운 로그 파일 생성
# create new (empty) log files after rotating old ones
create

## 순환된 파일의 파일명 변경 옵션(YYYYMMDD)
# use date as a suffix of the rotated file
#dateext

## 순환된 파일을 압축하여 보관하는 옵션
# uncomment this if you want your log files compressed
#compress

## 로그 순환에 대한 추가 설정 파일이 저장된 디렉토리
# packages drop log rotation information into this directory
include /etc/logrotate.d

# system-specific logs may be also be configured here.

데몬 기본 정보

  • 데몬 위치: /usr/sbin/logrotate
  • 데몬 설정 파일 경로: /etc/logrotate.conf
  • rotate 기능을 활용하고 싶은 application의 rotate 설정 파일 경로: /etc/logrotate.d/
  • 데몬에서 관리되는 log file에 대한 rotate 상태 기록 파일: /var/lib/logrotate/logrotate.status

Application log의 rotate 설정

/etc/logrotate.d/ 경로에 testrotate 파일 생성

/logs/*.log {
rotate 10000
create
daily
missingok
dateext
dateformat -%Y-%m-%d_%H
maxage 14
maxsize 10M
}
  • rotate 10000: 로그 파일 개수를 10000개로 유지
    • 10000 개가 넘어가면 오래된 파일부터 삭제
  • create: 원본 파일이 rotate 되고, 원본 파일 명으로 신규 파일이 생성
    • 비활성화 nocreate, 복사본을 만들려면 copy, copy 옵션을 사용할 경우 create 옵션이 적용되지 않음
  • daily: 일 단위로 rotate
    • weeky, monthly, houly 등 원하는 rotate 기간에 따라 설정
  • missingok: 로그 파일이 없는 경우에도 에러 없이 다음으로 넘어감
    • nomissingok가 default, 로그 없을 시 error 발생
  • dateext: test.log.1 test.log.2처럼 숫자를 붙이는 대신 지정한(dateformat) 날짜 형태의 값으로 확장자를 생성
  • dateformat: dateext 옵션과 함께 사용하며, 로그 파일이 rotate될 때, 어떤 형태로 확장자를 생성할 지 설정
    • test.log → test.log-2022-10-12_00
  • maxage 14: 14일 이상이 지난 로그 파일을 삭제
  • maxsize 10M: 10M 이상이 된 로그 파일을 설정된 기간 조건과 관계 없이 rotate
  • compress: gzip으로 압축
  • nocompress: 압축하지 않음
  • size 용량: 지정된 용량이 되면 rotate
  • sharedscripts: postrotate ~ endscript 안에 script 가 동작할 때 각각의 로그 마다 실행되는 것이 아니라 1번만 실행되도록 하는 옵션
  • notifempty: 로그 파일이 비어있으면 로테이트 하지 않음
  • create 640 nginx adm: 로그 파일 새로 생성 시, 파일 권한을 640으로 생성, 소유자/그룹은 nginx
  • postrotate: 로테이트 작업이 끝난 후, 실행할 스크립트 입력

vi /etc/logrotate.d/nginx

/var/log/nginx/*.log {
        daily
        rotate 1
        size 1M
        missingok
        compress
        delaycompress
        notifempty
        dateext
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}
/var/log/nginx/*.log {
        ##일 단위로 rotate 
        daily
        ## 로그 파일 개수를 1개로 유지
        rotate 1
        ## 지정된 용량이 되면 rotate (1M)
        size 1M
        ## 로그 파일이 없는 경우에도 에러 없이 다음으로 넘어감
        missingok
        ## gzip으로 압축
        compress
        ## 최근파일 외 전부 압축
        delaycompress
        ## 로그 파일이 비어있으면 rotate 하지 않음 
        notifempty
        ## rotate된 로그파일명에 1,2.. 등 numbering이 아닌 날짜를 붙여서 보관
        dateext
        ## 새로 로그 파일 생성시 640 모드로 생성하고 소유자, 그룹은 nginx
        create 640 nginx adm
        ## 로그파일이 여러개 있어도 스크립트를 공유하여 prerotate, postrotate 스크립트를 한번만 실행
        sharedscripts
        ## 실행 후 스트립트 파일 실행
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

Test

# 강제실행
logrotate -f /etc/logrotate.d/nginx

# 디버깅
logrotate -d /etc/logrotate.d/nginx

파일 초기화

api/9c6fdb8c-172e-47f4-b8da-cfcb43eba42a:/var/vcap/sys/log/cloud_controller_ng# cat /dev/null > post-start.stderr.log
  • /dev/null: 용량이 0인 파일

ta inception test

  • /etc/logrotate.d/vcap 경로 파일 설정
# Generated by bosh-agent

/var/vcap/data/sys/log/*.log /var/vcap/data/sys/log/.*.log /var/vcap/data/sys/log/*/*.log /var/vcap/data/sys/log/*/.*.log /var/vcap/data/sys/log/*/*/*.log /var/vcap/data/sys/log/*/*/.*.log {
  daily
  missingok
  rotate 5
  compress
  copytruncate
  size=5M
  dateext
}
  • 파일설정에 dateext 설정
  • logrotate -f /etc/logrotate.d/* 명령어로 전체 강제 실행
    • cloud_controller_ng.log 만 실행하는 경로 못찾음
    • 날짜로 설정하니 하나 이상은 skip 에러
error: skipping "/var/log/alternatives.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
error: destination /var/vcap/data/sys/log/bosh-dns/bosh_dns_health.stdout.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/bosh-dns/bosh_dns.stdout.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/cloud_controller_ng/nginx-access.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/cloud_controller_ng/nginx-error.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/loggregator_agent/loggregator_agent.stderr.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/metrics-discovery-registrar/bpm.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/metrics-discovery-registrar/metrics-discovery-registrar.stderr.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/route_registrar/bpm.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/route_registrar/route_registrar.stderr.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/route_registrar/route_registrar.stdout.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/routing-api/bpm.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/routing-api/routing-api.stderr.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/routing-api/routing-api.stdout.log-20221026.gz already exists, skipping rotation
error: destination /var/vcap/data/sys/log/cloud_controller_ng/drain/drain.log-20221026.gz already exists, skipping rotation

ta inception mysql garbd 작업

/var/vcap/data/sys/log/garbd/*.log {
    daily
    rotate 3
    size=1M
    missingok
    compress
    dateext
    dateformat -%Y-%m-%d_%H_%M
    copytruncate
}
arbitrator/aaf70783-b131-4514-b4a1-33cad7f2bed5:/var/vcap/data/sys/log/garbd# logrotate -f /etc/logrotate.d/vcap 
arbitrator/aaf70783-b131-4514-b4a1-33cad7f2bed5:/var/vcap/data/sys/log/garbd# ll
total 48
drwxr-xr-x 2 root root 4096 Oct 27 16:26 ./
drwxr-x--- 9 root vcap 4096 Sep 25 03:19 ../
-rw-r--r-- 1 root root  540 Oct 27 16:26 garbd.stderr.log
-rw-r--r-- 1 root root 1267 Oct 27 16:23 garbd.stderr.log-2022-10-27_16_23.gz
-rw-r--r-- 1 root root   20 Oct 27 16:23 garbd.stderr.log-2022-10-27_16_24.gz
-rw-r--r-- 1 root root 1467 Oct 27 16:25 garbd.stderr.log-2022-10-27_16_26.gz

~# logrotate -d /etc/logrotate.d/pxc-mysql

rotating pattern: /var/vcap/sys/log/pxc-mysql/mysql_general.log  104857600 bytes (3 rotations)
empty log files are rotated, old logs are removed
considering log /var/vcap/sys/log/pxc-mysql/mysql_general.log
Creating new state
  Now: 2022-10-28 06:25
  Last rotated at 2022-10-28 06:00
  log needs rotating
rotating log /var/vcap/sys/log/pxc-mysql/mysql_general.log, log->rotateCount is 3
Converted ' -%Y-%m-%d' -> '-%Y-%m-%d'
dateext suffix '-2022-10-28'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'
glob finding old rotated logs failed
copying /var/vcap/sys/log/pxc-mysql/mysql_general.log to /var/vcap/sys/log/pxc-mysql/mysql_general.log-2022-10-28
truncating /var/vcap/sys/log/pxc-mysql/mysql_general.log
compressing log with: /bin/gzip
database/eb411a1e-259c-4da0-83c5-916974333df6:/var/vcap/sys/log/pxc-mysql# logrotate -f /etc/logrotate.d/pxc-mysql
database/eb411a1e-259c-4da0-83c5-916974333df6:/var/vcap/sys/log/pxc-mysql# ll
total 44104
drwxrwx---  2 vcap vcap     4096 Oct 28 06:30 ./
drwxr-x--- 22 root vcap     4096 Jan 25  2022 ../
-rw-------  1 vcap vcap     2319 Jan 25  2022 bpm.log
-rw-r--r--  1 vcap vcap        0 Jan 25  2022 galera-init.log
-rw-------  1 vcap vcap        0 Jan 25  2022 galera-init.stderr.log
-rw-------  1 vcap vcap    12641 Jan 25  2022 galera-init.stdout.log
-rw-r-----  1 vcap vcap   305978 Oct 28 06:28 mysql.err.log
-rw-r-----  1 vcap vcap    91698 Oct 28 06:30 mysql_general.log
-rw-r-----  1 vcap vcap 44574384 Oct 28 06:25 mysql_general.log-2022-10-28.gz
-rw-r--r--  1 vcap vcap   141148 Sep  5 06:27 mysql_slow_query.log
-rw-r-----  1 vcap vcap        0 Jan 25  2022 pre-start.stderr.log
-rw-r-----  1 vcap vcap     1832 Jan 25  2022 pre-start.stdout.log

설정 변경

crontab 설정

0개의 댓글