zoukankan      html  css  js  c++  java
  • 【转】Android使用Log4j例子

    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");
       
    }
    }
  • 相关阅读:
    bzoj 1188 [HNOI2007]分裂游戏(SG函数,博弈)
    poj 3710 Christmas Game(树上的删边游戏)
    poj 1704 Georgia and Bob(阶梯博弈)
    110 最小路径和
    109 数字三角形
    63 搜索旋转排序数组II
    62 搜索旋转排序数组
    61 搜索区间
    58 四数之和
    关于初始值的问题
  • 原文地址:https://www.cnblogs.com/niutouzdq/p/3489062.html
Copyright © 2011-2022 走看看