溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

log4j如何在Android項(xiàng)目中使用

發(fā)布時(shí)間:2020-11-20 16:48:58 來(lái)源:億速云 閱讀:286 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

log4j如何在Android項(xiàng)目中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

 Android 使用log4j

前言:

 如果要直接在android工程中使用log4j,是有點(diǎn)問(wèn)題的,會(huì)報(bào)如下的錯(cuò):

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 

因此,可以另外多下載一個(gè)叫android-logging-log4j的項(xiàng)目,地址在:

http://code.google.com/p/android-logging-log4j/downloads/list,注意,原本的log4j還是需要的。

  在AndroidManifest.xml中,增加如下設(shè)置:

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

   然后在程序中如下使用:  

package com.android.myapp; 
  
. 
import java.io.File; 
  
 
import org.apache.log4j.Level; 
  
 
import org.apache.log4j.Logger; 
. 
import android.app.Application; 
  
 
import android.os.Environment; 
  
 
import de.mindpipe.android.logging.log4j.LogConfigurator; 
 . 
  
public class MyApplication extends Application { 
  
 
    @Override 
  
 
    public void onCreate() { 
  
 
        super.onCreate(); 
  
 
        LogConfigurator logConfigurator = new LogConfigurator(); 
  
 
        logConfigurator.setFileName(Environment.getExternalStorageDirectory() 
  
 
                + File.separator + "MyApp" + File.separator + "logs" 
  
 
                + File.separator + "log4j.txt"); 
  
 
        logConfigurator.setRootLevel(Level.DEBUG); 
  
 
        logConfigurator.setLevel("org.apache", Level.ERROR); 
  
 
        logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n"); 
  
 
        logConfigurator.setMaxFileSize(1024 * 1024 * 5); 
  
 
        logConfigurator.setImmediateFlush(true); 
  
 
        logConfigurator.configure(); 
  
 
        Logger log = Logger.getLogger(MyApplication.class); 
  
 
        log.info("My Application Created"); 
  
 
    } 
  
 
} 

     現(xiàn)在日志則是以:

Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "log4j.txt
   的方式保存了。其他和log4j的用法差不多了。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI