Java tutorial
//package com.java2s; //License from project: Apache License import android.os.Environment; import android.text.TextUtils; import android.util.Log; import java.io.File; import java.io.FileOutputStream; public class Main { private static String DEFAULT_LOG_TAG = "mengdd"; private final static boolean LOG_TO_FILE = false; private final static String LOG_FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/mengdd_debug_log.txt"; private static File LOG_FILE; public static int e(String msg) { return println(Log.ERROR, DEFAULT_LOG_TAG, msg); } public static int e(String tag, String msg) { return println(Log.ERROR, tag, msg); } public static int e(String tag, String msg, Throwable tr) { return println(Log.ERROR, tag, msg + '\n' + getStackTraceString(tr)); } public static int println(int priority, String tag, String msg) { if (TextUtils.isEmpty(msg)) { msg = ""; } if (LOG_TO_FILE) { logToFile(tag, msg); return 0; } else { return Log.println(priority, tag, msg); } } public static String getStackTraceString(Throwable tr) { return Log.getStackTraceString(tr); } private static synchronized void logToFile(String tag, String msg) { try { if (LOG_FILE == null) { LOG_FILE = new File(LOG_FILE_PATH); if (LOG_FILE.exists()) { LOG_FILE.delete(); } LOG_FILE.createNewFile(); } FileOutputStream outputStream = new FileOutputStream(LOG_FILE, true); StringBuilder builder = new StringBuilder(); builder.append(tag).append(" : ").append(msg).append("\n"); outputStream.write(builder.toString().getBytes()); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }