您的位置:首页 > 其它

安卓使用log4j输出日志到文件

2015-02-27 11:19 579 查看

在android中,实现输出log内容到sd卡中的文件里面,做法是:

还是相对来说,log4j,算是好用。

以下库可以到我的资源里面找可以找到。

http://download.csdn.net/detail/janchin/8460189

http://download.csdn.net/detail/janchin/8460197

1.下载android的log4j的库(的封装)

去:http://code.google.com/p/android-logging-log4j/

下载对应的android-logging-log4j-1.0.3.jar,加到项目中。

2.再去下载所依赖的apache的log4j库

去:http://logging.apache.org/log4j/1.2/download.html

下载1.2系列版本的:log4j-1.2.17.zip

解压得到log4j-1.2.17.jar加到项目中。

3.写测试代码:

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");
}


即可实现:

(1)可以在/mnt/sdcard中生成对应的crifanli_log4j.log文件

(2)log输出的内容中,是DEBUG,且对应的是自己的字符串标识符CrifanLiLog4jTest

原文地址:http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_card_file/


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: