반응형
1. 로그가 저장될 테이블 생성
CREATE TABLE `log` (
`reg_dt` DATETIME NULL DEFAULT NULL,
`level` VARCHAR(100) NULL DEFAULT NULL,
`logger` VARCHAR(100) NULL DEFAULT NULL,
`message` LONGTEXT NULL,
`exception` LONGTEXT NULL
)
2. 메이븐 추가 pom.xml
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
3. DatabaseConnection 클래스 생성
package net.example.db;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnection;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
public class ConnectionFactory {
private static interface Singleton {
final ConnectionFactory INSTANCE = new ConnectionFactory();
}
private final DataSource dataSource;
private ConnectionFactory() {
Properties properties = new Properties();
properties.setProperty("user", "logging"); //db 계정
properties.setProperty("password", "abc123"); // db 비밀번호
try {
loadDriver("com.mysql.jdbc.Driver"); //db드라이버
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
"jdbc:mysql://example.org:3306/exampleDb", properties
);// db 주소
new PoolableConnectionFactory(
connectionFactory, pool, null, "SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
);
this.dataSource = new PoolingDataSource(pool);
}
public static Connection getDatabaseConnection() throws SQLException {
return Singleton.INSTANCE.dataSource.getConnection();
}
private static void loadDriver(String driver) throws SQLException{
try
{
Class.forName(driver).newInstance();
}
catch(Exception e)
{
throw new SQLException("Unable to load driver: " + driver);
}
}
}
4. log4j2.xml 설정 추가
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %5p [%c] %m%n" />
</Console>
<!-- 추가 -->
<JDBC name="db" tableName="log">
<ConnectionFactory class="net.example.db.ConnectionFactory" method="getDatabaseConnection" />
<Column name="reg_dt" isEventTimestamp="true" />
<Column name="level" pattern="%p" />
<Column name="logger" pattern="%c" />
<Column name="message" pattern="%m" />
<Column name="exception" pattern="%ex{full}" />
<!-- 모든 로그 다 저장하면 속도가 느리기 때문에 로그필터 적용 (없어도 상관없음 , 필요에따라 필터 수정)-->
<Filters>
<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
<RegexFilter regex=".*Exception.*" onMatch="NEUTRAL" onMismatch="DENY"/>
<RegexFilter regex=".*exception.*" onMatch="NEUTRAL" onMismatch="DENY"/>
</Filters>
<!-- 모든 로그 다 저장하면 속도가 느리기 때문에 로그필터 적용 (없어도 상관없음 , 필요에따라 필터 수정)-->
</JDBC>
<!-- 추가 -->
</Appenders>
<!--로그저장이 필요한곳에 추가 아래는 예시 -->
<Root level="ERROR">
<AppenderRef ref="console" />
<AppenderRef ref="db" /> <!--추가 -->
</Root>
<logger name="org.springframework" level="DEBUG" additivity="false">
<appender-ref ref="console" />
<AppenderRef ref="db" /> <!--추가 -->
</logger>
반응형
'Web Spring 전정프' 카테고리의 다른 글
javascript 파일 용량 , 파일 확장자 체크 (0) | 2021.02.02 |
---|---|
전자정부프레임워크 Exception 관리 ExceptionHandler 적용 방법 (0) | 2021.01.22 |
javascript 마우스 오른쪽 클릭 우클릭 , 드래그 막는 방법 (0) | 2021.01.12 |
bootstrap-datepicker 부트스트랩 데이트피커 시작일 종료일 설정 방법 (0) | 2020.12.28 |
java xml String document 변환 하는 방법 (0) | 2020.12.18 |