Skip to content

Instantly share code, notes, and snippets.

@yelinaung
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save yelinaung/d8b166c108dd2e50ea2c to your computer and use it in GitHub Desktop.

Select an option

Save yelinaung/d8b166c108dd2e50ea2c to your computer and use it in GitHub Desktop.
SimpleLogger
/*
* Copyright (c) 2014. Ye Lin Aung
* 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.
*/
/*
* Copyright 2012 Google Inc.
*
* 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 android.util.Log;
/**
* Created by Ye Lin Aung on 14/01/18.
*/
public class LogUtils {
private static String LOG_PREFIX = "YOUR_APP_NAME";
private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
private static final int MAX_LOG_TAG_LENGTH = 23;
// Define your prefix here
public LogUtils() {
}
// TODO You have to specify your own prefix
public static String setPrefix(String prefix) {
return LOG_PREFIX = prefix;
}
public static String makeLogTag(String str) {
if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
}
return LOG_PREFIX + str;
}
public static String makeLogTag(Class<?> cls) {
return makeLogTag(cls.getSimpleName());
}
public static void LOGD(final String tag, String message) {
Throwable throwable = new Throwable();
StackTraceElement[] e = throwable.getStackTrace();
String c_name = e[1].getMethodName();
if (BuildConfig.DEBUG) {
Log.d(tag, "[" + c_name + "] " + message);
}
}
public static void LOGD(final String tag, String message, Throwable throwable) {
if (BuildConfig.DEBUG) {
Log.d(tag, message, throwable);
}
}
public static void LOGV(final String tag, String message) {
Throwable throwable = new Throwable();
StackTraceElement[] e = throwable.getStackTrace();
String c_name = e[1].getMethodName();
if (BuildConfig.DEBUG) {
Log.v(tag, "[" + c_name + "] " + message);
}
}
public static void LOGV(final String tag, String message, Throwable throwable) {
if (BuildConfig.DEBUG) {
Log.v(tag, message, throwable);
}
}
public static void LOGI(final String tag, String message) {
Throwable throwable = new Throwable();
StackTraceElement[] e = throwable.getStackTrace();
String c_name = e[1].getMethodName();
if (BuildConfig.DEBUG) {
Log.i(tag, "[" + c_name + "] " + message);
}
}
public static void LOGI(final String tag, String message, Throwable throwable) {
if (BuildConfig.DEBUG) {
Log.i(tag, message, throwable);
} else if (Log.isLoggable(tag, Log.VERBOSE)) {
Log.i(tag, message, throwable);
} else {
Log.i(tag, message, throwable);
}
}
public static void LOGE(final String tag, String message) {
Throwable throwable = new Throwable();
StackTraceElement[] e = throwable.getStackTrace();
String c_name = e[1].getMethodName();
if (BuildConfig.DEBUG) {
Log.e(tag, "[" + c_name + "] " + message);
}
}
public static void LOGE(final String tag, String message, Throwable throwable) {
if (BuildConfig.DEBUG) {
Log.e(tag, message, throwable);
}
}
}
private String TAG = makeLogTag(LoginActivity.class);
LOGD(TAG, "lat " + getLatLong()[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment