Search in sources :

Example 1 with Handler

use of java.util.logging.Handler in project jetty.project by eclipse.

the class JavaUtilLogTest method restoreJUL.

@AfterClass
public static void restoreJUL() {
    LogManager lmgr = LogManager.getLogManager();
    java.util.logging.Logger root = lmgr.getLogger("");
    // Remove test handlers
    for (Handler existing : root.getHandlers()) {
        root.removeHandler(existing);
    }
    // Restore original handlers
    for (Handler original : originalHandlers) {
        root.addHandler(original);
    }
}
Also used : Handler(java.util.logging.Handler) LogManager(java.util.logging.LogManager) AfterClass(org.junit.AfterClass)

Example 2 with Handler

use of java.util.logging.Handler in project OpenGrok by OpenGrok.

the class LoggerUtil method setFileHandlerLogPath.

public static void setFileHandlerLogPath(String path) throws IOException {
    if (path != null) {
        File jlp = new File(path);
        if (!jlp.exists() && !jlp.mkdirs()) {
            throw new IOException("could not make logpath: " + jlp.getAbsolutePath());
        }
    }
    StringBuilder logfile = new StringBuilder();
    logfile.append(path == null ? "%t" : path);
    logfile.append(File.separatorChar).append("opengrok%g.%u.log");
    for (Handler handler : getBaseLogger().getHandlers()) {
        if (handler instanceof FileHandler) {
            FileHandler fileHandler = (FileHandler) handler;
            FileHandler newFileHandler;
            try {
                int logFilesSizeLimit = loggerIntProperty("java.util.logging.FileHandler.limit", DEFAULT_FILEHANDLER_LIMIT);
                int logFilesCount = loggerIntProperty("java.util.logging.FileHandler.count", DEFAULT_FILEHANDLER_COUNT);
                newFileHandler = new FileHandler(logfile.toString(), logFilesSizeLimit, logFilesCount);
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Cannot create new logger FileHandler: " + logfile.toString(), e);
                return;
            }
            String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
            newFileHandler.setLevel(fileHandler.getLevel());
            try {
                newFileHandler.setFormatter((Formatter) Class.forName(formatter).newInstance());
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                newFileHandler.setFormatter(new FileLogFormatter());
            }
            getBaseLogger().addHandler(newFileHandler);
            getBaseLogger().removeHandler(fileHandler);
            loggerFile = logfile.toString();
        }
    }
}
Also used : FileLogFormatter(org.opensolaris.opengrok.logger.formatter.FileLogFormatter) FileHandler(java.util.logging.FileHandler) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) IOException(java.io.IOException) File(java.io.File) FileHandler(java.util.logging.FileHandler)

Example 3 with Handler

use of java.util.logging.Handler in project jersey by jersey.

the class ResourceBundleTest method testBadResource.

@Test
public void testBadResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(BadResource.class);
    ByteArrayOutputStream logOutput = new ByteArrayOutputStream();
    Handler logHandler = new StreamHandler(logOutput, new SimpleFormatter());
    GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
    // TODO: there should be a better way to get the log output!
    final Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames();
    while (loggerNames.hasMoreElements()) {
        String name = loggerNames.nextElement();
        if (name.startsWith("org.glassfish")) {
            LogManager.getLogManager().getLogger(Errors.class.getName()).addHandler(logHandler);
        }
    }
    GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
    logOutput.flush();
    final String logOutputAsString = logOutput.toString();
    Assert.assertFalse(logOutputAsString.contains("[failed to localize]"));
    Assert.assertTrue(logOutputAsString.contains("BadResource"));
}
Also used : StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) StreamHandler(java.util.logging.StreamHandler) Handler(java.util.logging.Handler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 4 with Handler

use of java.util.logging.Handler in project hudson-2.x by hudson.

the class WeakLogHandler method setErrorManager.

@Override
public void setErrorManager(ErrorManager em) {
    super.setErrorManager(em);
    Handler t = resolve();
    if (t != null)
        t.setErrorManager(em);
}
Also used : Handler(java.util.logging.Handler)

Example 5 with Handler

use of java.util.logging.Handler in project randomizedtesting by randomizedtesting.

the class WithNestedTestClass method setupNested.

@BeforeClass
public static final void setupNested() throws IOException {
    runningNested = true;
    zombieToken = new Object();
    // capture sysout/ syserr.
    sw = new StringWriter();
    sysout = System.out;
    syserr = System.err;
    System.setOut(new PrintStream(new TeeOutputStream(System.out, new WriterOutputStream(sw))));
    System.setErr(new PrintStream(new TeeOutputStream(System.err, new WriterOutputStream(sw))));
    // Add custom logging handler because java logging keeps a reference to previous System.err.
    loggingMessages = new StringWriter();
    logger = Logger.getLogger("");
    handlers = logger.getHandlers();
    for (Handler h : handlers) logger.removeHandler(h);
    logger.addHandler(new Handler() {

        final SimpleFormatter formatter = new SimpleFormatter();

        @Override
        public void publish(LogRecord record) {
            loggingMessages.write(formatter.format(record) + "\n");
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    });
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) Handler(java.util.logging.Handler) BeforeClass(org.junit.BeforeClass)

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