Example usage for java.util.logging Logger addHandler

List of usage examples for java.util.logging Logger addHandler

Introduction

In this page you can find the example usage for java.util.logging Logger addHandler.

Prototype

public void addHandler(Handler handler) throws SecurityException 

Source Link

Document

Add a log Handler to receive logging messages.

Usage

From source file:net.technicpack.launcher.LauncherMain.java

private static void setupLogging(LauncherDirectories directories, ResourceLoader resources) {
    System.out.println("Setting up logging");
    final Logger logger = Utils.getLogger();
    File logDirectory = new File(directories.getLauncherDirectory(), "logs");
    if (!logDirectory.exists()) {
        logDirectory.mkdir();/*from  w w  w .j ava2  s  .  co  m*/
    }
    File logs = new File(logDirectory, "techniclauncher_%D.log");
    RotatingFileHandler fileHandler = new RotatingFileHandler(logs.getPath());

    fileHandler.setFormatter(new BuildLogFormatter(resources.getLauncherBuild()));

    for (Handler h : logger.getHandlers()) {
        logger.removeHandler(h);
    }
    logger.addHandler(fileHandler);
    logger.setUseParentHandlers(false);

    LauncherMain.consoleFrame = new ConsoleFrame(2500, resources.getImage("icon.png"));
    Console console = new Console(LauncherMain.consoleFrame, resources.getLauncherBuild());

    logger.addHandler(new ConsoleHandler(console));

    System.setOut(new PrintStream(new LoggerOutputStream(console, Level.INFO, logger), true));
    System.setErr(new PrintStream(new LoggerOutputStream(console, Level.SEVERE, logger), true));

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "Unhandled Exception in " + t, e);

            //                if (errorDialog == null) {
            //                    LauncherFrame frame = null;
            //
            //                    try {
            //                        frame = Launcher.getFrame();
            //                    } catch (Exception ex) {
            //                        //This can happen if we have a very early crash- before Launcher initializes
            //                    }
            //
            //                    errorDialog = new ErrorDialog(frame, e);
            //                    errorDialog.setVisible(true);
            //                }
        }
    });
}

From source file:com.surfs.nas.log.LogFactory.java

/**
 *
 *
 * @param filename/*ww  w. j  a  v a2s  .c om*/
 * @param log
 */
public static void configJDKLog(String filename, Logger log) {
    try {
        File f = new File(filename);
        if (!f.exists()) {
            return;
        }
        java.util.logging.LogManager.getLogManager().readConfiguration(new FileInputStream(f));
        java.util.logging.Logger logger = java.util.logging.LogManager.getLogManager().getLogger("");
        java.util.logging.Handler[] handlers = logger.getHandlers();
        for (java.util.logging.Handler h : handlers) {
            logger.removeHandler(h);
        }
        JdkLogHandler handle = new JdkLogHandler(log);
        logger.addHandler(handle);

    } catch (Exception e) {
    }
}

From source file:org.spoutcraft.launcher.Main.java

private static void setupLogger() {
    Logger logger = Logger.getLogger("launcher");
    File logDirectory = new File(Utils.getWorkingDirectory(), "logs");
    if (!logDirectory.exists()) {
        logDirectory.mkdir();/*from   www. j  a va 2  s.  c om*/
    }
    File logs = new File(logDirectory, "spoutcraft_%D.log");
    RotatingFileHandler fileHandler = new RotatingFileHandler(logs.getPath());

    fileHandler.setFormatter(new DateOutputFormatter(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")));

    for (Handler h : logger.getHandlers()) {
        logger.removeHandler(h);
    }
    logger.addHandler(fileHandler);

    Main.handler = fileHandler;

    logger.setUseParentHandlers(false);

    System.setOut(new PrintStream(new LoggerOutputStream(Level.INFO, logger), true));
    System.setErr(new PrintStream(new LoggerOutputStream(Level.SEVERE, logger), true));
    Main.logger = logger;
}

From source file:org.openhab.tools.analysis.checkstyle.test.KarafFeatureCheckTest.java

@Before
public void setup() {
    Logger system = Logger.getLogger("");
    system.addHandler(handler);
}

From source file:android.net.http.CookiesTest.java

/**
 * Test that we don't log potentially sensitive cookie values.
 * http://b/3095990//from w w  w .j a  v  a 2  s . com
 */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();

        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));

    } finally {
        logger.removeHandler(handler);
    }
}

From source file:org.apache.cxf.management.web.logging.atom.AbstractAtomBean.java

/**
 * Initializes bean; creates ATOM handler based on current properties state, and attaches handler to
 * logger(s)./*  ww  w. j  av  a2 s  .c om*/
 */
