Search in sources :

Example 56 with Handler

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.
}
Also used : CompactFormatter(org.yamcs.CompactFormatter) Handler(java.util.logging.Handler) Logger(java.util.logging.Logger)

Example 57 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)

Example 58 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 59 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 60 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)

Aggregations

Handler (java.util.logging.Handler)135 Logger (java.util.logging.Logger)52 ConsoleHandler (java.util.logging.ConsoleHandler)30 LogRecord (java.util.logging.LogRecord)24 Test (org.junit.Test)22 FileHandler (java.util.logging.FileHandler)17 File (java.io.File)14 IOException (java.io.IOException)13 Level (java.util.logging.Level)11 SimpleFormatter (java.util.logging.SimpleFormatter)8 Formatter (java.util.logging.Formatter)7 LogManager (java.util.logging.LogManager)6 PrintStream (java.io.PrintStream)5 ArrayList (java.util.ArrayList)5 SLF4JBridgeHandler (org.slf4j.bridge.SLF4JBridgeHandler)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 StringTokenizer (java.util.StringTokenizer)3 StdOutConsoleHandler (alma.acs.logging.StdOutConsoleHandler)2