在android 中使用og4j

Android is a Linux-based operating system designed primarily for touchscreen mobile devices such as smartphones and tablet computers, developed by Google in conjunction with the Open Handset Alliance. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language. Apache log4j is a logging library for Java and an Apache Software Foundation Project and developed by a dedicated team of Committers of the Apache Software Foundation. Here we will explain how to integrate Log4J in Android based Java applications.

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

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

更多相关文章

  1. 每日一道Android(安卓)面试题,面试途中不败题
  2. Android使用XML全攻略(2)
  3. 使用android快速开发框架afinal 开发android应用程序
  4. Android(安卓)页面惯性回弹效果,Nested接口接口简单实现。
  5. 想抢先体验Android操作系统的魅力吗?那就使用Android(安卓)LiveCD
  6. Android使用libjpeg实现图片压缩
  7. Android自定义属性与自定义属性的获取
  8. 想抢先体验Android操作系统的魅力吗?那就使用Android(安卓)LiveCD
  9. 强烈推荐Android新手的入门学习策略之一,Android(安卓)Studio4使

随机推荐

  1. MySQL使用全库备份数据恢复单表数据的方
  2. MySQL用户账户管理和权限管理深入讲解
  3. windows 环境下 MySQL 8.0.13 免安装版配
  4. MYSQL8.0.13免安装版配置教程实例详解
  5. Centos7.5安装mysql5.7.24二进制包方式部
  6. 基于更新SQL语句理解MySQL锁定详解
  7. 使用PDO防sql注入的原理分析
  8. MySQL 8.0.13 下载安装教程图文详解
  9. MySQL5.6的zip包安装教程详解
  10. 如何批量生成MySQL不重复手机号大表实例