public void init() {
    checkInit();
    initialized = true;
    Handler h = createHandler();
    for (int i = 0; i < loggers.size(); i++) {
        Logger l = LogUtils.getL7dLogger(AbstractAtomBean.class, null, loggers.get(i).getLogger());
        l.addHandler(h);
        l.setLevel(LogLevel.toJUL(LogLevel.valueOf(loggers.get(i).getLevel())));
    }
}

From source file:de.ailis.microblinks.blinks.compiler.Main.java

/**
 * Setups logging.// w  w w . j av  a  2s  . co m
 */
private void setupLogging() {
    final Logger logger = Logger.getLogger("");
    for (final Handler handler : logger.getHandlers()) {
        logger.removeHandler(handler);
    }
    logger.addHandler(new LogHandler(progName));
}

From source file:com.diversityarrays.kdxplore.KDXplore.java

static private java.util.logging.Logger establishLogger(ApplicationFolder appFolder) {
    // Establish logger
    java.util.logging.Logger logger = null;

    try {/*from ww w. j  a  v a 2s.  c  o m*/
        File applicationFolder = appFolder.getApplicationFolder();

        logger = Shared.Log.getLogger();
        if (logger == null) {
            String kdxploreLog = appFolder.getApplicationName().toLowerCase() + ".log"; //$NON-NLS-1$
            File logFile = new File(applicationFolder, kdxploreLog);
            if (logFile.exists()) {
                File bakFile = new File(applicationFolder, kdxploreLog + ".bak"); //$NON-NLS-1$
                if (bakFile.exists()) {
                    bakFile.delete();
                }
                logFile.renameTo(bakFile);
            }
            java.util.logging.FileHandler fh = new FileHandler(kdxploreLog);
            fh.setFormatter(new SimpleFormatter());
            logger = java.util.logging.Logger.getLogger(appFolder.getApplicationName());
            logger.addHandler(fh);
            Shared.Log.setLogger(logger);

            logger.info("==== Log Started ===="); //$NON-NLS-1$
        }

        ExplorerProperties.getInstance(applicationFolder);
    } catch (IOException e1) {
        JOptionPane.showMessageDialog(null, e1.getMessage(), "Unable to initialise environment", //$NON-NLS-1$
                JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }

    return logger;
}

From source file:ryerson.daspub.Main.java

/**
 * Execute commands./*from  w ww . j a  v  a2 s  .  c o m*/
 */
@Override
public void run() {
    // show help message
    if (cmd.hasOption(CMD_HELP)) {
        showHelpMessage();
        System.exit(SUCCESS);
    }
    // load configuration
    if (cmd.hasOption(CMD_CONFIG)) {
        try {
            String path = cmd.getOptionValue(CMD_CONFIG);
            configFile = new File(path);
            config = Config.load(configFile);
        } catch (Exception ex) {
            String stack = ExceptionUtils.getStackTrace(ex);
            logger.log(Level.SEVERE, "Could not parse configuration file\n\n{0}", stack);
            System.exit(FAIL);
        }
    }
    // show gui or process command line options
    if (cmd.hasOption(CMD_GUI)) {
        ApplicationJFrame frame = ApplicationJFrame.getInstance();
        // send logging output to jtextarea
        JTextArea textArea = frame.getLogOutputTextArea();
        JTextAreaOutputHandler handler = new JTextAreaOutputHandler(textArea);
        handler.setFormatter(new JTextAreaOutputFormatter());
        handler.setLevel(Level.ALL);
        Logger rootlogger = LogManager.getLogManager().getLogger("");
        rootlogger.addHandler(handler);
        // load project if specified at command line
        if (configFile != null) {
            try {
                frame.openProject(configFile);
            } catch (Exception ex) {
                String stack = ExceptionUtils.getStackTrace(ex);
                logger.log(Level.SEVERE, "Could not load configuration file {0}\n\n{1}",
                        new Object[] { configFile.getAbsolutePath(), stack });
            }
        }
        // display the application frame
        frame.setVisible(true);
        logger.log(Level.INFO, "Ready");
    } else if (cmd.hasOption(CMD_INIT)) {
        executeInit();
    } else if (cmd.hasOption(CMD_PUBLISH)) {
        executePublish();
    }
}

From source file:ste.xtest.logging.BugFreeLoggingByteArrayOutputStream.java

private Logger getLog() {
    Logger log = Logger.getLogger("ste.xtest.logging");

    for (Handler h : log.getHandlers()) {
        log.removeHandler(h);/*from w  w  w  .ja  v  a2s .  com*/
    }

    log.addHandler(new ListLogHandler());

    return log;
}