Log4j实现在Android中 [英] Log4j Implementation in Android
问题描述
我是新来的Android development.I希望可以用Log4j.What我这样写日志的SD Card.How一个文件中所有的步骤是要实现Log4j.I看了很多atricles.But他们没有说明如何配置和实现it.Can有人请解释如何做到这一点在Android中简单的话。
I am new to android development.I want to write logs to one file in SD Card.How can i do this using Log4j.What all are the steps to Implement Log4j.I read many atricles.But none of them describing how to configure and implement it.Can anyone please explain how to do this in android in simple words.
推荐答案
您应该看看 的logback
(下一代的log4j的)。使用 FileAppender
或<一href="http://logback.qos.ch/manual/appenders.html#RollingFileAppender"><$c$c>RollingFileAppender$c$c>.
You should look at logback
(the next generation of log4j). Use the FileAppender
or RollingFileAppender
.
-
添加
SLF4J-API-1.6.6.jar
和的logback-的Android 1.0.6-2.jar
到类路径中。
创建文件在你的项目资产/ logback.xml
(或使用的AndroidManifest.xml
...查看例如),包含以下配置:
Create the file assets/logback.xml
in your project (or use the AndroidManifest.xml
...see example), containing the following configuration:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/sdcard/testFile.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
注:由于指定的路径是SD,确保使用<一个href="http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE"><$c$c>WRITE_EXTERNAL_STORAGE$c$c>允许。您也可以指定一个不同的路径,你已经有写权限。的
您的Java code,其中包含了SLF4J记录来电,现在记录的所有事件达到或超过 DEBUG
级到 / SD卡/testFile.log
。
Your Java code, which contains the SLF4J logging calls, now logs all events at or above the DEBUG
level to the /sdcard/testFile.log
.
package com.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.R;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroidActivity extends Activity {
static private final Logger LOG =
LoggerFactory.getLogger(HelloAndroidActivity.class);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
// Based on the configuration above, these log statements
// are written to /sdcard/testFile.log
//
LOG.info("Hello Android!");
LOG.debug("reply: {}", Example.hello());
}
}
class Example {
static private final Logger LOG =
LoggerFactory.getLogger(Example.class);
static public String hello() {
LOG.trace("entered hello()");
return "Hi there!";
}
}
这篇关于Log4j实现在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!