标签:
一、记住
加写sd卡权限
二、代码
1 package com.example.logtest; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import org.apache.log4j.Appender; 7 import org.apache.log4j.FileAppender; 8 import org.apache.log4j.Logger; 9 import org.apache.log4j.PatternLayout; 10 11 12 import android.app.Activity; 13 import android.os.Bundle; 14 import android.os.Environment; 15 import android.util.Log; 16 17 18 19 public class MainActivity extends Activity { 20 21 public static Logger gLoggerTvMenu; 22 public static String pathTvMenuCache = Environment.getExternalStorageDirectory()+File.separator+"5wei"+ File.separator+"TvMenuCache"+ File.separator; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 30 gLoggerTvMenu = Logger.getLogger("tvMenu"); 31 PatternLayout tvMenuLayout = new PatternLayout("%d %p [%c] - %m%n"); 32 try { 33 Appender tvMenuAppender = new FileAppender(tvMenuLayout,pathTvMenuCache+"tvMenu.log"); 34 gLoggerTvMenu.addAppender(tvMenuAppender); 35 } catch (IOException e) { 36 // TODO Auto-generated catch block 37 Log.e("error", "wrong :"+e.getMessage()); 38 e.printStackTrace(); 39 } 40 41 gLoggerTvMenu.debug("it is a test"); 42 } 43 }
三、效果
在对应目录生成对应的日志文件
四、demo外链
http://pan.baidu.com/s/1dDCiS17
标签:
原文地址:http://www.cnblogs.com/kunyashaw/p/4338693.html