메뉴 건너뛰기

Bigdata, Semantic IoT, Hadoop, NoSQL

Bigdata, Hadoop ecosystem, Semantic IoT등의 프로젝트를 진행중에 습득한 내용을 정리하는 곳입니다.
필요한 분을 위해서 공개하고 있습니다. 문의사항은 gooper@gooper.com로 메일을 보내주세요.



/etc/logrotate.d 을 이용한 catalina.out 나누기

 

Apache Tomcat의 로그파일인 catalina.out은 기본적으로 한개의 파일에 로그가 쌓이게 됩니다.


Tomcat을 재시작 하지 않는 이상 계속적으로 쌓이는 듯합니다. 벌써 용량이 후덜덜하네요.
이 파일을 다시는 볼일이 없다면 상관이 없지만...
열어 보려고 한다면... 시간 꽤 걸리듯합니다.
catalina.out의 만행을 그냥 두고 못 본척하신다면... 나중에 눈물 흘릴 수도 있습니다. :")

저도 모니터링 중에 문제가 생겨서 확인하려고 catalina.out 파일을 열어봤더니...
시간이 좀 걸리더군요...

그래서 catalina.out을 나누는 방법을 검색해봤습니다.

로그 파일을 rotate하는 방법에는 보통 4가지가 있습니다.

1. log4j
2. cronolog
3. logrotate
4. shell script


이번 포스트에서는 logrotate에 대하여 알아보겠습니다. 가장 사용하기 쉽고 제가 원하는 동작을 하더군요. :")

구분위치 및 실행 방법설명
데몬(위치)/usr/sbin/logrotate데몬의 위치 및 데몬프로그램
데몬 설정파일/etc/logrotate.conf설정파일
설정디렉토리/etc/logrotate.dlogrotate에 적용할 각종로그파일들 보관하는 디렉토리
상황파일/var/lib/logrotate.statuslogrotate한 작업내역을 보관한 파일
cron(일단위)/etc/cron.daily/logrotatelogrotate는 주기적으로 실행이되어야하므로 cron에 의해 일단위로 실행이 됨


Apache Tomcat 로그 순환 설정 : http://koov.net/blog/?/tag/rotate
위 블로그 가시면 간단히 catalina.out 로그 파일을 자동으로 나누게 설정하는

방법이 있습니다.

$ cd /etc/logrotate.d
$ cat > tomcat

/var/local/tomcat/logs/catalina.out {
copytruncate
daily
rotate 30
missingok
notifempty
}

$ logrotate -f /etc/logrotate.d/tomcat


실행 후 화면,


위 이미지와 같이 catalina.out.1 이라는 파일이 생성되는군요. 흠... 그럼 1, 2, 3 이런식으로 쌓일텐데 관리자의 입장에서는 좀 불편할 수도.... Buuuuuuuuuuut!!!!!

dateext 라는 옵션이 있군요!!!

$ cd /etc/logrotate.d
$ cat > tomcat

/var/local/tomcat/logs/catalina.out {
copytruncate
daily
rotate 30
missingok
notifempty
dateext
}

$ logrotate -f /etc/logrotate.d/tomcat




알아두셔야 할 점은 copytruncate 옵션은 반드시 써야 한다고 합니다.

copytruncate 옵션을 쓰지 않으면 기존의 맨처음 로그가 생성된 파일에 계속해서 로그가 쌓이게 됩니다. 이러한 원인은 톰캣의 로그가 System.err 객체나 System.out을 사용하기 때문에 시스템의 raw디바이스로 출력이 되어 파이프를 타지 않기 때문입니다.
따라서 원래 로그를 다른이름으로 저장하고 새로운 catalina.out을 생성한다해도 실제 로그 스트림은 다른이름으로 지정된 원래 로그쪽으로 계속해서 쌓이게 됩니다.
이걸 방지하기 위해 현재 로그내용을 복사하여 백업본으로 저장한후 원본 로그의 내용을 비우는 방식으로 저장해야 합니다.
그래서 옵션을 copytruncate를 사용해야 합니다.

