본문 바로가기

리눅스

Ubuntu 환경에서 Tomcat 로그 관리하기 [Linux, Ubuntu, Tomcat, Log]

반응형

최근에 포스팅하며 올린 서버의 로그를 막 찾아다니고 있는데

어.. 뭔가 좀 많지 않아? 12일것도 남아있네;; 그리고 /var/lib는 뭔데 중복이지?;

그래서 일단 로그를 최소한만, 2일이 지난 파일까지만 저장해두고 나머진 삭제하려고 한다. 그리고 왜 /var/lib에도 tomcat9가 있고 /etc에도 tomcat9가 있는지 알아내봐야겠다.


1. 일단 어떤파일을 수정해야할지부터 알아볼까?

catalina.sh는 정상적으로 하나만 있고..
네? 저기요 선생님.. logging.properties파일이 뭐가 이렇게 많아요?

그 래 서.. 급하게 톰캣 페이지를 뒤져봤다.

출처: https://tomcat.apache.org/tomcat-9.0-doc/introduction.html

 

Apache Tomcat 9 (9.0.50) - Introduction

Before you start using CATALINA_BASE, first consider and create the directory tree used by CATALINA_BASE. Note that if you do not create all the recommended directories, Tomcat creates the directories automatically. If it fails to create the necessary dire

tomcat.apache.org

CATALINA_HOME은 실질적으로 Tomcat이 깔린 경로, CATALINA_BASE는 런타임 구성 루트이고 (톰캣을 여러개 쓸 때) 뭔가를 수정하려면 CATALINA_BASE에 있는걸 수정하라는 것 같다.

 

그럼 내 서버에 깔려있는 Tomcat의 HOME은 어디고 BASE는 어디인가?

이 화면은 내가 프로젝트를 배포하기 전, 그냥 톰캣을 실행했을 때 나온 화면이다. It works! 제외 3번째줄 중간을 보면,

아하~ 내 CATALINA_HOME 은 /usr/share/tomcat9이고 CATALINA_BASE는 /var/lib/tomcat9 라고? 오키 확인했음

그럼 이제 어떤 파일을 수정해야할 지 쬐끔씩 감이 온다.

출처: https://tomcat.apache.org/tomcat-9.0-doc/logging.html

 

Apache Tomcat 9 (9.0.50) - Logging in Tomcat

The internal logging for Apache Tomcat uses JULI, a packaged renamed fork of Apache Commons Logging that is hard-coded to use the java.util.logging framework. This ensures that Tomcat's internal logging and any web application logging will remain independe

tomcat.apache.org

오호라, ${catalina.base}/conf/logging.properties가 대표적이라구?

그럼 내 CATALINA_BASE는 /var/lib/tomcat9 이고, 거기에서 /conf/logging.properties파일을 수정해야 하니 결론은..

/var/lib/tomcat9/conf/logging.properties 파일을 수정하면 된다~ 고말이야

근데 여기서 의문이 생겼다.. 왜 그런데 find를 했을 때에는 /var/lib/tomcat9/conf/logging.properties가 검색이 안됐지? /var은 가변적인 데이터만 들어있다고 기억하고 있었는데.. 혹시 심볼릭 링크인건가?

아니 진짜 분명히 없었잖아..내 눈이 벌써 침침할리가 없어.. 난 아직 젊다고.. ! 크윽···!

 

cd /var/lib/tomcat9
ls -l conf
lrwxrwxrwx 1 root root 12 Oct 20  2020 conf -> /etc/tomcat9
# conf -> /etc/tomcat9 라는 것은 심볼릭링크인 conf는 원본폴더인 /etc/tomcat9를 바라보고 있다.

그렇다.. /var/lib/tomcat9/conf는 /etc/tomcat9의 심볼릭 링크였던거였다.. 그래서 아마도 원본파일만 검색이 되고 심볼릭링크 경로는 검색이 안됐던 거겠지.. 그래서 나는 원본 폴더인 /etc/tomcat9의 logging.properties를 수정하기로 했다.

심볼릭 링크란?
단순하게 원본파일을 가리키는 링크만 연결하는 건데, 쉽게 말하자면 바로가기다. 그래서 실존하지 않은 경로이기 때문에 검색이 안됐던 것이 아닐까?
위의 두 폴더는 심볼릭 링크로 연결되어있기 때문에 어디에서 수정하던지 양쪽 다 수정된다.

2. 설정파일을 수정해보자

cd /etc/tomcat9
vi logging.properties

아래는 기본적으로 설정되어있었던 logging.properties이다.

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.SystemdFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler

# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE

# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE

# To see debug messages for HTTP/2 handling, uncomment the following line:
#org.apache.coyote.http2.level = FINE

# To see debug messages for WebSocket handling, uncomment the following line:
#org.apache.tomcat.websocket.level = FINE

아래처럼 수정해주었다.

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.org.apache.juli.AsyncFileHandler.level = WARNING
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 2

2localhost.org.apache.juli.AsyncFileHandler.level = WARNING
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 2

java.util.logging.ConsoleHandler.level = WARNING
java.util.logging.ConsoleHandler.formatter = org.apache.juli.SystemdFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = WARNING
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler

# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE

# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE

# To see debug messages for HTTP/2 handling, uncomment the following line:
#org.apache.coyote.http2.level = FINE

# To see debug messages for WebSocket handling, uncomment the following line:
#org.apache.tomcat.websocket.level = FINE

level을 모두 WARNING으로 변경해줬으며, maxDays를 2로 두었다.

service tomcat9 restart

이후 /var/logs/tomcat9 폴더를 확인해보면 오늘, 어제, 2일전 로그만 남아있고 모두 지워진 걸 확인할 수 있다.

 

 

 

 

 

 

 

 

 

 

그리고 난 오늘도 나 자신이 똥멍청이임을 뼈저리게 깨달았다..... 아이고난....