在android中,实现输出log内容到sd卡中的文件里面,做法是:
1.导入者两个jar包
2.测试代码
import de.mindpipe.android.logging.log4j.LogConfigurator; import java.io.File; import android.os.Environment; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class BaseActivity extends Activity { private Logger gLogger; public void configLog() { final LogConfigurator logConfigurator = new LogConfigurator(); logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "crifanli_log4j.log"); // Set the root log level logConfigurator.setRootLevel(Level.DEBUG); // Set log level of a specific logger logConfigurator.setLevel("org.apache", Level.ERROR); logConfigurator.configure(); //gLogger = Logger.getLogger(this.getClass()); gLogger = Logger.getLogger("CrifanLiLog4jTest"); } @Override protected void onCreate(Bundle savedInstanceState) { configLog(); gLogger.debug("test android log to file in sd card using log4j"); }
在AndroidManifest.xml中,增加如下设置:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
否则会报 Caused by: java.io.FileNotFoundException: /storage/sdcard0/crifanli_log4j.log: open failed: EACCES (Permission denied)错误
最后即可实现
(1)可以在/mnt/sdcard中生成对应的crifanli_log4j.log文件
(2)log输出的内容中,是DEBUG,且对应的是自己的字符串标识符CrifanLiLog4jTest
jar包下载地址http://down.51cto.com/data/2155282
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。