SoFunction
Updated on 2025-04-07

How to use log4j on Android and precautions

Android uses log4j

Preface:

If you want to use log4j directly in android project, there is a problem and you will report the following error:

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 
 
at org..(:64) 
at org.(:253) 
at org.(:265) 
... 
Caused by: : . 
at .(:772) 
at .(:735) 
at .(:615) 
at .(:502) 
at .(:547) 
at .(:483) 
at ..(:127) 
... 20 more 

Therefore, you can download another project called android-logging-log4j, with the address at:

/p/android-logging-log4j/downloads/list, note that the original log4j is still needed.

In, add the following settings:

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

Then use it in the program as follows:

package ; 
  
. 
import ; 
  
 
import .; 
  
 
import .; 
. 
import ; 
  
 
import ; 
  
 
import .; 
 . 
  
public class MyApplication extends Application { 
  
 
    @Override 
  
 
    public void onCreate() { 
  
 
        (); 
  
 
        LogConfigurator logConfigurator = new LogConfigurator(); 
  
 
        (() 
  
 
                +  + "MyApp" +  + "logs" 
  
 
                +  + ""); 
  
 
        (); 
  
 
        ("", ); 
  
 
        ("%d %-5p [%c{2}]-[%L] %m%n"); 
  
 
        (1024 * 1024 * 5); 
  
 
        (true); 
  
 
        (); 
  
 
        Logger log = (); 
  
 
        ("My Application Created"); 
  
 
    } 
  
 
} 

Now the log is as follows:

() + + "MyApp" + + "logs" + + "
Saved in the way. Others are similar to log4j usage.

Thank you for reading, I hope it can help you. Thank you for your support for this site!