Search in sources :

Example 11 with Handler

use of java.util.logging.Handler in project commons by twitter.

the class RootLogConfig method setVlog.

private static void setVlog(Logger logger, LogLevel logLevel) {
    final Level newLevel = logLevel.getLevel();
    logger.setLevel(newLevel);
    do {
        for (Handler handler : logger.getHandlers()) {
            Level handlerLevel = handler.getLevel();
            if (newLevel.intValue() < handlerLevel.intValue()) {
                handler.setLevel(newLevel);
            }
        }
    } while (logger.getUseParentHandlers() && (logger = logger.getParent()) != null);
}
Also used : ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) Level(java.util.logging.Level)

Example 12 with Handler

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

the class ReporterHandler method publish.

@Override
public void publish(LogRecord record) {
    if (record.getThrown() == null) {
        return;
    }
    throwable = record.getThrown();
    if (throwable != null && throwable instanceof OutOfMemoryError) {
        Handler[] handlers = Logger.getLogger("").getHandlers();
        for (int i = 0; i < handlers.length; i++) {
            Handler h = handlers[i];
            h.close();
        }
        NotifyDescriptor nd = new NotifyDescriptor.Message(MEMORY_ERROR, NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(nd);
        LifecycleManager.getDefault().exit();
    }
}
Also used : NotifyDescriptor(org.openide.NotifyDescriptor) Handler(java.util.logging.Handler)

Example 13 with Handler

use of java.util.logging.Handler in project guice by google.

the class ProviderMethodsTest method validateNullableWarns.

private void validateNullableWarns(Injector injector, Dependency<?> dependency) {
    final List<LogRecord> logRecords = Lists.newArrayList();
    final Handler fakeHandler = new Handler() {

        @Override
        public void publish(LogRecord logRecord) {
            logRecords.add(logRecord);
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    };
    Logger.getLogger(Guice.class.getName()).addHandler(fakeHandler);
    try {
        // no exception, but assert it does log.
        injector.getInstance(Integer.class);
        LogRecord record = Iterables.getOnlyElement(logRecords);
        assertEquals("Guice injected null into {0} (a {1}), please mark it @Nullable." + " Use -Dguice_check_nullable_provides_params=ERROR to turn this into an" + " error.", record.getMessage());
        assertEquals(Errors.convert(dependency.getKey()), record.getParameters()[1]);
    } finally {
        Logger.getLogger(Guice.class.getName()).removeHandler(fakeHandler);
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Handler(java.util.logging.Handler)

Example 14 with Handler

use of java.util.logging.Handler in project grpc-java by grpc.

the class ContextTest method exceptionOfExecutorDoesntThrow.

@Test
public void exceptionOfExecutorDoesntThrow() {
    final AtomicReference<Throwable> loggedThrowable = new AtomicReference<Throwable>();
    Handler logHandler = new Handler() {

        @Override
        public void publish(LogRecord record) {
            Throwable thrown = record.getThrown();
            if (thrown != null) {
                if (loggedThrowable.get() == null) {
                    loggedThrowable.set(thrown);
                } else {
                    loggedThrowable.set(new RuntimeException("Too many exceptions", thrown));
                }
            }
        }

        @Override
        public void close() {
        }

        @Override
        public void flush() {
        }
    };
    Logger logger = Logger.getLogger(Context.class.getName());
    logger.addHandler(logHandler);
    try {
        Context.CancellableContext base = Context.current().withCancellation();
        final AtomicReference<Runnable> observed1 = new AtomicReference<Runnable>();
        final Error err = new Error();
        base.addListener(cancellationListener, new Executor() {

            @Override
            public void execute(Runnable runnable) {
                observed1.set(runnable);
                throw err;
            }
        });
        assertNull(observed1.get());
        assertNull(loggedThrowable.get());
        base.cancel(null);
        assertNotNull(observed1.get());
        assertSame(err, loggedThrowable.get());
        final Error err2 = new Error();
        loggedThrowable.set(null);
        final AtomicReference<Runnable> observed2 = new AtomicReference<Runnable>();
        base.addListener(cancellationListener, new Executor() {

            @Override
            public void execute(Runnable runnable) {
                observed2.set(runnable);
                throw err2;
            }
        });
        assertNotNull(observed2.get());
        assertSame(err2, loggedThrowable.get());
    } finally {
        logger.removeHandler(logHandler);
    }
}
Also used : Handler(java.util.logging.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Logger(java.util.logging.Logger) Executor(java.util.concurrent.Executor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) LogRecord(java.util.logging.LogRecord) Test(org.junit.Test)

Example 15 with Handler

use of java.util.logging.Handler in project jdk8u_jdk by JetBrains.

the class LoggingExceptionTest method main.

public static void main(String[] args) {
    Handler handler = new ConsoleHandler();
    Logger logger = Logger.getLogger("javax.management.modelmbean");
    logger.addHandler(handler);
    logger.setLevel(Level.FINEST);
    try {
        for (int i = 0; i < tests.length; i++) {
            System.out.println(">>> DescriptorSupportLoggingTest: Test Case " + i);
            DescriptorSupport ds;
            String msg = "Instantiate " + tests[i];
            System.out.println(msg);
            switch(i) {
                case 0:
                    ds = new DescriptorSupport();
                    break;
                case 1:
                    ds = new DescriptorSupport(10);
                    break;
                case 2:
                    ds = new DescriptorSupport(new DescriptorSupport().toXMLString());
                    break;
                case 3:
                    ds = new DescriptorSupport("name1=value1", "name2=value2");
                    break;
                case 4:
                    ds = new DescriptorSupport(new String[] { "name" }, new Object[] { "value" });
                    break;
                case 5:
                    ds = new DescriptorSupport(new DescriptorSupport());
                    break;
                case 6:
                    RequiredModelMBean mbean = new RequiredModelMBean();
                    NotificationListener nl = new NotificationListener() {

                        public void handleNotification(Notification notification, Object handback) {
                        }
                    };
                    mbean.addAttributeChangeNotificationListener(nl, null, null);
                    break;
                default:
                    throw new AssertionError();
            }
            System.out.println(msg + " OK");
        }
    } catch (Exception e) {
        System.out.println("Got unexpected exception = " + e);
        String msg = "Test FAILED!";
        System.out.println(msg);
        throw new IllegalArgumentException(msg);
    }
    System.out.println("Test PASSED!");
}
Also used : ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) Logger(java.util.logging.Logger) ConsoleHandler(java.util.logging.ConsoleHandler) Notification(javax.management.Notification) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) NotificationListener(javax.management.NotificationListener)

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