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. C# WebBrowser NativeMethods

    using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...

  2. Html获取经纬度

    if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( function success(pos) {alert( ...

  3. Android基础总结

    原文  http://blog.csdn.net/heimady/article/details/10363995 1. 前言 1.1. 什么是 3G . 4G Ÿ 第三代移动通信技术(3rd - G ...

  4. c++形参和实参同名时,如何单步执行观察形参的变化。

    c++形参和实参同名时,如何单步执行观察形参的变化? 方法:当程序运行到函数中时,添加变量观察即可.

  5. access手工注入

    1.判断数据类型 and exists (select * from msysobjects) >0 and exists (select * from sysobjects) >0 一般 ...

  6. PP

  7. IOS图片缩放

    1.自动缩放到指定大小 + (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize { UIImage *newimage; ...

  8. PHP中的正则表达式的使用

    PHP中的正则表达式基础知识1.正则表达式就是描述字符串排列模式的一种自定义语法规则2.如果可以使用字符串处理函数完成的任务,就不使用正则表达式3.有一些复杂的操作,只能使用正则表达式完成4.正则表达 ...

  9. Alice and Bob(不断补充)

    我之前做过一些博弈的题目,以为博弈都是DP,结果被坑了很多次,其实博弈有很多种,在此,把我见过的类型都搬上来. 1,HDU3951(找规律) 题意:把n枚硬币围成一个圆,让Alice和Bob两个人分别 ...

  10. UI 网络程序

    一,从网络地址获取一张图片 -(void)didClickDownLoad:(id)sender{    NSLog(@"%@",[NSDate date].description ...