본문 바로가기

DevOps/[Linux]

[Log 관리] Logrotate 를 활용한 로그파일 관리(feat. Nginx)

반응형

INTRO


 

Logrotate는 Linux 기반의 서버 환경에 기본적으로 설치되어있는 패키지이며,

서버에서 발생하는 로그를 순환시켜 서버 성능 유지 및 오류 모니터링등에 활용한다.

 


 

1. 서론

- 로그는 서버 오류 디버깅 모니터링, 통계 확인 다양한 목적으로 필요하다.

- 로그가 지속적으로 쌓일 경우 많은 용량을 차지하여 안정적인 서버 구동에 영향을 미칠 가능성이 있고,

- 또한 쌓인 로그를 확인할 때에도 적절하게 분리되어있지 않으면 확인에 많은 시간을 사용해야 할 수도 있다.

- 이 외에도 다양한 상황을 고려하면 로그는 정책에 의해 오래된 로그 삭제/백업 등 별도의 처리를 해주는것이 효율적이다.

- 일반적으로 이러한 과정을 'rotate(또는 rolling)' 이라고 한다.

 

- rotate 정책을 적용하는 방법은 정말 다양한데, 

- Spring Boot의 경우 spring-boot-starter-web 에 logback 이 기본적으로 탑재되어 이를 활용하여 rotate가 가능하며,

- Node.js 기반의 서버도 winston, morgan등을 조합하여 rotate 정책을 만든다.

- 그러나 Web Server 구축 과정에서 주로 사용되는 Nginx, Apache 는 주로 리눅스 기본 패키지인 logrotate를 활용하여 로그를 관리한다.

 

2. logrotate 가 실행되는 과정

- logrotate는 crontab을 사용하여 주기적으로 실행된다.

- /etc/cron.daily의 logrotate 파일에 이에 관한 내용이 있다.

- crontab에서는 /etc/logrotate.conf 파일을 참조하고, 해당 파일은 /etc/logrotate.d 경로를 include하고있다.

# see "man logrotate" for details

# global options do not affect preceding include directives

# 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

# 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 also be configured here.

- 해당 경로에는 nginx, apache등의 서버 이름으로 설정 파일들이 생성되어있다.

- 특별한 경우가 아닌 이상, 'cron에 의해 매일 호출된다' 정도만 알면 될 것 같고, 실제 서버 로그에 대한 설정은 /etc/logrotate.d 폴더 내 각 서버 이름으로 저장되어있는 구성 파일을 수정하면 된다.

 

 

 

3. logrotate 설정 파일 구성

- 아래는 필자의 서버에 구성해놓은 nginx logrotate 구성파일이다.

/data/log/nginx_*.log {
    daily
    rotate 90
    dateext
    missingok
    notifemptyi
    copytruncate
    postrotate
        /usr/sbin/nginx -s reload
    endscript
}

- 로그의 경로와 함께, daily로 실행한다, 90일이 지나면 rotate한다, 로그파일이 없으면 새로 생성한다, rotate가 끝나면 nginx를 재기동한다 등의 옵션을 설정할 수 있다.

- 자세한 옵션값들은 아래 표로 정리한다.

설정
의미
daily | weekly | monthly | yearly 
rotate 주기
rotate 개수
순환되어 보관될 파일 개수
compress
순환될 파일 압축(gzip) 
nocompress
순환될 파일 압축하지 않음
compressext 확장자명
압축된 백업로그파일에 지정할 확장자 설정
compresscmd 압축명
gzip이외의 압축파일 지정 
cpmpressoptions 옵션
압축프로그램에 대한 옵션 설정(-9: 압축률 최대)
dateext
로그파일에  YYYYMMDD형식의 확장자 추가
errors 메일주소
에러발생시 지정된 메일주소로 메일 발송
extention 확장자명
순환된 로그파일의 확장자 지정
ifempty
로그파일이 비어있는 경우 순환(기본값)
notifempty 
로그파일이 비어있는 경우 순환하지 않는다.
mail 메일주소
순환 후 이전 로그파일을 지정된 메일주소로 발송
maxage
count로 지정된 날수가 지난 백업파일 삭제
missingok
로그파일이 없을 경우에도 에러처리하지 않는다.
prerotate / endscript
순환작업 전에 실행할 작업 설정
postrotate / endscript
순환작업 후에 실행할 작업 설정
sharedscripts
prerotate, postrotate 스크립트를 한번만 실행
size 사이즈
순환결과 파일사이즈가 지정한 크기를 넘징낳도록 설정
copytruncate
현재 로그파일의 내용을 복사하여 원본로그 파일의 크기를 0으로 생성

- 출처 : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=sory1008&logNo=221124291927

 

 

4. 추가적인 정보들

- logrotate의 버전을 확인하려면 아래 명령어를 입력한다.

logrotate --version


logrotate 3.19.0

    Default mail command:       /usr/bin/mail
    Default compress command:   /bin/gzip
    Default uncompress command: /bin/gunzip
    Default compress extension: .gz
    Default state file path:    /var/lib/logrotate/status
    ACL support:                yes
    SELinux support:            yes

 

- github에서 오픈소스로 관리되고 있으며, 커밋도 꾸준히 발생하고 있다.

https://github.com/logrotate/logrotate

 

GitHub - logrotate/logrotate: The logrotate utility is designed to simplify the administration of log files on a system which ge

The logrotate utility is designed to simplify the administration of log files on a system which generates a lot of log files. - GitHub - logrotate/logrotate: The logrotate utility is designed to si...

github.com

 

 

 

 

마무리

알아두면 로그를 관리하는데 유용하게 활용할 수 있다.(사실 nginx 서버 운영에는 필수가 아닌가 싶다.)

 

 

-퍼가실 때는 출처를 꼭 같이 적어서 올려주세요!

 

반응형