Skip to content

Instantly share code, notes, and snippets.

@MaTriXy
Forked from dors/LogUtil.java
Created November 18, 2015 13:00
Show Gist options
  • Save MaTriXy/7fd09e101e8fc459d61b to your computer and use it in GitHub Desktop.
Save MaTriXy/7fd09e101e8fc459d61b to your computer and use it in GitHub Desktop.
Cool log util for Android apps that prints each log with the thread, class name and method name from which the log was made
import android.util.Log;
import java.text.MessageFormat;
/*
* Copyright 2015 Dor Sakal
*
* 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.
*/
public class LogUtil {
//TODO: use your own TAG here
private static final String TAG = "MY_TAG";
/*********************************
* public static methods
*********************************/
public static void v(String text) {
doLog(Log.VERBOSE, text);
}
public static void d(String text) {
doLog(Log.DEBUG, text);
}
public static void i(String text) {
doLog(Log.INFO, text);
}
public static void w(String text) {
doLog(Log.WARN, text);
}
public static void e(String text) {
doLog(Log.ERROR, text);
}
/*********************************
* private static methods
*********************************/
private static void doLog(int logLevel, String logText) {
StackTraceElement[] stackTrace = Thread.currentThread()
.getStackTrace();
//take stackTrace element at index 4 because:
//0: VMStack.getThreadStackTrace(Native Method)
//1: java.lang.Thread.getStackTrace
//2: LogUtil -> doLog method (this method)
//3: LogUtil -> log method
//4: this is the calling method!
if (stackTrace != null && stackTrace.length > 4) {
StackTraceElement element = stackTrace[4];
String fullClassName = element.getClassName();
String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
//add class and method data to logText
logText = MessageFormat.format("T:{0} | {1} , {2}() | {3}", Thread.currentThread()
.getId(), simpleClassName, element.getMethodName(), logText);
}
Log.println(logLevel, TAG, logText);
}
}
//Calling from from MainActivity's onCreate like so:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LogUtil.v("test v");
LogUtil.d("test d");
LogUtil.i("test i");
LogUtil.w("test w");
LogUtil.e("test e");
}
//Would cause the following log:
11-18 00:16:16.304 21659-21659/? V/MY_TAG: T:1 | MainActivity , onCreate() | test v
11-18 00:16:16.308 21659-21659/? D/MY_TAG: T:1 | MainActivity , onCreate() | test d
11-18 00:16:16.308 21659-21659/? I/MY_TAG: T:1 | MainActivity , onCreate() | test i
11-18 00:16:16.308 21659-21659/? W/MY_TAG: T:1 | MainActivity , onCreate() | test w
11-18 00:16:16.308 21659-21659/? E/MY_TAG: T:1 | MainActivity , onCreate() | test e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment