Java Log to Stream logToStream(OutputStream o)

Here you can find the source of logToStream(OutputStream o)

Description

Adds a handler that will log all messages to a given Output Stream.

License

Open Source License

Parameter

Parameter Description
o The OutputStream to which to log.

Declaration

public static void logToStream(OutputStream o) 

Method Source Code


//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 * /*from   w w  w  . ja  v  a  2  s  .c  o m*/
 * Copyright (c) 2011 Colin J. Fuller
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * ***** END LICENSE BLOCK ***** */

import java.io.OutputStream;
import java.util.logging.*;

public class Main {
    static java.util.List<Handler> addedHandlers;
    public static final String LOGGER_NAME = "edu.stanford.cfuller.imageanalysistools";

    /**
     * Adds a handler that will log all messages to a given Output Stream.
     * @param o     The OutputStream to which to log.
     */
    public static void logToStream(OutputStream o) {

        StreamHandler sh = new StreamHandler(o, new SimpleFormatter());
        addedHandlers.add(sh);

        getLogger().addHandler(sh);

    }

    /**
     * Adds a handler to the logger for this package.
     * @param h The handler to add.
     */
    public static void addHandler(Handler h) {

        if (!addedHandlers.contains(h)) {

            addedHandlers.add(h);
            getLogger().addHandler(h);

        }

    }

    /**
     * Get a reference to the underlying logger used by this class.
     * @return  A reference to the logger.
     */
    public static Logger getLogger() {
        return java.util.logging.Logger.getLogger(LOGGER_NAME);
    }
}

Related

  1. logCallStack(PrintStream out)