반응형
log를 출력하는데 에러가 발생하고 출력이 되지 않아 해결 방법을 정리합니다.
에러 원인
log4j.xml이나 log4j.properties를 찾지 못해서 생기는 에러입니다.
해결 방법
WEB-INF/classes에 log4j.xml로 이동합니다.
web.xml에 log4j.xml에 등록합니다.
web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.xml</param-value>
</context-param>
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration
xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console-infolog"
class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p %m%n" />
</layout>
</appender>
<appender name="CONSOLE"
class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%p - %C{1}.%M(%L) | %m%n" />
</layout>
</appender>
<appender name="FILEAPPENDER"
class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="file" value="kr/Sunshiny/logs/Execute_free.log" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{MMM dd hh:mm:ss aa} %p - %C{1}.%M(%L) | %m%n" />
</layout>
</appender>
<appender name="ADVICECONSOLE"
class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p - %m%n" />
</layout>
</appender>
<appender name="CONSOLE_SERVER"
class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%p[%d{yyy-MM-dd HH:mm:ss}] | %m%n" />
</layout>
</appender>
<!-- logger 의 name 에 정의된 패키지의 하위를 디버깅 함. -->
<logger name="com.otrodevym">
<level value="DEBUG" />
</logger>
<logger name="org.apache">
<level value="ERROR" />
</logger>
<logger name="java.sql.Connection">
<level value="ERROR" />
</logger>
<logger name="java.sql.Statement">
<level value="ERROR" />
</logger>
<logger name="java.sql.PreparedStatement">
<level value="INFO" />
</logger>
<logger name="java.sql.ResultSet">
<level value="INFO" />
</logger>
<category name="log4j.logger.org.hibernate.SQL">
<priority value="ERROR" />
</category>
<category name="log4j.logger.org.hibernate.transaction">
<priority value="INFO" />
</category>
<category name="log4j.logger.org.hibernate.jdbc">
<priority value="ERROR" />
</category>
<root>
<level value="ERROR" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
LoggerTest.java
package com.otrodevym.mfaw.common;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="file:src/main/webapp/WEB-INF/spring/**/**.xml")
@WebAppConfiguration
public class LoggerTest {
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
@Test
public void LoggerTest() {
logger.info("---info");
logger.debug("---debug");
logger.error("---error");
logger.warn("-----warn");
logger.trace("----trace");
}
}
출력
반응형