Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active August 29, 2015 14:00
Show Gist options
  • Save kristopherjohnson/11219895 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/11219895 to your computer and use it in GitHub Desktop.
Android logging methods that check Log.isLoggable()
package net.kristopherjohnson.util;
import android.util.Log;
/**
* Static utility methods related to logging, using the android.util.Log class.
*
* All application classes should use the methods of this class rather than using
* android.util.Log directly, to allow application-level customization of the logging mechanism.
*/
public class LogUtil
{
// Private constructor: class cannot be instantiated
private LogUtil()
{
}
/**
* Checks to see whether log for tag at specified level should be logged
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param level
* @return Whether or not that this is allowed to be logged
*/
public static boolean isLoggable(String tag, final int level)
{
// Note: Log.isLoggable() will only return true for levels of INFO and above by
// default. To override this behavior, set system properties as described in the
// documentation for Log.isLoggable(), or add appropriate filtering code here.
// Log.isLoggable() will throw an exception if the length of the tag is greater than
// 23 characters, so trim it if necessary to avoid the exception.
if (tag.length() > 23)
{
tag = tag.substring(0, 22);
}
return Log.isLoggable(tag, level);
}
/**
* Call Log.e(tag, msg, tr) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
* @param tr
*/
public static void e(final String tag, final String msg, final Throwable tr)
{
if (isLoggable(tag, Log.ERROR))
{
Log.e(tag, msg, tr);
}
}
/**
* Call Log.e(tag, msg) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/
public static void e(final String tag, final String msg)
{
if (isLoggable(tag, Log.ERROR))
{
Log.e(tag, msg);
}
}
/**
* Call Log.w(tag, msg, tr) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
* @param tr
*/
public static void w(final String tag, final String msg, final Throwable tr)
{
if (isLoggable(tag, Log.WARN))
{
Log.w(tag, msg, tr);
}
}
/**
* Call Log.w(tag, msg) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/
public static void w(final String tag, final String msg)
{
if (isLoggable(tag, Log.WARN))
{
Log.w(tag, msg);
}
}
/**
* Call Log.i(tag, msg, tr) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
* @param tr
*/
public static void i(final String tag, final String msg, final Throwable tr)
{
if (isLoggable(tag, Log.INFO))
{
Log.i(tag, msg, tr);
}
}
/**
* Call Log.i(tag, msg) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/
public static void i(final String tag, final String msg)
{
if (isLoggable(tag, Log.INFO))
{
Log.i(tag, msg);
}
}
/**
* Call Log.d(tag, msg, tr) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
* @param tr
*/
public static void d(final String tag, final String msg, final Throwable tr)
{
if (isLoggable(tag, Log.DEBUG))
{
Log.d(tag, msg, tr);
}
}
/**
* Call Log.d(tag, msg) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/
public static void d(final String tag, final String msg)
{
if (isLoggable(tag, Log.DEBUG))
{
Log.d(tag, msg);
}
}
/**
* Call Log.v(tag, msg) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
* @param tr
*/
public static void v(final String tag, final String msg, final Throwable tr)
{
if (isLoggable(tag, Log.VERBOSE))
{
Log.v(tag, msg, tr);
}
}
/**
* Call Log.v(tag, msg) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/
public static void v(final String tag, final String msg)
{
if (isLoggable(tag, Log.VERBOSE))
{
Log.v(tag, msg);
}
}
/**
* Call Log.wtf(tag, msg, tr) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
* @param tr
*/
public static void wtf(final String tag, final String msg, final Throwable tr)
{
if (isLoggable(tag, Log.ASSERT))
{
Log.wtf(tag, msg, tr);
}
}
/**
* Call Log.wtf(tag, msg) if loggable
*
* @param tag
* Used to identify the source of a log message. It usually identifies the class
* or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/
public static void wtf(final String tag, final String msg)
{
if (isLoggable(tag, Log.ASSERT))
{
Log.wtf(tag, msg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment