메뉴 건너뛰기

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

번호 제목 글쓴이 날짜 조회 수
38 hadoop 기반 문서 검색 총관리자 2014.09.25 957
37 하둡 분산 파일 시스템을 기반으로 색인하고 검색하기 총관리자 2014.09.25 951
36 mysql 5.5.34-0ubuntu0.13.04용 설치/진행 화면 총관리자 2014.09.10 697
35 외부 기기(usb, 하드)등 mount(연결)하기 총관리자 2014.08.03 546
34 anaconda3 (v5.2) 설치및 머신러닝 관련 라이브러리 설치 절차 총관리자 2018.07.27 513
33 데이타 제공 사이트 링크 총관리자 2014.08.03 508
32 원격지에서 zio공유기를 통해서 노트북의 mysql접속을 허용하는 방법 총관리자 2014.09.07 494
31 시스템날짜를 현재 정보로 동기화 하는 방법(rdate, ntpdate이용) 총관리자 2014.08.24 481
30 원보드 컴퓨터 비교표 file 총관리자 2014.08.04 408
29 Ubuntu 16.04 LTS에서 사이트에 무료인증서를 이용하여 SSL적용 file 총관리자 2017.05.23 353
28 Runtime.getRuntime().exec(cmd) sample 소스 총관리자 2015.11.19 305
27 여러 홈페이지를 운영하거나 혹은 서버에 가입한 사용자들에게 홈페이지 계정을 나누어 줄수 있도록 설정/계정 생성방법 총관리자 2018.01.23 281
26 [MemoryLeak분석]다수의 MongoCleaner 쓰레드가 Sleep상태에 있으면서 Full GC가 계속 발생되는 문제 해결방법 file 총관리자 2017.01.11 263
25 부팅을 외장하드에서 하도록 변경하는 방법 총관리자 2015.07.28 247
» /etc/logrotate.d 을 이용한 catalina.out 나누기 file 총관리자 2017.01.19 208
23 [개발] 온라인 IDE - 개발 환경 구축 없어 어디서나 웹브라우저로 개발하기 총관리자 2022.05.02 200
22 anaconda3(v5.4)를 이용하여 tensorflow설치후 ipython프로그램을 실행하여 import할때 오류발생시 조치 총관리자 2018.07.27 188
21 우분투 16.04 설치후 APM (Apache2, PHP, MySQL) 설치 총관리자 2017.01.29 185
20 대표 오픈소스 라이선스, 한 눈에 보기! 총관리자 2015.12.10 168
19 [PHP7.0]로그파일 위치 총관리자 2017.05.07 151

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.

위로