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");
}
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()]);
}
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
}
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"));
}
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());
}
Aggregations