Search in sources :

Example 1 with CommonsLogFilter

use of com.alibaba.druid.filter.logging.CommonsLogFilter in project druid by alibaba.

the class CommonsLogFilterTest method test_logger.

public void test_logger() throws Exception {
    CommonsLogFilter filter = new CommonsLogFilter();
    filter.setDataSourceLoggerName("_datasource_name_");
    filter.setConnectionLoggerName("_connection_name_");
    filter.setStatementLoggerName("_statement_name_");
    filter.setResultSetLoggerName("_resultset_name_");
    Assert.assertEquals(filter.getDataSourceLoggerName(), "_datasource_name_");
    Assert.assertEquals(filter.getConnectionLoggerName(), "_connection_name_");
    Assert.assertEquals(filter.getStatementLoggerName(), "_statement_name_");
    Assert.assertEquals(filter.getResultSetLoggerName(), "_resultset_name_");
    filter.setDataSourceLogger(new FakeLogger("_datasoure_", true));
    filter.setConnectionLogger(new FakeLogger("_connection_", true));
    filter.setStatementLogger(new FakeLogger("_statement_", true));
    filter.setResultSetLogger(new FakeLogger("_resultset_", true));
    Assert.assertEquals(filter.getDataSourceLoggerName(), "_datasoure_");
    Assert.assertEquals(filter.getConnectionLoggerName(), "_connection_");
    Assert.assertEquals(filter.getStatementLoggerName(), "_statement_");
    Assert.assertEquals(filter.getResultSetLoggerName(), "_resultset_");
    setLogEnableAll(filter, true);
    Assert.assertTrue(filter.isDataSourceLogEnabled());
    Assert.assertTrue(filter.isConnectionLogEnabled());
    Assert.assertTrue(filter.isConnectionLogErrorEnabled());
    Assert.assertTrue(filter.isConnectionConnectBeforeLogEnabled());
    Assert.assertTrue(filter.isConnectionConnectAfterLogEnabled());
    Assert.assertTrue(filter.isConnectionCloseAfterLogEnabled());
    Assert.assertTrue(filter.isConnectionCommitAfterLogEnabled());
    Assert.assertTrue(filter.isConnectionRollbackAfterLogEnabled());
    Assert.assertTrue(filter.isStatementLogEnabled());
    Assert.assertTrue(filter.isStatementLogErrorEnabled());
    Assert.assertTrue(filter.isStatementCreateAfterLogEnabled());
    Assert.assertTrue(filter.isStatementCloseAfterLogEnabled());
    Assert.assertTrue(filter.isStatementExecuteAfterLogEnabled());
    Assert.assertTrue(filter.isStatementExecuteBatchAfterLogEnabled());
    Assert.assertTrue(filter.isStatementExecuteQueryAfterLogEnabled());
    Assert.assertTrue(filter.isStatementExecuteUpdateAfterLogEnabled());
    Assert.assertTrue(filter.isStatementLogErrorEnabled());
    Assert.assertTrue(filter.isStatementParameterSetLogEnabled());
    Assert.assertTrue(filter.isStatementPrepareAfterLogEnabled());
    Assert.assertTrue(filter.isStatementPrepareCallAfterLogEnabled());
    Assert.assertTrue(filter.isResultSetLogEnabled());
    Assert.assertTrue(filter.isResultSetLogErrorEnabled());
    Assert.assertTrue(filter.isResultSetCloseAfterLogEnabled());
    Assert.assertTrue(filter.isResultSetNextAfterLogEnabled());
    Assert.assertTrue(filter.isResultSetOpenAfterLogEnabled());
    // ////
    filter.setDataSourceLogger(new FakeLogger("_datasoure_", false));
    filter.setConnectionLogger(new FakeLogger("_connection_", false));
    filter.setStatementLogger(new FakeLogger("_statement_", false));
    filter.setResultSetLogger(new FakeLogger("_resultset_", false));
    Assert.assertFalse(filter.isDataSourceLogEnabled());
    Assert.assertFalse(filter.isConnectionLogEnabled());
    Assert.assertFalse(filter.isConnectionLogErrorEnabled());
    Assert.assertFalse(filter.isConnectionConnectBeforeLogEnabled());
    Assert.assertFalse(filter.isConnectionConnectAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionCloseAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionCommitAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionRollbackAfterLogEnabled());
    Assert.assertFalse(filter.isStatementLogEnabled());
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    Assert.assertFalse(filter.isStatementCreateAfterLogEnabled());
    Assert.assertFalse(filter.isStatementCloseAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteBatchAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteQueryAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteUpdateAfterLogEnabled());
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    Assert.assertFalse(filter.isStatementParameterSetLogEnabled());
    Assert.assertFalse(filter.isStatementPrepareAfterLogEnabled());
    Assert.assertFalse(filter.isStatementPrepareCallAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetLogEnabled());
    Assert.assertFalse(filter.isResultSetLogErrorEnabled());
    Assert.assertFalse(filter.isResultSetCloseAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetNextAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetOpenAfterLogEnabled());
    // ////////////////////////////////////////
    // ////////////////////////////////////////
    // ////////////////////////////////////////
    setLogEnableAll(filter, false);
    // ////
    filter.setDataSourceLogger(null);
    filter.setConnectionLogger(null);
    filter.setStatementLogger(null);
    filter.setResultSetLogger(null);
    filter.setDataSourceLogger(new FakeLogger("_datasoure_", false));
    filter.setConnectionLogger(new FakeLogger("_connection_", false));
    filter.setStatementLogger(new FakeLogger("_statement_", false));
    filter.setResultSetLogger(new FakeLogger("_resultset_", false));
    filter.setStatementLogErrorEnabled(true);
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    filter.setStatementLogErrorEnabled(false);
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    Assert.assertFalse(filter.isDataSourceLogEnabled());
    Assert.assertFalse(filter.isConnectionLogEnabled());
    Assert.assertFalse(filter.isConnectionLogErrorEnabled());
    Assert.assertFalse(filter.isConnectionConnectBeforeLogEnabled());
    Assert.assertFalse(filter.isConnectionConnectAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionCloseAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionCommitAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionRollbackAfterLogEnabled());
    Assert.assertFalse(filter.isStatementLogEnabled());
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    Assert.assertFalse(filter.isStatementCreateAfterLogEnabled());
    Assert.assertFalse(filter.isStatementCloseAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteBatchAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteQueryAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteUpdateAfterLogEnabled());
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    Assert.assertFalse(filter.isStatementParameterSetLogEnabled());
    Assert.assertFalse(filter.isStatementPrepareAfterLogEnabled());
    Assert.assertFalse(filter.isStatementPrepareCallAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetLogEnabled());
    Assert.assertFalse(filter.isResultSetLogErrorEnabled());
    Assert.assertFalse(filter.isResultSetCloseAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetNextAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetOpenAfterLogEnabled());
    // //
    // ////
    filter.setDataSourceLogger(new FakeLogger("_datasoure_", true));
    filter.setConnectionLogger(new FakeLogger("_connection_", true));
    filter.setStatementLogger(new FakeLogger("_statement_", true));
    filter.setResultSetLogger(new FakeLogger("_resultset_", true));
    Assert.assertFalse(filter.isConnectionLogEnabled());
    Assert.assertFalse(filter.isStatementLogEnabled());
    Assert.assertFalse(filter.isResultSetLogEnabled());
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    filter.setStatementLogErrorEnabled(true);
    Assert.assertTrue(filter.isStatementLogErrorEnabled());
    filter.setStatementLogErrorEnabled(false);
    filter.setConnectionLogEnabled(true);
    filter.setStatementLogEnabled(true);
    filter.setResultSetLogEnabled(true);
    Assert.assertFalse(filter.isDataSourceLogEnabled());
    Assert.assertFalse(filter.isConnectionLogErrorEnabled());
    Assert.assertFalse(filter.isConnectionConnectBeforeLogEnabled());
    Assert.assertFalse(filter.isConnectionConnectAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionCloseAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionCommitAfterLogEnabled());
    Assert.assertFalse(filter.isConnectionRollbackAfterLogEnabled());
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    Assert.assertFalse(filter.isStatementCreateAfterLogEnabled());
    Assert.assertFalse(filter.isStatementCloseAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteBatchAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteQueryAfterLogEnabled());
    Assert.assertFalse(filter.isStatementExecuteUpdateAfterLogEnabled());
    Assert.assertFalse(filter.isStatementLogErrorEnabled());
    Assert.assertFalse(filter.isStatementParameterSetLogEnabled());
    Assert.assertFalse(filter.isStatementPrepareAfterLogEnabled());
    Assert.assertFalse(filter.isStatementPrepareCallAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetLogErrorEnabled());
    Assert.assertFalse(filter.isResultSetCloseAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetNextAfterLogEnabled());
    Assert.assertFalse(filter.isResultSetOpenAfterLogEnabled());
}
Also used : CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter)

