use of java.util.logging.Handler in project yamcs-studio by yamcs.
the class Application method configureLogging.
private void configureLogging() {
Logger root = Logger.getLogger("");
// We use the convention where INFO goes to end-user (via 'Console View' inside Yamcs Studio)
// And FINE goes to stdout (--> debuggable by end-user if needed, and visible in PDE/UI)
// By default only allow WARNING messages
root.setLevel(Level.WARNING);
// Exceptions only for plugins that do not flood the Console View with INFO messages:
Logger.getLogger("com.spaceapplications").setLevel(Level.FINE);
Logger.getLogger("org.csstudio").setLevel(Level.FINE);
Logger.getLogger("org.yamcs.studio").setLevel(Level.FINE);
// At this point in the startup there should be only one handler (for stdout)
for (Handler handler : root.getHandlers()) {
handler.setLevel(Level.FINE);
handler.setFormatter(new CompactFormatter());
}
// A second handler will be created by the workbench window advisor when the ConsoleView
// is available.
}
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());
}
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 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
}
Aggregations