Androidログ出力ツールクラスのカスタマイズ


<span style="font-size:18px;">/***
 *-----------     ---------------
 * 1.0.0     :
 * a、      ,                  ;
 * b、                ,         :
 * 	(1)                 is_print_log,   : <bool name="is_print_log">true</bool>;
 * 	(2)  ,            ,Logger.getInstance(Context);
 * 	(3)  ,             , Logger.i("","");
 * 2.0.0     :
 * a、              ;
 * 3.0.0      :                ;
 */
package com.keyisoftware.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

/**
 *         ,            configs.xml    is_print_log   false;
 * 
 * @author [email protected]
 * @version 2.0.0
 */
public class KyLog {

	public enum LogType {
		debug, error, info, verbose, warn
	}

	private static final String TAG = "KyLog_";
	private static KyLog log;

	/**          */
	private static boolean isPrintDebugLog = false;
	/**          */
	private static boolean isWriteToSdcard = false;
	/**       */
	private static String projectDirectory;

	private static String DebugDir = "";
	private static String ErrorDir = "";
	private static String VerboseDir = "";
	private static String WarnDir = "";
	private static String InfoDir = "";

	/**
	 *     
	 * 
	 * @param context
	 *               
	 */
	private KyLog(Context context) {
		try {
			// TODO:               R   。
			isPrintDebugLog = context.getResources().getBoolean(R.bool.is_print_log);
			isWriteToSdcard = context.getResources().getBoolean(R.bool.is_write_to_sdcard);
			projectDirectory = context.getResources().getString(R.string.project_directory);

			//         
			/**        **/
			if (projectDirectory == null || projectDirectory == "") {
				projectDirectory = "KyUtils";
			}
			String LogDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + projectDirectory + "/Log/";

			DebugDir = LogDirectory + "/Debug/";
			ErrorDir = LogDirectory + "/Error/";
			VerboseDir = LogDirectory + "/Verbose/";
			WarnDir = LogDirectory + "/Warn/";
			InfoDir = LogDirectory + "/Info/";

			/**        **/
			File dir = new File(DebugDir);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			dir = new File(ErrorDir);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			dir = new File(VerboseDir);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			dir = new File(WarnDir);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			dir = new File(InfoDir);
			if (!dir.exists()) {
				dir.mkdirs();
			}

		} catch (Exception e) {
			Log.e(TAG, "Can't find is_print_log in configs.xml file!");
		}
	}

	/**
	 *        
	 * 
	 * @param context
	 *               
	 * @return        
	 */
	public static KyLog getInstance(Context context) {
		if (log == null) {
			log = new KyLog(context);
		}
		return log;
	}

	public static void d(String tag, String msg) {
		if (isPrintDebugLog) {
			Log.d(TAG + tag, msg);
		}

		if (isWriteToSdcard) {
			writeLogToSdCard(LogType.debug, tag, msg);
		}
	}

	public static void e(String tag, String msg) {
		if (isPrintDebugLog)
			Log.e(TAG + tag, msg);

		if (isWriteToSdcard) {
			writeLogToSdCard(LogType.error, tag, msg);
		}
	}

	public static void i(String tag, String msg) {
		if (isPrintDebugLog)
			Log.i(TAG + tag, msg);

		if (isWriteToSdcard) {
			writeLogToSdCard(LogType.info, tag, msg);
		}
	}

	public static void v(String tag, String msg) {
		if (isPrintDebugLog)
			Log.v(TAG + tag, msg);

		if (isWriteToSdcard) {
			writeLogToSdCard(LogType.verbose, tag, msg);
		}
	}

	public static void w(String tag, String msg) {
		if (isPrintDebugLog)
			Log.w(TAG + tag, msg);

		if (isWriteToSdcard) {
			writeLogToSdCard(LogType.warn, tag, msg);
		}
	}

	public static KyLog getLog() {
		return log;
	}

	public static void setLog(KyLog log) {
		KyLog.log = log;
	}

	/***
	 *          ,              , 2015-12-29
	 * 
	 * @return        
	 */
	private static String setLogFileName() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(new Date()) + ".txt";
	}

	/***
	 *     
	 * 
	 * @param logType
	 * @param tag
	 * @param msg
	 */
	private static void writeLogToSdCard(LogType logType, String tag, String msg) {

		String dir = "";
		if (logType.equals(LogType.debug)) {
			dir = DebugDir + setLogFileName();
		} else if (logType.equals(LogType.error)) {
			dir = ErrorDir + setLogFileName();
		} else if (logType.equals(LogType.info)) {
			dir = InfoDir + setLogFileName();
		} else if (logType.equals(LogType.verbose)) {
			dir = VerboseDir + setLogFileName();
		} else if (logType.equals(LogType.warn)) {
			dir = WarnDir + setLogFileName();
		}

		File file = new File(dir);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		writeLogMsg(file, tag, msg);
	}

	/**
	 *             
	 * 
	 * @param tag
	 * @param msg
	 * @return
	 */
	private static String setLogContent(String tag, String msg) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String time = dateFormat.format(new Date());
		return time + " " + tag + " " + msg + "
"; } /*** * * * @param file * @param tag * @param msg */ private static void writeLogMsg(File file, String tag, String msg) { FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { fileWriter = new FileWriter(file, true); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(setLogContent(tag, msg)); bufferedWriter.flush(); bufferedWriter.close(); fileWriter.close(); } catch (IOException e) { try { bufferedWriter.close(); fileWriter.close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } } /*** * */ public static void clearLogDir() { // TODO: } } </span>