Log4j2在Android [英] Log4j2 on Android

查看:2892
本文介绍了Log4j2在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的Andr​​oid项目log4j2(我的工作在Android工作室)。

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

为了简化这个问题,我会explaine我在一个简单的虚拟项目已经做了(我在实际项目中同样的错误比这个虚拟项目)。

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.

我添加了log4j2库...

I add the log4j2 libs...

这是gradle这个:

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'
}

我已经减少了log4j2.xml这样:

I've reduced the log4j2.xml to this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/>
        <PatternLayout pattern="%m%n" />
    </Console>
</Appenders>

<Loggers>
    <root level="INFO">
        <AppenderRef ref="Console"/>
    </root>
    <logger name="Queries" level="ALL" additivity="false">
        <AppenderRef ref="Console"/>
    </logger>
</Loggers>

在活动中,我导入库...

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...");

我已经上了一个错误'记录仪记录= LogManager.getLogger(查询);

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)

我不知道哪里log4j2.xml文件应该是的,所以我板式把它放在\\ SRC上的\\ src \\主\\ java的\\ COM \\例子\\ test20150130(同一目录比主活动),Test20150130 \\程序\\编译\\输出\\ APK(同一目录比APK),......

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),...

由于错误的信息,我trye​​d的getLogger无处不在:最初的的onCreate,那么,我板式它onClick事件,但我得到了同样的错误。

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...)

我的log4j已经工作了Android项目,但我刚刚迁移的Java EE项目来log4j2,我希望做同样的Andr​​oid项目...

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...

该log4j2文档有关于Android很少的信息。它只是说,有几个模块没有工作在Android(JNDI,JMX,...),但没有别的...
也许它在内部需要这些模块(JNDI?)...

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?)...

寻找信息,我创办了这(<一个href=\"https://issues.apache.org/jira/browse/LOG4J2-446?jql=project%20%3D%20LOG4J2%20AND%20text%20~%20android\" rel=\"nofollow\">https://issues.apache.org/jira/browse/LOG4J2-446?jql=project%20%3D%20LOG4J2%20AND%20text%20~%20android)但我did'n找到一个解决方案...

Looking for information, I've founded this (https://issues.apache.org/jira/browse/LOG4J2-446?jql=project%20%3D%20LOG4J2%20AND%20text%20~%20android) but I did'n find a solution...

是否有可能在Android上使用log4j2?

Is it possible to use log4j2 on Android?

非常感谢你提前

推荐答案

根据你自己的答案,但有简化的code(使用 Configurator.initialize()而不是的 XmlConfigurationFactory 等):

Based on your own answer but with simplified code (using Configurator.initialize() instead of XmlConfigurationFactory etc.):

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

// Content of log4j2.xml
String log4j2xmlFileContent = getLog4j2xmlContent(); 

// convert String into InputStream
InputStream is = new ByteArrayInputStream(log4j2xmlFileContent.getBytes());

try {
    ConfigurationSource source = new ConfigurationSource(is);
    return Configurator.initialize(null, source);
} catch (IOException e) {
    e.printStackTrace();
}
return null;

需要注意的是:


  • 这code必须在例如在第一次调用之前运行 LogManager.getLogger()

  • 的成员函数 Configurator.initialize()不是log4j2公共API的一部分(见的这里

  • this code must run before the first call of e.g. LogManager.getLogger()
  • the member function Configurator.initialize() is not part of the log4j2 public API (see here)

另请参阅 LOG4J2-952 和<一个href=\"https://github.com/Joe-Merten/Mpjoe/blob/master/Common/src/main/java/de/jme/util/log/Log4jConfigure.java\"相对=nofollow> Log4jConfigure.java

这篇关于Log4j2在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