Last active
October 12, 2015 10:33
-
-
Save Dhaval2404/f5bb7076d9ac542e7dae to your computer and use it in GitHub Desktop.
Wrapper class for Android Log. Here it's very easy to print Log on consol as well as you can store log in file also.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dhaval2404.log | |
import android.util.Log; | |
import android.os.Environment; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Locale; | |
/** | |
* Wrapper class for Android Log. | |
* | |
* @author Dhaval Patel | |
* @version 1.0, Oct 12, 2015 | |
* @since 1.0 | |
*/ | |
public final class Logger { | |
private static final Boolean ENABLE_CONSOLE_LOG = true; | |
private static final Boolean ENABLE_FILE_LOG = true; | |
private static final LogLevel GLOBAL_LOG_LEVEL = LogLevel.VERBOSE; | |
private static final String LOG_DIRECTORY = Environment.getExternalStorageDirectory() + File.separator+"AppLog"+File.separator; | |
private enum LogLevel{ | |
VERBOSE(Log.VERBOSE), | |
DEBUG(Log.DEBUG), | |
INFO(Log.INFO), | |
WARNING(Log.WARN), | |
ERROR(Log.ERROR), | |
ASSERT(Log.ASSERT); | |
private final int logLevel; | |
LogLevel(int logLevel) { | |
this.logLevel = logLevel; | |
} | |
public int getLogLevel() { | |
return logLevel; | |
} | |
} | |
/** | |
* | |
* @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(String tag, String msg) { | |
write(LogLevel.VERBOSE, tag, msg); | |
} | |
/** | |
* | |
* @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 An exception to log | |
* | |
*/ | |
public static void v(String tag, String msg, Throwable tr) { | |
write(LogLevel.VERBOSE, tag, msg, tr); | |
} | |
/** | |
* | |
* @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(String tag, String msg) { | |
write(LogLevel.DEBUG, tag, msg); | |
} | |
/** | |
* | |
* @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 An exception to log | |
* | |
*/ | |
public static void d(String tag, String msg, Throwable tr) { | |
write(LogLevel.DEBUG, tag, msg, tr); | |
} | |
/** | |
* | |
* @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(String tag, String msg) { | |
write(LogLevel.INFO, tag, msg); | |
} | |
/** | |
* | |
* @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 An exception to log | |
* | |
*/ | |
public static void i(String tag, String msg, Throwable tr) { | |
write(LogLevel.INFO, tag, msg, tr); | |
} | |
/** | |
* | |
* @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(String tag, String msg) { | |
write(LogLevel.WARNING, tag, msg); | |
} | |
/** | |
* | |
* @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 An exception to log | |
* | |
*/ | |
public static void w(String tag, String msg, Throwable tr) { | |
write(LogLevel.WARNING, tag, msg, tr); | |
} | |
/** | |
* | |
* @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(String tag, String msg) { | |
write(LogLevel.ERROR, tag, msg); | |
} | |
/** | |
* | |
* @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 An exception to log | |
* | |
*/ | |
public static void e(String tag, String msg, Throwable tr) { | |
write(LogLevel.ERROR, tag, msg, tr); | |
} | |
private static boolean isLogEnable(LogLevel logLevel){ | |
return GLOBAL_LOG_LEVEL.getLogLevel() < logLevel.getLogLevel(); | |
} | |
private static void write(LogLevel logLevel, String tag, String log) { | |
if (isLogEnable(logLevel) && ENABLE_CONSOLE_LOG){ | |
Log.println(logLevel.getLogLevel(), tag, log); | |
} | |
if (isLogEnable(logLevel) && ENABLE_FILE_LOG){ | |
String msg = Calendar.getInstance().getTime().toString()+"\t"+logLevel.name()+"/"+tag+": "+log; | |
write(msg); | |
} | |
} | |
private static void write(LogLevel logLevel, String tag, String log, Throwable tr){ | |
if (isLogEnable(logLevel) && ENABLE_CONSOLE_LOG){ | |
Log.println(logLevel.getLogLevel(), tag, log +"\n" + Log.getStackTraceString(tr)); | |
} | |
if (isLogEnable(logLevel) && ENABLE_FILE_LOG){ | |
String msg = Calendar.getInstance().getTime().toString()+"\t"+logLevel.name()+"/"+tag+": "+log+"\n"+Log.getStackTraceString(tr); | |
write(msg); | |
} | |
} | |
private static void write(String text){ | |
BufferedWriter out = null; | |
String filePath=LOG_DIRECTORY; | |
try { | |
SimpleDateFormat df = new SimpleDateFormat("dd_MMM_yyyy", Locale.ENGLISH); | |
String formattedDate = df.format(System.currentTimeMillis()); | |
if(!new File(LOG_DIRECTORY).exists()) | |
new File(LOG_DIRECTORY).mkdir(); | |
filePath = LOG_DIRECTORY +formattedDate+".log"; | |
if(!new File(filePath).exists()) | |
new File(filePath).createNewFile(); | |
FileWriter fStream = new FileWriter(filePath, true); | |
out = new BufferedWriter(fStream); | |
out.write(text+"\n"); | |
out.flush(); | |
} catch (IOException e) { | |
Log.e("Log", "Path:"+filePath); | |
e.printStackTrace(); | |
} finally { | |
try { | |
if(out!=null) | |
out.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's usage will be same like Android Log.
e.g. Logger.e("TAG", "Sample log message");
e.g. Logger.i("TAG", "Sample log message");