一、记住
加写sd卡权限
二、代码
package com.example.logtest; import java.io.File;
import java.io.IOException; import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log; public class MainActivity extends Activity { public static Logger gLoggerTvMenu;
public static String pathTvMenuCache = Environment.getExternalStorageDirectory()+File.separator+"5wei"+ File.separator+"TvMenuCache"+ File.separator; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); gLoggerTvMenu = Logger.getLogger("tvMenu");
PatternLayout tvMenuLayout = new PatternLayout("%d %p [%c] - %m%n");
try {
Appender tvMenuAppender = new FileAppender(tvMenuLayout,pathTvMenuCache+"tvMenu.log");
gLoggerTvMenu.addAppender(tvMenuAppender);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("error", "wrong :"+e.getMessage());
e.printStackTrace();
} gLoggerTvMenu.debug("it is a test");
}
}
三、效果
在对应目录生成对应的日志文件
四、demo外链