Android Development Prerequisite Log Printing tool class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.