Java tutorial
/* * Copyright 2013 Luluvise Ltd * * 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. */ package com.github.luluvise.droid_utils.logging; import android.util.Log; import com.github.luluvise.droid_utils.DroidConfig; import com.google.common.annotations.Beta; import com.google.common.base.Strings; /** * Helper class containing static utility methods for logging. * * @since 1.0 * @author Marco Salis */ @Beta public class LogUtils { private static final String TAG = LogUtils.class.getSimpleName(); private LogUtils() { // hidden constructor, no instantiation needed } /** * Logs a message in LogCat, only if application debugging is active. Logcat has a message * length limitation of ~4000 chars, so if the logMessage is > 4000 chars, recursively print * 4000 characters of the logMessage until all chars are logged. * * @param logLevel * The log level to use: must be one of the level constants * provided by the {@link Log} class. * @param logTag * @param logMessage */ public static void log(int logLevel, String logTag, String logMessage) { if (!Strings.isNullOrEmpty(logMessage)) { if (logMessage.length() > 4000) { logHelper(logLevel, logTag, logMessage.substring(0, 4000)); log(logLevel, logTag, logMessage.substring(4000)); } else { logHelper(logLevel, logTag, logMessage); } } } private static void logHelper(int logLevel, String logTag, String logMessage) { if (DroidConfig.DEBUG) { switch (logLevel) { case Log.ASSERT: case Log.VERBOSE: Log.v(logTag, logMessage); break; case Log.DEBUG: Log.d(logTag, logMessage); break; case Log.INFO: Log.i(logTag, logMessage); break; case Log.WARN: Log.w(logTag, logMessage); break; case Log.ERROR: Log.e(logTag, logMessage); break; } } } /** * Prints on LogCat a stack track of an exception, only if application * debugging is active. * * @param t * The {@link Throwable} to get the stack trace from */ public static void logException(Throwable t) { if (DroidConfig.DEBUG) { if (t != null) { t.printStackTrace(); } } } /** * Prints on LogCat a log warning message with the exception stack, only if * application debugging is active. * * @param logTag * @param logMessage * @param t * The {@link Throwable} to get the stack trace from */ public static void logException(String logTag, String logMessage, Throwable t) { if (DroidConfig.DEBUG) { if (t != null) { Log.w(logTag, logMessage, t); } } } /** * Prints on LogCat a log warning message with the exception's message * string, only if application debugging is active. * * @param t * The {@link Throwable} to get the message from */ public static void logExceptionMessage(String logTag, String logMessage, Throwable t) { if (DroidConfig.DEBUG) { if (t != null) { Log.w(logTag, logMessage + "\n" + t.getClass().getSimpleName() + " message: " + t.getMessage()); } } } /** * Prints on LogCat an exception's message string, only if application * debugging is active. * * @deprecated Use {@link #logExceptionMessage(String, String, Throwable)} * instead for more accurate logging * @param t * The {@link Throwable} to get the message from */ @Deprecated public static void logExceptionMessage(Throwable t) { if (DroidConfig.DEBUG) { if (t != null) { Log.w(TAG, t.getClass().getSimpleName() + " message: " + t.getMessage()); } } } }