package com.seatrend.enforcement.logpritln; import android.os.Environment; import android.util.Log; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by Administrator on 2019/11/5. */ public class Logutil { File file = null; private String filePath = Environment.getExternalStorageDirectory()+"/"+"aLohpritlin/"; String fileName = "log.txt"; Date date = new Date(); String time = date.toLocaleString(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒"); String sim = dateFormat.format(date); public void e(String mark,String info){ writeTxtToFile(sim+":error"+":"+mark+":"+info); } public void i(String mark,String info){ writeTxtToFile(sim+":info"+":"+mark+":"+info); } public void d(String mark,String info){ writeTxtToFile(sim+":debug"+":"+mark+":"+info); } public void w(String mark,String info){ writeTxtToFile(sim+":warn"+":"+mark+":"+info); } // 将字符串写入到文本文件中 private void writeTxtToFile(String strcontent) { File file = new File(filePath); if (!file.exists()){ file.mkdirs(); } File file1 = new File(filePath+fileName); if (!file1.exists()){ try { file1.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } // 每次写入时,都换行写 String strContent = strcontent + "\r\n"; try { FileOutputStream raf = new FileOutputStream (file1); raf.write(strContent.getBytes()); raf.close(); } catch (Exception e) { Log.e("TestFile", "Error on write File:" + e); } } }
android自定义log日志的输出
最新推荐文章于 2022-02-25 14:48:46 发布