Example 2 with CommonsLogFilter

use of com.alibaba.druid.filter.logging.CommonsLogFilter in project druid by alibaba.

the class LogFilterTest method test_logFilter_1.

public void test_logFilter_1() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    log4jFilter.init(dataSource);
    config.getFilters().add(log4jFilter);
    setLogDisableAll(log4jFilter, true);
    CommonsLogFilter commonLogFilter = new CommonsLogFilter() {

        @Override
        public boolean isDataSourceLogEnabled() {
            return false;
        }

        @Override
        public boolean isConnectionLogEnabled() {
            return false;
        }

        @Override
        public boolean isStatementLogEnabled() {
            return false;
        }

        @Override
        public boolean isResultSetLogEnabled() {
            return false;
        }

        @Override
        public boolean isResultSetLogErrorEnabled() {
            return false;
        }

        @Override
        public boolean isResultSetNextAfterLogEnabled() {
            return false;
        }
    };
    commonLogFilter.init(dataSource);
    config.getFilters().add(commonLogFilter);
    setLogDisableAll(commonLogFilter, true);
    executeSQL(dataSource);
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter)

Example 3 with CommonsLogFilter

use of com.alibaba.druid.filter.logging.CommonsLogFilter in project druid by alibaba.

the class LogFilterTest method test_logFilter_0.

