Step 1

Download Log4J library from http://logging.apache.org/log4j/1.2/download.html

Step 2

Configuring Log4J library the normal way - using XML configuration file - can't be used in Android based Java application as the configuration classes of Log4J use beans from the package "java.beans" (e.g. PropertyDescriptor). Not all classes of this package are supported in Android, so using Log4J directly in Android Java application will case exceptions like the following one to be thrown:

11-23 09:44:56.947: D/dalvikvm(1585): GC_FOR_MALLOC freed 3278 objects / 311568 bytes in 31ms
rejecting opcode 0x21 at 0x000a
rejected Lorg/apache/log4j/config/PropertySetter;.getPropertyDescriptor
(Ljava/lang/String;)Ljava/beans/PropertyDescriptor;
Verifier rejected class Lorg/apache/log4j/config/PropertySetter;
Exception Ljava/lang/VerifyError; thrown during Lorg/apache/log4j/LogManager;.
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x400259f8)
FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:64)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:253)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265)
...
Caused by: java.lang.VerifyError: org.apache.log4j.config.PropertySetter
at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:772)
at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:547)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
at org.apache.log4j.LogManager.(LogManager.java:127)
... 20 more

There is a project called android-logging-log4j, which provides a convenient way to configure the log4j system properly.

Download "Android Logging Log4J" library fromhttp://code.google.com/p/android-logging-log4j/downloads/list

Step 3

Add Both libraries "log4j" and "android-logging-log4j" to your application libraries

Step 4

In order to log to a file on the external storage, the following permission needs to be placed in AndroidManifest.xml

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 5

In your application Main class:

  1. package com.android.myapp;
  2.  
  3. import java.io.File;
  4.  
  5. import org.apache.log4j.Level;
  6. import org.apache.log4j.Logger;
  7.  
  8. import android.app.Application;
  9. import android.os.Environment;
  10. import de.mindpipe.android.logging.log4j.LogConfigurator;
  11.  
  12. public class MyApplication extends Application {
  13.         @Override
  14.         public void onCreate() {
  15.                 super.onCreate();
  16.                 LogConfigurator logConfigurator = new LogConfigurator();
  17.                 logConfigurator.setFileName(Environment.getExternalStorageDirectory()
  18.                                 + File.separator + "MyApp" + File.separator + "logs"
  19.                                 + File.separator + "log4j.txt");
  20.                 logConfigurator.setRootLevel(Level.DEBUG);
  21.                 logConfigurator.setLevel("org.apache", Level.ERROR);
  22.                 logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
  23.                 logConfigurator.setMaxFileSize(1024 * 1024 * 5);
  24.                 logConfigurator.setImmediateFlush(true);
  25.                 logConfigurator.configure();
  26.                 Logger log = Logger.getLogger(MyApplication.class);
  27.                 log.info("My Application Created");
  28.         }
  29. }

Now you will have log4j configured to log to Path: (Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "log4j.txt") with DEBUG level and ERROR lever for "org.apache" package with file pattern "%d %-5p [%c{2}]-[%L] %m%n" and other configuration parameters.

6. 程序中使用的实例。

import org.apache.log4j.Logger;

public class ExampleLog4J{
    privatefinalLogger log =Logger.getLogger(ExampleLog4J.class);     publicvoid myMethod(){
        log.info("This message should be seen in log file and logcat");
    }
}

【转】Android使用Log4j例子的更多相关文章

  1. [Android Pro] 完美Android Cursor使用例子(Android数据库操作)

    reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...

  2. Android MediaCodec 使用例子

    Android MediaCodec 使用例子 下面的例子是使用MediaCodec 录制到文件的例子. 1 public class AvcEncoder { private MediaCodec ...

  3. 我的Android进阶之旅------>Android拍照小例子

    今天简单的学习了一下android拍照的简单实现. 当然该程序是个小例子,非常简单,没有什么复杂的操作,但是可以学习到Android 拍照API流程. 1.在布局文件中添加一个 surfaceView ...

  4. Android中log4j的运用

    网上一查关于android上面运用Log4j的运用,各种说需要添加多样的包的,照着log4j的官网教程看了下,给了个简单的输出到console上面的代码,似乎没什么用.看网上关于Log4j更多是在ja ...

  5. android highcharts 柱状图例子

    android提供achartengine api 只能做简单的,如果是复杂的图表,个人的想法结合highcharts来完成:减小工作量,官方提供的例子也非常丰富. 通过android webview ...

  6. 编译android framework的例子【转】

    本文转载自:http://blog.csdn.net/brucexu1978/article/details/7610358 在开发过程中,尤其是Framework相关开发时,有时候需要重新编译资源文 ...

  7. Android test---robotium----简单例子

    1.首先新建一个要被测试的工程,命名为”robotium“:一个很简单的Android 应用程序:主页面只有个 TextView 控件: 2. 在建一个用于测试的工程 ,命名为”robotiumTes ...

  8. Android密码约束规则例子一

    Android常用的一个密码规则 (一)密码必须是8至16位:(二)密码必须包含英文字母和数字:(三)密码不能包含4位连续相同的字符,如0000或AAAA:(四)密码不能包含4位连续递增或连续递减的数 ...

  9. PhoneGap: Android平台入门例子(Hello World)

    Hello World Demo: http://docs.phonegap.com/en/2.0.0/guide_getting-started_android_index.md.html#Gett ...

随机推荐

  1. 结对编程——关于Fault、Error、Failure程序设计

    一.问题描述:         构造程序,分别是:         •不能触发Fault         •触发Fault,但是不能触发Error         •触发Error,但是不能产生Fai ...

  2. WP8 独立存储 总结3(应用设置)

    •可在独立存储中使用ApplicationSettings对象•在独立存储中存储键/值对的Dictionary方式存储 •存储的对象将永久保存 在应用设置中保存数据 void saveString(s ...

  3. vector用法总结(转载)

    一.vector的基本概念 vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库负责管理存储元素的相关内存.我们把vector称为容器,是因为它可以包 ...

  4. ASP.NET MVC4 & Entity Framework 6.0 IIS 部署出错解决方案

    博客地址 http://blog.csdn.net/foxdave 近期了解MVC4的时候弄了一个简单的小工程,使用Entity Framework作为Model,F5启动调试运行的时候没有问题,但是 ...

  5. SecureCRT下的串口无法输入

    用串口配置交换机的时候,出现的问题: 用secureCRT建了一个串口COM1后,连接上开发板后,可以正确接受和显示串口的输出,但是按键输入无效. 解决方法: Session Options -> ...

  6. 事件委托 EventHandler

    事件就是当对象或类状态发生改变时,对象或类发出的信息或通知.发出信息的对象或类称为"事件源",对事件进行处理的方法称为"接收者",通常事件源在发出状态改变信息时 ...

  7. BZOJ 3450 Easy

    注意细节啊... 和上一道差不多. #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  8. Apache 的启动/重启/停止

    Task: Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 startor$ sudo /etc/init.d/apache2 star ...

  9. PHP图像处理之在原图像处理

    处理原有的图像        图片处理,缩放,裁剪,翻转,旋转,透明,锐化等图片操作        一.创建图片资源            imagecreatetruecolor(width,hei ...

  10. 第一个PHP程序

    <html> <head> <title><?php echo"这是第一个php程序"?></title> <st ...