메뉴 건너뛰기

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

번호 제목 글쓴이 날짜 조회 수
360 [vi]블럭 및 문서내 복사등에 관련된 명령어 총관리자 2017.02.17 27
359 서버중 slave,worker,regionserver만 재기동해야 할때 필요한 기동스크립트및 사용방법 총관리자 2017.02.03 143
358 fuseki가 제공하는 web ui를 통해서 dataset를 remove->create할 경우 동일한 동일한 이름으로 지정했을때 fuseki-server.jar가 뜨지 않는 현상 총관리자 2017.02.03 54
357 magento2 2.1.3을 수동으로 설치하는 방법 총관리자 2017.02.01 147
356 magento2 log파일 위치 총관리자 2017.01.31 45
355 magento2 샘플데이타 설치 총관리자 2017.01.31 24
354 magento2 설치후 초기화면이 깨지는 문제 file 총관리자 2017.01.31 42
353 [Magento]php7에 Composer를 이용하여 Magento 2.1.3 설치 file 총관리자 2017.01.30 970
352 우분투 16.04 설치후 APM (Apache2, PHP, MySQL) 설치 총관리자 2017.01.29 185
351 테이블의 row수를 빠르게 카운트 하는 방법 총관리자 2017.01.26 77
350 파일명 혹은 확장자 일괄 변경하는 방법 총관리자 2017.01.26 51
349 HDFS상의 /tmp폴더에 Permission denied오류가 발생시 조치사항 총관리자 2017.01.25 360
348 [vi] test.nq파일에서 특정문자열(예, <>)을 찾아서 포함되는 라인을 삭제한 동일한 이름의 파일을 만드는 방법 총관리자 2017.01.25 36
347 https://github.com/Merck/Halyard프로젝트 컴파일및 배포/테스트 총관리자 2017.01.24 103
346 Java 8에서 pom.xml에 JavaDoc 관련 태그가 설정되어 있으나 오류등으로 실패하면 나머지 Maven작업이 종료되는 문제 해결 방법 총관리자 2017.01.24 432
345 [springframework]Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Could not read resultset: unexpected end of stream, read 0 bytes from 4 오류 발생시 조치사항 총관리자 2017.01.23 680
344 federated query 예제 총관리자 2017.01.19 31
» /etc/logrotate.d 을 이용한 catalina.out 나누기 file 총관리자 2017.01.19 208
342 [JSON 파싱]mongodb의 document를 GSON을 이용하여 parsing할때 ObjectId값에서 오류 발생시 조치방법 총관리자 2017.01.18 323
341 [tomcat] logrotate를 이용하여 catalina.out로그파일 일별로 로테이션 저장하기 file 총관리자 2017.01.18 318

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.

위로