linux log 관리하기

Younghwan Cha·2023년 6월 19일
post-thumbnail

log 는 뭐랄까
꼭 안 쌓으면 터지는 오묘한 존재다.
평소에는 필요없어서 등한시하다가
중요한 순간에 문제 해결사로 등장하는 그런 존재
오늘은 linux 에서 log 쌓는 것에 대해 정리해보려 한다

logrotate

linux 에서는 log/var/log directory 에 쌓도록 명시하고 있다.
그리고, logrotate 와 관련된 설정은 /etc/logrotate.conf 에서 진행한다.

# /etc/logrotate.conf

# 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.

해당 설정에 대해 하나씩 알아보자.

/etc/logrotate.d

해당 폴더에는 개별 package 들의 logrotation 규칙들이 저장되어 있다. /etc/logrotate.d/nginx 의 설정을 한번 보자.

/var/log/nginx/*.log {
        daily
        
        missingok
        rotate 14
        
        compress
        delaycompress
        
        notifempty
        
        dateext
        dateformat -%Y%m%d
        
        create 0640 www-data adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}

/var/log/nginx/*.log
rotate 할 파일들을 지정한다

rotate [Count]
log file 은 삭제되기 전에 Count 수 만큼 rotate 된다
Count 0: rotate 되지 않고 삭제 [Default]
Count -1: 삭제되지 않음

missingok
log file 이 없을 경우 error 를 던지지 않고 다음 log 로 넘어간다

compress
이전 log 파일들을 gzip 파일로 압축하여 저장한다

delaycompress
프로그램이 log 파일에 대한 입력을 종료하지 못하고 일정 기간 계속 쌓아야 할 경우, log 파일에 대한 rotate 를 다음 주기로 미룬다. 이 설정은 compress 옵션과 함께 쓰여야한다

notifempty
file 이 비어있을 경우 rotate 하지 않는다

dateext/dateformat
archive 처리된 log 의 이름에 시간 정보를 기입한다.
시간 정보 포멧은 dateformat 에서 정의하며, logrotate 는 파일 이름을 정렬하여 더 오래된 log 를 삭제하기 때문에 Y/M/D
형식은 되지만 D/M/Y 형식으로 저장해서는 안된다

copytruncate
log 파일의 rotate 이후에 log 파일을 새로 생성하는 대신에 기존 log 파일의 사이즈를 0 으로 만든다. 이는 특정 프로그램에서 기존의 이름과 동일한 log 파일에 계속 log 를 쌓아야 할 경우에 사용하며, 사이즈를 0 으로 만들고 다시 쌓는 과정에서 어느 정도의 log 손실이 일어날 수 있다.

[logrotate man] https://man7.org/linux/man-pages/man8/logrotate.8.html
[logrotate] https://www.redhat.com/sysadmin/setting-logrotate

profile
개발 기록

0개의 댓글