Java slf4j Logger close(final AutoCloseable closeable, final Logger log)

Here you can find the source of close(final AutoCloseable closeable, final Logger log)

Description

Attempt to close the specified closeable and log any eventual error.

License

Apache License

Parameter

Parameter Description
closeable the closeable to close.
log the logger to use; if null no logging occurs.

Declaration

public static void close(final AutoCloseable closeable, final Logger log) 

Method Source Code


//package com.java2s;
/*//from w w w .jav a 2s  . c  om
 * Fowarder4j.
 * Copyright (C) 2015 Fowarder4j Team.
 * https://github.com/lolocohen/forwarder4j
 *
 * 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 org.slf4j.*;

public class Main {
    /**
     * Attempt to close the specified closeable without logging an eventual error.
     * @param closeable the closeable to close.
     * @throws Exception if any error occurs while closing the closeable.
     */
    public static void close(final AutoCloseable closeable) throws Exception {
        closeable.close();
    }

    /**
     * Attempt to close the specified closeable and log any eventual error.
     * @param closeable the closeable to close.
     * @param log the logger to use; if null no logging occurs.
     */
    public static void close(final AutoCloseable closeable, final Logger log) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                if (log != null) {
                    String s = "unable to close stream/reader/writer: " + getMessage(e);
                    if (log.isDebugEnabled())
                        log.debug(s, e);
                    else
                        log.warn(s);
                }
            }
        }
    }

    /**
     * Get the message of the specified <code>Throwable</code> along with its class name.
     * @param t the <code>Throwable</code> object from which to get the message.
     * @return a formatted message from the <code>Throwable</code>.
     */
    public static String getMessage(final Throwable t) {
        if (t == null)
            return "null";
        return t.getClass().getName() + ": " + t.getMessage();
    }
}

Related

  1. callShell(final String shellString, final Logger log)
  2. caught(Exception e, String... bizDesc)
  3. checkInterrupted(String type, String name, Logger logger)
  4. cleanLoggingMdc()
  5. clearRequestId()
  6. closeQuietly(AutoCloseable toClose, Logger log)
  7. configureXWikiLogs()
  8. configureXWikiLogs()
  9. countMSCandidates(byte[] byteARR)