Android Log class log output and save it to a file

android.util.Log conventional method has the following five: 

Log.v () Log.d () Log.i () Log.w () and Log.e (). The first letter corresponding VERBOSE, DEBUG, INFO, WARN, ERROR. 

1, Log.v debug color is black, no messages are output, v winded verbose representatives here meant, that is usually used Log.v ( "", ""); 

2, Log.d output color is blue, the debug output only mean, but he will output the upper, filtering it can be selected by Logcat label of DDMS.

3, Log.i output of green, general informational Information message, and it does not output Log.v Log.d information, but the information i, w and e are displayed.

4, Log.w mean orange, can be seen as a warning warning, we need to pay attention to the general Android optimized code while also selected output Log.e information after it. 

5, Log.e is red, it is conceivable error error, shown here in red only error messages, these errors we need careful analysis to view the information stack up.

the MyLog {class public 
    
    Private static Boolean = MYLOG_SWITCH to true; // log file main switch 
    private static Boolean MYLOG_WRITE_TO_FILE = true; // write the log file switch 
    private static char MYLOG_TYPE = 'v' ; // Input log type, w output only representatives alarm information, etc., v represents the output of all information 
    private static String MYLOG_PATH_SDCARD_DIR = "/ sdcard / kantu / log"; // log file path in sdcard in the 
    private static int SDCARD_LOG_FILE_SAVE_DAYS = 0; save the maximum number of days // sd card in the log file 
    private static String MYLOGFILEName = "Log.txt" ; // this class log file name output 
    private static SimpleDateFormat myLogSdf = new SimpleDateFormat ( "yyyy-mM-dd HH: mm: ss"); // log output format 
    private static SimpleDateFormat logfile = new SimpleDateFormat ( "yyyy -MM-dd"); // log file format 
    public context context;

    public static void w(String tag, Object msg) { // 警告信息
        log(tag, msg.toString(), 'w');
    }

    public static void e(String tag, Object msg) { // 错误信息
        log(tag, msg.toString(), 'e');
    }

    public static void d(String tag, Object msg) {// 调试信息
        log(tag, msg.toString(), 'd');
    }

    public static void i(String tag, Object msg) {//
        log(tag, msg.toString(), 'i');
    }

    public static void v(String tag, Object msg) {
        log(tag, msg.toString(), 'v');
    }

    public static void w(String tag, String text) {
        log(tag, text, 'w');
    }

    public static void e(String tag, String text) {
        log(tag, text, 'e');
    }

    public static void d(String tag, String text) {
        log(tag, text, 'd');
    }

    public static void i(String tag, String text) {
        log(tag, text, 'i');
    }

    public static void v(String tag, String text) {
        log(tag, text, 'v');
    }

    /**
     * 根据tag, msg和等级,输出日志
     * @param tag
     * @param msg
     * @param level
     */
    private static void log(String tag, String msg, char level) { 
        IF (MYLOG_SWITCH) {// log file main switch
            if ('e' == level && ('e' == MYLOG_TYPE || 'v'== MYLOG_TYPE)) {// outputs error information
                Log.e(tag, msg);
            } else if ('w' == level && ('w' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
                Log.w(tag, msg);
            } else if ('d' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
                Log.d(tag, msg);
            } else if ('i' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
                Log.i(tag, msg);
            } else {
                Log.v(tag, msg);
    }
        }
                writeLogtoFile (String.valueOf (Level), Tag, MSG);
            IF (MYLOG_WRITE_TO_FILE) // write the log file switch
            }

     * @param Tag
     * @param mylogtype
     * Open the log file and written to the log
    / ** 
     * @param text 
     * / 
    Private static void writeLogtoFile (String mylogtype, Tag String, String text) {// create a new log file is opened or 
        a Date = new new nowtime a Date (); 
        String = needWriteFiel logfile.format (nowtime); 
        String needWriteMessage = myLogSdf.format (nowtime) + "" + mylogtype + "" + Tag + "" + text; 
        File Environment.getExternalStorageDirectory dirpath = (); 

        File = dirsFile new new File (MYLOG_PATH_SDCARD_DIR); 
        IF (dirsFile.exists ()!) { 
            dirsFile.mkdirs (); 
        } 
        //Log.i ( "create file", "create a file");
        File File = new new File (dirsFile.toString (), needWriteFiel MYLOGFILEName +); // MYLOG_PATH_SDCARD_DIR 
        IF {(File.Exists ()!) 
            The try { 
            bufWriter.close (); 
            filerWriter.close (); 
        } the catch (IOException E) {
                // create a file in the specified folder 
                file.createNewFile (); 
            } the catch (Exception E) { 
            } 
        } 

        the try { 
            FileWriter filerWriter = new new FileWriter (File, to true); // this parameter represents the later is not connected to a file in the original data, without covering 
            BufferedWriter, bufWriter = new new BufferedWriter, (filerWriter); 
            bufWriter.write (needWriteMessage); 
            bufWriter.newLine ();
            e.printStackTrace (); 
        } 
    } 

    / ** 
     * Delete the development of log files 
     * / 
    public static void delfile () {// delete the log files 
        String needDelFiel = logfile.format (getDateBefore ()); 
        File dirpath = Environment.getExternalStorageDirectory ( ); 
        File File = new new File (dirpath, needDelFiel MYLOGFILEName +); // MYLOG_PATH_SDCARD_DIR 
        IF (File.Exists ()) { 
            File.delete (); 
        } 
    } 

    / ** 
     * days before the date of present time obtained for get need to delete the log file name 
     * / 
    Private static getDateBefore a Date () { 
        a Date = new new nowtime a Date (); 
        Calendar now = Calendar.getInstance (); 
        now.setTime (nowtime); 
        now.set (Calendar.DATE, now.get ( Calendar.DATE) - SDCARD_LOG_FILE_SAVE_DAYS); 
        return now.getTime (); 
    } 
}

  Note: also need to add the file permissions in AndroidManifest.xml
<uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" />

Reference to: https://www.cnblogs.com/jeffen/p/6828569.html

 

Guess you like

Origin www.cnblogs.com/changyiqiang/p/11225350.html