JSP

 

스프링으로 개발하는 것이 아니기 때문에 

https://www.slf4j.org/docs.html 

slf4j 가 아닌 아파치 logging 를 사용하자.

 

 

다운로드 주소 

http://logging.apache.org/log4j/2.x/download.html

또한 메이븐으로 개발하는 것이 아니기 때문에 라이브러리를 일일이 찾아서 설정하기가 힘들다. 따라서

다운로드 받은 폴더 중에   아래 라이브러리라만 활용하기로 하자.

log4j-1.2-api-2.9.1.jar

log4j-api-2.9.1.jar

log4j-core-2.9.1-sources.jar

log4j-core-2.9.1-tests.jar

log4j-core-2.9.1.jar

log4j-web-2.9.1.jar

 

 

WEB-INF  폴더 아래 classes 폴더를 생성한다.

다음과 같이 

log4j2.xml 파일을 생성한다.

다음을 복사해서 넣자.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  
  <Loggers>
    <Logger name="com.example" level="INFO" />
    <Root level="debug" >
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
  
</Configuration>

 

https://github.com/apache/logging-log4j2

세부 필터 설정 은 

http://logging.apache.org/log4j/2.x/manual/filters.html    <= 이 페이지에 찾아서 취향에 맞게 설정하면 된다.

 

잘 설정했는지 테스트를  다음과 같이 해보자.

 

TestLog

package config;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@WebServlet("/TestLog")
public class TestLog extends HttpServlet {

	public static Logger logger2 = LogManager.getFormatterLogger(TestLog.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		final Logger logger = LogManager.getLogger(TestLog.class);

		logger.debug("debug, servlet! {}", "debug- aaa");
		logger.info("Hello,  {}", "info- aaa");
		logger.error("error, servlet! {}", "error- aaa");
		logger.warn("warn, servlet! {}", "warn- aaa");
		logger.fatal("fatal, servlet! {}", "fatal- aaa");

		logger2.debug("Logging in user %s with birthday %s", "macaronics", "20171003");
		logger2.debug("Integer.MAX_VALUE = %,d", Integer.MAX_VALUE);
		logger2.debug("Long.MAX_VALUE = %,d", Long.MAX_VALUE);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

00:38:28.960 [http-nio-8090-exec-20] DEBUG config.TestLog - debug, servlet! debug- aaa
00:38:29.305 [http-nio-8090-exec-20] INFO  config.TestLog - Hello,  info- aaa
00:38:29.305 [http-nio-8090-exec-20] ERROR config.TestLog - error, servlet! error- aaa
00:38:29.305 [http-nio-8090-exec-20] WARN  config.TestLog - warn, servlet! warn- aaa
00:38:29.306 [http-nio-8090-exec-20] FATAL config.TestLog - fatal, servlet! fatal- aaa
00:38:29.307 [http-nio-8090-exec-20] DEBUG config.TestLog - Logging in user macaronics with birthday 20171003
00:38:29.317 [http-nio-8090-exec-20] DEBUG config.TestLog - Integer.MAX_VALUE = 2,147,483,647
00:38:29.317 [http-nio-8090-exec-20] DEBUG config.TestLog - Long.MAX_VALUE = 9,223,372,036,854,775,807

 

 

macaronics.net

소스 :  https://github.com/braverokmc79/jsp_sin

 

 

 

about author

PHRASE

Level 60  라이트

사랑은 최선의 것을 더욱 좋은 것으로 만든다. -윌리엄 워즈워드

댓글 ( 4)

댓글 남기기

작성