Java slf4j Logger logBytes(byte[] data, int length, String prefix, Logger logger)

Here you can find the source of logBytes(byte[] data, int length, String prefix, Logger logger)

Description

Logs sent and received bytes for debugging

License

Open Source License

Parameter

Parameter Description
data Bytes sent/received
length Number of Bytes sent/received
prefix Logging prefix

Declaration

public static void logBytes(byte[] data, int length, String prefix, Logger logger) 

Method Source Code

//package com.java2s;
/*//  w w  w  . ja v  a 2 s  .  c o m
 * Copyright 2012-13 Fraunhofer ISE
 *
 * This file is part of jDLMS.
 * For more information visit http://www.openmuc.org
 *
 * jDLMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * jDLMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with jDLMS.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class Main {
    private static Marker COMMUNICATION = MarkerFactory.getMarker("Communication");

    /**
     * Logs sent and received bytes for debugging
     * 
     * @param data
     *            Bytes sent/received
     * @param length
     *            Number of Bytes sent/received
     * @param prefix
     *            Logging prefix
     */
    public static void logBytes(byte[] data, int length, String prefix, Logger logger) {
        if (logger.isTraceEnabled()) {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < length; i++) {
                byte b = data[i];
                String byteString = Integer.toHexString(b & 0x000000FF);
                byteString = byteString.length() == 1 ? "0" + byteString : byteString;
                sb.append(" ").append(byteString);
            }

            logger.trace(COMMUNICATION, prefix + ": " + sb.toString());
        }
    }
}

Related

  1. log(String format, Object... arguments)
  2. log(String message)
  3. logAndIdError(Logger log, String errorMessage)
  4. logArgs(final Logger logger, final Object[] args)
  5. logBind(Logger log, int index, Object value)
  6. logDebug(Logger logger, String mensagem, Object... params)
  7. logDebug(String string)
  8. logEntrance(Logger logger, String methodName, String methodArguments)
  9. logError(final Logger logger, final String message, final Throwable cause)