copytruncate
Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one, It can be used when some program can not be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.




logrotate의 설정 옵션에 대하여 조금 더 알아보면은,

dailyLog files are rotated every day.
weeklyLog files are rotated if the current weekday is less than the weekday of the last rotation or if more than a week has passed since the last rotation. This is normally the same as rotating logs on the first day of the week, but if logrotate is not being run every night a log rotation will happen at the first valid opportunity.
monthlyLog files are rotated the first time logrotate is run in a month (this is normally on the first day of the month).
notifemptyDo not rotate the log if it is empty (this overrides the ifempty option).
nocompressOld versions of log files are not compressed.
delaycompressPostpone compression of the previous log file to the next rotation cycle. This only has effect when used in combination with compress. It can be used when some program cannot be told to close its logfile and thus might continue writing to the previous log file for some time.
compressOld versions of log files are compressed with gzip by default.
mail addressWhen a log is rotated out of existence, it is mailed to address. If no mail should be generated by a particular log, the nomail directive may be used.
missingokIf the log file is missing, go on to the next one without issuing an error message.


보다 더 많은 옵션과 설명을 보시려면, http://linuxcommand.org/man_pages/logrotate8.html를 참고하세요.

superuser 사이트입니다. 옵션에 대해서 한글로 자세히 설명되어 있습니다.
http://www.superuser.co.kr/linux/logrotate/page04.htm

번호 제목 글쓴이 날짜 조회 수
58 magento2 샘플데이타 설치 총관리자 2017.01.31 24
57 [vi] test.nq파일에서 특정문자열(예, <>)을 찾아서 포함되는 라인을 삭제한 동일한 이름의 파일을 만드는 방법 총관리자 2017.01.25 36
56 [u-Auctions]목록이 1개만 나오는 문제 총관리자 2017.05.29 38
55 eclipse 3.1 단축키 정리파일 총관리자 2017.01.02 38
54 magento2 설치후 초기화면이 깨지는 문제 file 총관리자 2017.01.31 42
53 windows 혹은 mac에서 docker설치하기 위한 파일 총관리자 2017.10.13 42
52 magento2 log파일 위치 총관리자 2017.01.31 45
51 webid에서 google처럼 검색할 수 있도록 하는 프로그램 총관리자 2017.05.16 46
50 "암은 평범한 병, 심호흡만 잘해도 암세포 분열 저지” 총관리자 2016.06.02 48
49 LAGOM 1.4 AND KUBERNETES ORCHESTRATION 총관리자 2019.07.19 78
48 에러 추적(Error Tracking) 및 로그 취합(logging aggregation) 시스템인 Sentry 설치 총관리자 2018.03.14 88
47 php auction 프로그램 총관리자 2017.05.14 94
46 컴퓨터 무한 재부팅 원인및 조치방법 file 총관리자 2017.12.05 106
45 https용 인증서 발급 명령문 예시및 오류 메세지 총관리자 2018.01.24 109
44 바나나 파이의 /tmp폴더를 외장하드로 변경하기 총관리자 2015.07.24 136
43 magento2 2.1.3을 수동으로 설치하는 방법 총관리자 2017.02.01 147
42 천문학적, 기후학적, 기상학적, 생물학적, 농사계절 구분 총관리자 2015.12.16 147
41 이미지 관리 오픈소스 목록 총관리자 2018.03.11 149
40 [PHP7.0]로그파일 위치 총관리자 2017.05.07 151
39 대표 오픈소스 라이선스, 한 눈에 보기! 총관리자 2015.12.10 168

A personal place to organize information learned during the development of such Hadoop, Hive, Hbase, Semantic IoT, etc.
We are open to the required minutes. Please send inquiries to gooper@gooper.com.

위로