Search in sources :

Example 96 with Handler

use of java.util.logging.Handler in project karaf by apache.

the class BootstrapLogManagerTest method testGetLogManager.

@Test
public void testGetLogManager() {
    new File("target/log/karaf.log").delete();
    BootstrapLogManager.setProperties(getConfigProperties());
    Handler handler = BootstrapLogManager.getDefaultHandler();
    Assert.assertNotNull(handler);
    assertExists("target/log/karaf.log");
}
Also used : Handler(java.util.logging.Handler) File(java.io.File) Test(org.junit.Test)

Example 97 with Handler

use of java.util.logging.Handler in project tomee by apache.

the class Log4jLogger method getHandlers.

public synchronized Handler[] getHandlers() {
    final List<Handler> ret = new ArrayList<Handler>();
    final Enumeration<?> en = log.getAllAppenders();
    while (en.hasMoreElements()) {
        final Appender ap = (Appender) en.nextElement();
        if (ap instanceof HandlerWrapper) {
            ret.add(((HandlerWrapper) ap).getHandler());
        }
    }
    return ret.toArray(new Handler[ret.size()]);
}
Also used : Appender(org.apache.log4j.Appender) ArrayList(java.util.ArrayList) Handler(java.util.logging.Handler)

Example 98 with Handler

use of java.util.logging.Handler in project tomee by apache.

the class Slf4jLogger method internalLogFormatted.

@Override
protected void internalLogFormatted(final String msg, final LogRecord record) {
    final Level level = record.getLevel();
    final Throwable t = record.getThrown();
    final Handler[] targets = getHandlers();
    if (targets != null) {
        for (final Handler h : targets) {
            h.publish(record);
        }
    }
    if (!getUseParentHandlers()) {
        return;
    }
    /*
        * As we can not use a "switch ... case" block but only a "if ... else if ..." block, the order of the
        * comparisons is important. We first try log level FINE then INFO, WARN, FINER, etc
        */
    if (Level.FINE.equals(level)) {
        if (locationAwareLogger == null) {
            logger.debug(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
        }
    } else if (Level.INFO.equals(level)) {
        if (locationAwareLogger == null) {
            logger.info(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.INFO_INT, msg, null, t);
        }
    } else if (Level.WARNING.equals(level)) {
        if (locationAwareLogger == null) {
            logger.warn(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, t);
        }
    } else if (Level.FINER.equals(level)) {
        if (locationAwareLogger == null) {
            logger.trace(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
        }
    } else if (Level.FINEST.equals(level)) {
        if (locationAwareLogger == null) {
            logger.trace(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.TRACE_INT, msg, null, t);
        }
    } else if (Level.ALL.equals(level)) {
        // but not accessible by the API Logger.xxx() API
        if (locationAwareLogger == null) {
            logger.error(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
        }
    } else if (Level.SEVERE.equals(level)) {
        if (locationAwareLogger == null) {
            logger.error(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
        }
    } else if (Level.CONFIG.equals(level)) {
        if (locationAwareLogger == null) {
            logger.debug(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
        }
    }
// don't log if Level.OFF
}
Also used : Handler(java.util.logging.Handler) Level(java.util.logging.Level)

Example 99 with Handler

use of java.util.logging.Handler 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 100 with Handler

use of java.util.logging.Handler in project tomee by apache.

the class AppDataSourceTest method run.

@Test
public void run() throws OpenEJBException, NamingException, IOException {
    final File tempWar = new File("target/AppDataSourceTest");
    tempWar.mkdirs();
    new File(tempWar, "WEB-INF").mkdirs();
    IO.writeString(new File(tempWar, "WEB-INF/resources.xml"), "<resources>\n" + "<Resource id=\"java:app/gace/MyDS\" type=\"DataSource\">\n" + "DataSourceCreator=dbcp\n" + "</Resource>\n" + "</resources>\n");
    final Collection<LogRecord> records = new ArrayList<>();
    try (final Container c = new Container(new Configuration().randomHttpPort())) {
        Jdk14Logger.class.cast(LogFactory.getLog(BasicDataSource.class)).getLogger().addHandler(new Handler() {

            @Override
            public void publish(final LogRecord record) {
                if (record.getLevel() == Level.SEVERE || record.getLevel() == Level.WARNING) {
                    records.add(record);
                }
            }

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

            @Override
            public void close() throws SecurityException {
            // no-op
            }
        });
        c.deploy(null, tempWar);
    }
    // if we have the JMX bug of dbcp2 integration (in 7.0.0) then we have a WARNING record from BasicDataSource.close()
    // saying:
    // Failed to unregister the JMX name:
    //     Tomcat:type=DataSource,host=localhost,context=/AppDataSourceTest,class=javax.sql.DataSource,name="openejb/Resource/AppDataSourceTest/app/gace/MyDS"
    assertTrue(records.isEmpty());
}
Also used : LogRecord(java.util.logging.LogRecord) ArrayList(java.util.ArrayList) Handler(java.util.logging.Handler) File(java.io.File) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Test(org.junit.Test)

Aggregations

Handler (java.util.logging.Handler)108 Logger (java.util.logging.Logger)35 ConsoleHandler (java.util.logging.ConsoleHandler)22 LogRecord (java.util.logging.LogRecord)16 Test (org.junit.Test)15 FileHandler (java.util.logging.FileHandler)13 IOException (java.io.IOException)9 File (java.io.File)8 Level (java.util.logging.Level)7 SimpleFormatter (java.util.logging.SimpleFormatter)7 Formatter (java.util.logging.Formatter)6 LogManager (java.util.logging.LogManager)6 PrintStream (java.io.PrintStream)5 ArrayList (java.util.ArrayList)4 SLF4JBridgeHandler (org.slf4j.bridge.SLF4JBridgeHandler)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 StringTokenizer (java.util.StringTokenizer)3 StdOutConsoleHandler (alma.acs.logging.StdOutConsoleHandler)2 ConsoleLogFormatter (alma.acs.logging.formatters.ConsoleLogFormatter)2 MonitoredResource (com.google.cloud.MonitoredResource)2