Java Log Exception logException(Logger logger, Level logLevel, Exception e)

Here you can find the source of logException(Logger logger, Level logLevel, Exception e)

Description

Logs an exception with the given logger and the given level.

License

Apache License

Parameter

Parameter Description
logger the logger
e an exception
logLevel the log level (see Level )

Declaration

public static void logException(Logger logger, Level logLevel, Exception e) 

Method Source Code

//package com.java2s;
/**//from   ww  w  . j av  a  2 s  . c om
 * Copyright 2013-2015 Linagora, Universit? Joseph Fourier, Floralis
 *
 * The present code is developed in the scope of the joint LINAGORA -
 * Universit? Joseph Fourier - Floralis research program and is designated
 * as a "Result" pursuant to the terms and conditions of the LINAGORA
 * - Universit? Joseph Fourier - Floralis research program. Each copyright
 * holder of Results enumerated here above fully & independently holds complete
 * ownership of the complete Intellectual Property rights applicable to the whole
 * of said Results, and may freely exploit it in any manner which does not infringe
 * the moral rights of the other copyright holders.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.PrintWriter;

import java.io.StringWriter;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
    /**
     * Logs an exception with the given logger and the given level.
     * <p>
     * Writing a stack trace may be time-consuming in some environments.
     * To prevent useless computing, this method checks the current log level
     * before trying to log anything.
     * </p>
     *
     * @param logger the logger
     * @param e an exception
     * @param logLevel the log level (see {@link Level})
     */
    public static void logException(Logger logger, Level logLevel, Exception e) {

        if (logger.isLoggable(logLevel))
            logger.log(logLevel, writeException(e));
    }

    /**
     * Logs an exception with the given logger and the FINEST level.
     * @param logger the logger
     * @param e an exception
     */
    public static void logException(Logger logger, Exception e) {
        logException(logger, Level.FINEST, e);
    }

    /**
     * Writes an exception's stack trace into a string.
     * <p>
     * This method used to be public.<br />
     * Its visibility was reduced to promote {@link #logException(Logger, Exception)},
     * which has better performances.
     * </p>
     *
     * @param e an exception (not null)
     * @return a string
     */
    static String writeException(Exception e) {

        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));

        return sw.toString();
    }
}

Related

  1. logError(String s, Throwable e)
  2. logException(Exception e, Logger logger)
  3. LogException(Exception ex)
  4. logException(final Throwable exception)
  5. logException(int type, String RID, String subRID, String propIndication, String error)
  6. logException(Logger logger, String message, Throwable t)
  7. logException(Logger logger, Throwable t)
  8. LogException(String description, Throwable e)
  9. logException(String msg, Exception ex)