在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中,增加如下设置:
否则会报 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