public void test_logFilter_0() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    log4jFilter.init(dataSource);
    config.getFilters().add(log4jFilter);
    setLogDisableAll(log4jFilter, true);
    CommonsLogFilter commonLogFilter = new CommonsLogFilter() {

        @Override
        public boolean isDataSourceLogEnabled() {
            return true;
        }

        @Override
        public boolean isConnectionLogEnabled() {
            return true;
        }

        @Override
        public boolean isStatementLogEnabled() {
            return true;
        }

        @Override
        public boolean isResultSetLogEnabled() {
            return true;
        }

        @Override
        public boolean isResultSetLogErrorEnabled() {
            return true;
        }

        @Override
        public boolean isResultSetNextAfterLogEnabled() {
            return true;
        }
    };
    commonLogFilter.init(dataSource);
    config.getFilters().add(commonLogFilter);
    setLogDisableAll(commonLogFilter, false);
    executeSQL(dataSource);
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter)

Example 4 with CommonsLogFilter

use of com.alibaba.druid.filter.logging.CommonsLogFilter in project druid by alibaba.

the class LogFilterTest method test_logFilter_2.

public void test_logFilter_2() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    {
        log4jFilter.init(dataSource);
        setLogDisableAll(log4jFilter, true);
        config.getFilters().add(log4jFilter);
    }
    CommonsLogFilter logFilter = new CommonsLogFilter();
    {
        logFilter.init(dataSource);
        setLogDisableAll(logFilter, true);
        config.getFilters().add(logFilter);
    }
    final MockResultSetMetaData rsMeta = new MockResultSetMetaData() {

        private int[] types = new int[] { Types.BLOB, Types.CLOB, Types.NCLOB, Types.BINARY, Types.OTHER };

        @Override
        public int getColumnCount() throws SQLException {
            return types.length;
        }

        @Override
        public int getColumnType(int column) throws SQLException {
            return types[column - 1];
        }
    };
    ConnectionProxy conn = (ConnectionProxy) dataSource.connect(new Properties());
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
    }
    {
        final MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                throw new SQLException();
            }
        };
        StatementProxy stmt = new StatementProxyImpl(conn, new MockStatement(conn) {

            public ResultSet getResultSet() throws SQLException {
                return rs;
            }
        }, 0);
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.statement_getResultSet(stmt);
    }
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        {
            logFilter.setResultSetLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
        {
            logFilter.setResultSetNextAfterLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
    }
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) Properties(java.util.Properties) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) StatementProxyImpl(com.alibaba.druid.proxy.jdbc.StatementProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) ResultSetMetaData(java.sql.ResultSetMetaData) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement)

Aggregations

CommonsLogFilter (com.alibaba.druid.filter.logging.CommonsLogFilter)4 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)3 MockDriver (com.alibaba.druid.mock.MockDriver)3 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)3 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)3 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 MockResultSet (com.alibaba.druid.mock.MockResultSet)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1 MockStatement (com.alibaba.druid.mock.MockStatement)1 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)1 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)1 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)1 StatementProxyImpl (com.alibaba.druid.proxy.jdbc.StatementProxyImpl)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1