egovframework.rte.fdl.logging.db.EgovJDBCAppender.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.rte.fdl.logging.db.EgovJDBCAppender.java

Source

/*
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed 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.
 */
package egovframework.rte.fdl.logging.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.log4j.MDC;
import org.apache.log4j.db.DBHelper;
import org.apache.log4j.jdbc.JDBCAppender;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
import org.springframework.stereotype.Component;

import egovframework.rte.fdl.logging.db.SingletonDataSourceProvider;

/**
 * log4j ? JDBCAppender ?  Spring DataSource  ?  ?
 * <p>
 * <b>NOTE</b>: log4j? JDBCAppender ? extends   JDBCAppender  log4j ? ?
 * ? ?  Connection ? ? ? ?, EgovJDBCAppender  Spring ? dataSource 
 * Annotation ? injection      Appender ?.
 * 
 * @author   
 * @since 2009.03.09
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.03.09             ?
 * 
 * </pre>
 */
@SuppressWarnings("deprecation")
@Component("egovJDBCAppender")
public class EgovJDBCAppender extends JDBCAppender {

    /**     flag */
    boolean locationInfo = false;

    /**  dataSource provider - Spring ? dataSource  */
    private final SingletonDataSourceProvider provider;

    /**
     * '@Autowired' Annotation ? dataSource   ?
     * SingletonDataSourceProvider ? setDataSource    .
     * 
     * @param dataSource
     *            - Spring ?  dataSource
     */
    // @Resource(name = "dataSource")
    //@Autowired(required = false)
    @Resource(name = "dataSource")
    public void setDataSource(DataSource dataSource) {
        provider.setDataSource(dataSource);
    }

    /**
     * EgovJDBCAppender ?  ??? SingletonDataSourceProvider  ? .
     * EgovJDBCAppender  log4j ? ? ?, ? Spring Container ? bean  ??
     * Container ? ?  dataSource injection  ?? ?   (log4j ? )
     * SingletonDataSourceProvider ? ? ?? .
     */
    public EgovJDBCAppender() {
        this.provider = SingletonDataSourceProvider.getInstance();
    }

    /**
     * connection pool ? connection ? ??  override .
     * 
     * @param con
     *            -  ? connection
     */
    @Override
    protected void closeConnection(Connection con) {
        try {
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *  Spring ??   Spring ?  dataSource  connection ?  
     * override .
     * 
     * @return Spring ? dataSource ? connection
     * @exception SQLException
     */
    @Override
    protected Connection getConnection() throws SQLException {
        return provider.getDataSource().getConnection();
    }

    /**
     * connection ?        ?? override .
     * 
     * @param sql
     *            - log4j.xml ? ? <param name="sql" .. ? 
     * @exception SQLException
     */
    @Override
    protected void execute(String sql) throws SQLException {

        Connection con = null;
        Statement stmt = null;

        try {
            con = getConnection();
            // dataSource bean ?  autoCommit false  ?   true  
            con.setAutoCommit(true);

            stmt = con.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            stmt.close();
            closeConnection(con);
        }

        // LogFactory.getLog("sysoutLogger").debug("Execute: " + sql);
    }

    /**
     * JDBCAppender ?  ?    ? buffer?   
     * log4j-1.3alpha-8 ? DB Appender ?  ? logging_event ?? 
     * (sequence_number, timestamp, rendered_message, logger_name,
     * level_string, thread_name, reference_flag)  locationInfo ?  
     * (caller_filename, caller_class, caller_method, caller_line) MDC  ?
     * .
     * 
     * @param event
     *            - JDBCAppender ? ?  LoggingEvent
     */
    @Override
    public void append(LoggingEvent event) {
        MDC.put("sequence_number", event.getSequenceNumber());
        MDC.put("timestamp", event.getTimeStamp());
        MDC.put("rendered_message", event.getRenderedMessage());
        MDC.put("logger_name", event.getLoggerName());
        MDC.put("level_string", event.getLevel().toString());
        String ndc = event.getNDC();
        if (ndc != null) {
            MDC.put("ndc", ndc);
        }
        MDC.put("thread_name", event.getThreadName());
        MDC.put("reference_flag", DBHelper.computeReferenceMask(event));

        LocationInfo li;

        if (event.locationInformationExists() || locationInfo) {
            li = event.getLocationInformation();
        } else {
            li = LocationInfo.NA_LOCATION_INFO;
        }

        MDC.put("caller_filename", li.getFileName());
        MDC.put("caller_class", li.getClassName());
        MDC.put("caller_method", li.getMethodName());
        MDC.put("caller_line", li.getLineNumber());

        // TODO Auto-generated method stub
        super.append(event);
    }

    /**
     * log4j.xml ? <param name="locationInfo" value="true" /> locationInfo  
     * 
     * @return boolean(true/false)
     */
    public boolean isLocationInfo() {
        return locationInfo;
    }

    /**
     * log4j.xml ? locationInfo ?  setter. locationInfo ?   default 
     * false .
     * 
     * @param locationInfo
     *            - <param name="locationInfo" value="true" />  ? 
     */
    public void setLocationInfo(boolean locationInfo) {
        this.locationInfo = locationInfo;
    }

    /**
     * SingletonDataSourceProvider  ??  getter - Singleton ??   ?
     * .
     * 
     * @return  EgovJDBCAppender ?  ? SingletonDataSourceProvider
     */
    public SingletonDataSourceProvider getProvider() {
        return provider;
    }

}