Search in sources :

Example 1 with LoggerCreator

use of org.apache.openejb.log.LoggerCreator in project tomee by apache.

the class LoggingSqlTest method checkOutput.

@Test
public void checkOutput() throws Exception {
    final Logger logger = Logger.getInstance(LogCategory.OPENEJB_SQL, LoggingPreparedSqlStatement.class);
    final JuliLogStream stream = JuliLogStream.class.cast(Reflections.get(logger, "logStream"));
    final LoggerCreator julCreator = LoggerCreator.class.cast(Reflections.get(stream, "logger"));
    final java.util.logging.Logger actualLogger = julCreator.call();
    final Collection<String> msgs = new LinkedList<>();
    final Handler handler = new Handler() {

        @Override
        public void publish(final LogRecord record) {
            msgs.add(record.getMessage());
        }

        @Override
        public void flush() {
        // no-op
        }

        @Override
        public void close() throws SecurityException {
        // no-op
        }
    };
    actualLogger.addHandler(handler);
    final Connection c = ds.getConnection();
    final PreparedStatement preparedStatement = c.prepareStatement("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS");
    preparedStatement.execute();
    preparedStatement.close();
    c.close();
    actualLogger.removeHandler(handler);
    assertEquals(1, msgs.size());
    final String msg = msgs.iterator().next();
    assertTrue(msg.contains("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS"));
    assertTrue(msg.contains("stack: -> org.apache.openejb.resource.jdbc.logging.LoggingSqlTest.checkOutput:83"));
}
Also used : JuliLogStream(org.apache.openejb.util.JuliLogStream) Connection(java.sql.Connection) Handler(java.util.logging.Handler) PreparedStatement(java.sql.PreparedStatement) Logger(org.apache.openejb.util.Logger) LinkedList(java.util.LinkedList) LoggerCreator(org.apache.openejb.log.LoggerCreator) LogRecord(java.util.logging.LogRecord) Test(org.junit.Test)

Example 2 with LoggerCreator

use of org.apache.openejb.log.LoggerCreator in project tomee by apache.

the class LoggingJAXRSCommons method getLooger.

protected java.util.logging.Logger getLooger() throws Exception {
    final Logger logger = Logger.getInstance(LogCategory.OPENEJB_RS, RESTService.class);
    final LogStreamAsync stream = LogStreamAsync.class.cast(Reflections.get(logger, "logStream"));
    final JuliLogStream ls = JuliLogStream.class.cast(Reflections.get(stream, "ls"));
    final LoggerCreator julCreator = LoggerCreator.class.cast(Reflections.get(ls, "logger"));
    return julCreator.call();
}
Also used : LoggerCreator(org.apache.openejb.log.LoggerCreator) JuliLogStream(org.apache.openejb.util.JuliLogStream) Logger(org.apache.openejb.util.Logger) LogStreamAsync(org.apache.openejb.util.LogStreamAsync)

Aggregations

LoggerCreator (org.apache.openejb.log.LoggerCreator)2 JuliLogStream (org.apache.openejb.util.JuliLogStream)2 Logger (org.apache.openejb.util.Logger)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 LinkedList (java.util.LinkedList)1 Handler (java.util.logging.Handler)1 LogRecord (java.util.logging.LogRecord)1 LogStreamAsync (org.apache.openejb.util.LogStreamAsync)1 Test (org.junit.Test)1