android 使用log4j2,java - Log4j2 on Android - Stack Overflow

I'm trying to use log4j2 on Android Project (I'm working on Android Studio).

In order to simplify this question, I will explaine what I've done in a simple dummy project (I got the same error in the real project than in this dummy project).

I create a simple dummy project with a main activity.

I add the log4j2 libs...

This is the gradle:

apply plugin: 'com.android.application'

android {

packagingOptions {

exclude 'META-INF/LICENSE'

exclude 'META-INF/NOTICE'

}

compileSdkVersion 21

buildToolsVersion "21.1.2"

defaultConfig {

applicationId "com.example.test20150130"

minSdkVersion 19

targetSdkVersion 21

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:21.0.3'

compile 'com.android.support:support-v4:21.0.3'

compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.1'

compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1'

}

I've reduced the log4j2.xml to this:

On the Activity, I import the libraries...

import org.apache.logging.log4j.Logger;

import org.apache.logging.log4j.LogManager;

But when I try to use them...

Logger logger= LogManager.getLogger ("Queries");

logger.error ("It works...");

I've got an error on the 'Logger logger= LogManager.getLogger ("Queries");'

01-30 00:49:29.413 1685-1685/com.example.test20150130 E/AndroidRuntime? FATAL EXCEPTION: main

Process: com.example.test20150130, PID: 1685

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test20150130/com.example.test20150130.MainScreenActivity}: java.lang.NullPointerException

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

at android.app.ActivityThread.access$800(ActivityThread.java:135)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:136)

at android.app.ActivityThread.main(ActivityThread.java:5017)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:515)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.NullPointerException

at org.apache.logging.log4j.util.ReflectionUtil$PrivateSecurityManager.getCallerClass(ReflectionUtil.java:301)

at org.apache.logging.log4j.util.ReflectionUtil.getCallerClass(ReflectionUtil.java:214)

at org.apache.logging.log4j.util.ReflectionUtil.getCallerClass(ReflectionUtil.java:193)

at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:72)

at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:195)

at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)

at org.apache.logging.log4j.LogManager.getContext(LogManager.java:160)

at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:492)

at com.example.test20150130.MainScreenActivity.delayedHide(MainScreenActivity.java:162)

at com.example.test20150130.MainScreenActivity.onPostCreate(MainScreenActivity.java:129)

at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1150)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

at android.app.ActivityThread.access$800(ActivityThread.java:135)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:136)

at android.app.ActivityThread.main(ActivityThread.java:5017)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:515)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

at dalvik.system.NativeStart.main(Native Method)

I'm not sure where the log4j2.xml file should be, so I trayed to put it on \src, on \src\main\java\com\example\test20150130 (same directory than the main activity), Test20150130\app\build\outputs\apk (same directory than the apk),...

Due to the error information, I tryed the getLogger everywhere: initially on the onCreate, then, I trayed it on an onClick event, but I got the same error.

(When I trayed to debug it, the sources I see are not aligned with the real execution...)

I had log4j already working on the Android project, but I've just migrated a Java EE project to log4j2 and I want to do the same on the Android project...

The log4j2 documentation has very few information about Android. It just says there are several modules not working on Android (JNDI, JMX,...), but nothing else...

Maybe it requires internally those modules (JNDI?)...

Is it possible to use log4j2 on Android?

Thank you very much in advance

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值