Search in sources :

Example 81 with Handler

use of java.util.logging.Handler in project jmonkeyengine by jMonkeyEngine.

the class TestCanvas method main.

public static void main(String[] args) {
    JmeFormatter formatter = new JmeFormatter();
    Handler consoleHandler = new ConsoleHandler();
    consoleHandler.setFormatter(formatter);
    Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]);
    Logger.getLogger("").addHandler(consoleHandler);
    createCanvas(appClass);
    try {
        Thread.sleep(500);
    } catch (InterruptedException ex) {
    }
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
            createFrame();
            currentPanel.add(canvas, BorderLayout.CENTER);
            frame.pack();
            startApp();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}
Also used : JmeFormatter(com.jme3.util.JmeFormatter) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) ConsoleHandler(java.util.logging.ConsoleHandler)

Example 82 with Handler

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

the class LoggerTest method test_initHandler.

/*
	 * Test whether privileged code is used to load resource bundles.
	 *
	public void testLoadResourceBundle() {
        //
		SecurityManager oldMan = System.getSecurityManager();
		System.setSecurityManager(new MockNoLoadingClassSecurityManager());
		try {
			Logger.getAnonymousLogger(VALID_RESOURCE_BUNDLE);
		} finally {
			System.setSecurityManager(oldMan);
		}
	}

    public void testLoadResourceBundleNonExistent() {
        try {
            // Try a load a non-existent resource bundle.
            LoggerExtension.loadResourceBundle("missinglogger.properties");
            fail("Expected an exception.");
        } catch (MissingResourceException ex) {
            // Expected exception is precisely a MissingResourceException
            assertTrue(ex.getClass() == MissingResourceException.class);
        }
    }

    /**
     * @tests java.util.logging.Logger#logrb(Level, String, String, String,
     *        String, Object)
     *
    public void test_init_logger()
            throws Exception {
        Properties p = new Properties();
        p.put("testGetLogger_Normal_ANewLogger2.level", "ALL");
        LogManager.getLogManager().readConfiguration(
                EnvironmentHelper.PropertiesToInputStream(p));

        assertNull(LogManager.getLogManager().getLogger(
                "testGetLogger_Normal_ANewLogger2"));
        SecurityManager originalSecurityManager = System.getSecurityManager();
        try {
            System.setSecurityManager(new SecurityManager());
            // should not throw expection
            Logger logger = Logger.getLogger("testGetLogger_Normal_ANewLogger2");
            // should thrpw exception
            try{
                logger.setLevel(Level.ALL);
                fail("should throw SecurityException");
            } catch (SecurityException e){
                // expected
            }
            try{
                logger.setParent(Logger.getLogger("root"));
                fail("should throw SecurityException");
            } catch (SecurityException e){
                // expected
            }
        } finally {
            System.setSecurityManager(originalSecurityManager);
        }
    }

    /*
     * test initHandler
     */
public void test_initHandler() throws Exception {
    InputStream logProps = ClassLoader.getSystemResourceAsStream(LOGGING_CONFIG_FILE);
    LogManager lm = LogManager.getLogManager();
    lm.readConfiguration(logProps);
    Logger log = Logger.getLogger("");
    // can log properly
    Handler[] handlers = log.getHandlers();
    assertEquals(2, handlers.length);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Handler(java.util.logging.Handler) Logger(java.util.logging.Logger) LogManager(java.util.logging.LogManager)

Example 83 with Handler

use of java.util.logging.Handler in project meteo by pierre.

the class SetupJULBridge method contextInitialized.

@Override
public void contextInitialized(final ServletContextEvent event) {
    // we first remove the default handler(s)
    final Logger rootLogger = LogManager.getLogManager().getLogger("");
    final Handler[] handlers = rootLogger.getHandlers();
    if (!ArrayUtils.isEmpty(handlers)) {
        for (final Handler handler : handlers) {
            rootLogger.removeHandler(handler);
        }
    }
    // and then we let jul-to-sfl4j do its magic so that jersey messages go to sfl4j (and thus log4j)
    SLF4JBridgeHandler.install();
    log.info("Assimilated java.util Logging");
}
Also used : Handler(java.util.logging.Handler) SLF4JBridgeHandler(org.slf4j.bridge.SLF4JBridgeHandler) Logger(java.util.logging.Logger)

Example 84 with Handler

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

the class LoggingHelper method loggerInit.

/**
   * Init java util logging
   *
   * @param level the Level of message to log
   * @param isRedirectStdOutErr whether we redirect std out&err
   * @param format the format to log
   */
public static void loggerInit(Level level, boolean isRedirectStdOutErr, String format) throws IOException {
    // Set the java util logging format
    setLoggingFormat(format);
    // Configure the root logger and its handlers so that all the
    // derived loggers will inherit the properties
    Logger rootLogger = Logger.getLogger("");
    for (Handler handler : rootLogger.getHandlers()) {
        handler.setLevel(level);
    }
    rootLogger.setLevel(level);
    if (rootLogger.getLevel().intValue() < Level.WARNING.intValue()) {
        // zookeeper logging scares me. if people want this, we can patch to config-drive this
        Logger.getLogger("org.apache.zookeeper").setLevel(Level.WARNING);
    }
    if (isRedirectStdOutErr) {
        // System.err to Logger, it results in an infinite loop.
        for (Handler handler : rootLogger.getHandlers()) {
            if (handler instanceof ConsoleHandler) {
                rootLogger.removeHandler(handler);
            }
        }
        // now rebind stdout/stderr to logger
        Logger logger;
        LoggingOutputStream los;
        logger = Logger.getLogger("stdout");
        los = new LoggingOutputStream(logger, StdOutErrLevel.STDOUT);
        System.setOut(new PrintStream(los, true));
        logger = Logger.getLogger("stderr");
        los = new LoggingOutputStream(logger, StdOutErrLevel.STDERR);
        System.setErr(new PrintStream(los, true));
    }
}
Also used : PrintStream(java.io.PrintStream) FileHandler(java.util.logging.FileHandler) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) Logger(java.util.logging.Logger) ConsoleHandler(java.util.logging.ConsoleHandler)

Example 85 with Handler

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

the class RootLogConfigTest method setUp.

@Before
public void setUp() {
    // Intercept stderr (this must be done first).
    fakeErrorLog = new ByteArrayOutputStream();
    errPrintStream = new PrintStream(fakeErrorLog);
    System.setErr(errPrintStream);
    // Create other members
    consoleHandler = new ConsoleHandler();
    fakeFileLog = new FakeHandler();
    // Emulate default setup (just a console handler), but avoiding the use
    // of the global root logger so not to get a flaky test due to background threads.
    fakeRootLogger = Logger.getLogger("FakeRoot-" + UUID.randomUUID().toString());
    fakeRootLogger.setUseParentHandlers(false);
    for (Handler h : fakeRootLogger.getHandlers()) {
        fakeRootLogger.removeHandler(h);
    }
    fakeRootLogger.addHandler(consoleHandler);
    testLogger = Logger.getLogger(RootLogConfigTest.class.getName());
    testLogger.setUseParentHandlers(true);
    testLogger.setParent(fakeRootLogger);
    setFakeRootForLogger(ClassA.logger);
    setFakeRootForLogger(ClassB.logger);
    resetLogs();
}
Also used : PrintStream(java.io.PrintStream) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConsoleHandler(java.util.logging.ConsoleHandler) Before(org.junit.Before)

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