All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperledger.fabric.Logger Maven / Gradle / Ivy

There is a newer version: 2.5.3
Show newest version
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.fabric;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.LogManager;

/**
 * Logger class to use throughout the Contract Implementation
 *
 */
public class Logger extends java.util.logging.Logger {

    protected Logger(String name) {
        super(name, null);

        // ensure that the parent logger is set
        this.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
    }

    public static Logger getLogger(String name) {
        return new Logger(name);
    }

    public void debug(Supplier msgSupplier) {
        log(Level.FINEST, msgSupplier);
    }

    public void debug(String msg) {
        log(Level.FINEST, msg);
    }

    public static Logger getLogger(Class class1) {
        // important to add the logger to the log manager
        Logger l = Logger.getLogger(class1.getName());
        LogManager.getLogManager().addLogger(l);
        return l;
    }

    public void error(String message) {
        log(Level.SEVERE, message);
    }

    public void error(Supplier msgSupplier) {
        log(Level.SEVERE, msgSupplier);
    }

    public String formatError(Throwable throwable) {
        if (throwable == null)
            return null;
        final StringWriter buffer = new StringWriter();
        buffer.append(throwable.getMessage());
        throwable.printStackTrace(new PrintWriter(buffer));

        Throwable cause = throwable.getCause();
        if (cause != null) {
            buffer.append(".. caused by ..");
            buffer.append(this.formatError(cause));
        }

        return buffer.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy