Today, we present a useful log printing tool. Everyone in the usual development with the most likely is Log.i("",""),Log.e("","")... that in order to view the log is less than the case, this method is really convenient to use, very easy to write, but also easy to view, but do not know if you have encountered such a situation, if you want to view the amount of data is very large, and then use the log class to print out later , but found that only a subset of the data was shown, and most of the data was truncated.
Yes, log the length of the print is limited, I had to analyze a piece of data from the server, the amount of data, with the log class can only see a portion of the data, most of the data is truncated, and later wrote a log printing tool class, the log printed to the phone SD card, This allows you to see the complete data, which I have encapsulated and shared with you.
PublicClass Logtools { PublicStaticvoidDailyLog (StringTitleString Log) {try {//If the external memory card exists and is readable and writable if(Environment.Getexternalstoragestate ().equals(Environment.media_mounted)) {Date Date = New Date(); SimpleDateFormat DF= NewSimpleDateFormat ("YyyyMMdd"); SimpleDateFormat TF= NewSimpleDateFormat ("HH:mm:ss:SSS");StringPath=Environment.getExternalStorageDirectory ().GetAbsolutePath ()+File.Separator+ "MyLog" +File.Separator+ "debuglog[" +Df.FormatDate)+ "]" + ". txt"; File file= NewFile (path);//If the file does not exist, recreate it if(!File.Exists ()) {//The last level is a file, preceded by a path, and creates a path if the path does not exist if(!File.Getparentfile ().Exists ()) {file.Getparentfile ().Mkdirs (); }//Create log fileFile.CreateNewFile (); }//write logFileWriter FW= NewFileWriter (file,true); Fw.Flush (); Fw.Write"\n[" +Df.FormatDate)+"--"+Tf.FormatDate)+ "]\n"); Fw.Write (title+ ": " + Log); Fw.Write"\ n"); Fw.Close (); }} catch (IOException e) {E.Printstacktrace (); } }}
Call:
publicclass MainActivity extends Activity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LogTools.dailyLog("我的日志""----33222211111111118838777777777777766666666666444444444"); }}
Download the tool source code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.
Android Development Prerequisite Log Printing tool class