Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-1921

Getting ClassCastException while getting LoggerContext

    Details

    • Type: Bug
    • Status: Open
    • Priority: Blocker
    • Resolution: Unresolved
    • Affects Version/s: 2.8.2
    • Fix Version/s: None
    • Component/s: Configurators
    • Labels:
    • Environment:

      Android

      Description

      Hi,

      I integrated the log4j with my Android application and did the initial config. It works fine. I have a menu item in "Settings" screen where I can change the log levels in my application. So, After I change the configuration, I need to update the loggers. So I am using following logic.

      LoggerContext ctx = LoggerContext.getContext()
              Configuration config = ctx.getConfiguration();
              LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
              loggerConfig.setLevel(myLogLevel);
              ctx.updateLoggers();
      

      While getting the logger context, I am getting a ClassCastException.

      05-26 22:33:21.011 E/AndroidRuntime( 9642): Caused by: java.lang.ClassCastException: org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
      05-26 22:33:21.011 E/AndroidRuntime( 9642): at org.apache.logging.log4j.core.LoggerContext.getContext(LoggerContext.java:190)
      05-26 22:33:21.011 E/AndroidRuntime( 9642): at org.apache.logging.log4j.core.config.Configurator.setRootLevel(Configurator.java:323)

      Please help here.

        Issue Links

          Activity

          Hide
          garydgregory Gary Gregory added a comment -

          Can you try one of the org.apache.logging.log4j.core.config.Configurator setLevel() method?

          Show
          garydgregory Gary Gregory added a comment - Can you try one of the org.apache.logging.log4j.core.config.Configurator setLevel() method?
          Hide
          Ajitha Ajitha added a comment - - edited

          @Greg: Same crash is being observed with:

          Configurator.setLevel(LogManager.ROOT_LOGGER_NAME, loglvl);

          Caused by: java.lang.ClassCastException: org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext

          Show
          Ajitha Ajitha added a comment - - edited @Greg: Same crash is being observed with: Configurator.setLevel(LogManager.ROOT_LOGGER_NAME, loglvl); Caused by: java.lang.ClassCastException: org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          This does not look like a complete stack trace. Can you please provide a stack trace that includes all the Log4j stack elements?

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - This does not look like a complete stack trace. Can you please provide a stack trace that includes all the Log4j stack elements?
          Hide
          Ajitha Ajitha added a comment -

          I hope this is ok::

          05-30 11:54:54.806 E/AndroidRuntime( 4642): Caused by: java.lang.ClassCastException: org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
          05-30 11:54:54.806 E/AndroidRuntime( 4642): at org.apache.logging.log4j.core.LoggerContext.getContext(LoggerContext.java:190)
          05-30 11:54:54.806 E/AndroidRuntime( 4642): at org.apache.logging.log4j.core.config.Configurator.setLevel(Configurator.java:291)

          Since we are using this in a enterprise application officially, I am bound not to give complete traces further... I hope this is sufficient.

          Show
          Ajitha Ajitha added a comment - I hope this is ok:: 05-30 11:54:54.806 E/AndroidRuntime( 4642): Caused by: java.lang.ClassCastException: org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext 05-30 11:54:54.806 E/AndroidRuntime( 4642): at org.apache.logging.log4j.core.LoggerContext.getContext(LoggerContext.java:190) 05-30 11:54:54.806 E/AndroidRuntime( 4642): at org.apache.logging.log4j.core.config.Configurator.setLevel(Configurator.java:291) Since we are using this in a enterprise application officially, I am bound not to give complete traces further... I hope this is sufficient.
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          OK, I guess I understand what you are doing. Unfortunately, this doesn't help diagnose the problem. The issue here is that you are probably seeing a message saying it cannot find the logging implementation. When that happens it uses SimpleLogger and the log4j-core Configurator cannot do anything with that implementation. I would suggest you add status="debug" to the configuration element in your log4j2.xml and supply the output of that.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - OK, I guess I understand what you are doing. Unfortunately, this doesn't help diagnose the problem. The issue here is that you are probably seeing a message saying it cannot find the logging implementation. When that happens it uses SimpleLogger and the log4j-core Configurator cannot do anything with that implementation. I would suggest you add status="debug" to the configuration element in your log4j2.xml and supply the output of that.
          Hide
          Ajitha Ajitha added a comment -

          @Ralph: Your information helps a bit. I had one problem in the integration. The log4j-api-2.8.3 has some problem with Android because Android does not support Java9 yet(expected in mere future) and I think log4j-api-2.8.3 has classes compiled in that version of java like ClassNamePredicate.class.

          So I took 2.8.2 which was compatible and then took log4j-core-2.8.3 . Hence i think there is some problem.Can you give me some additional pointers to integrate with android?

          And one small question. Do you have any sample application for Android with log4j application that I can refer to?

          Show
          Ajitha Ajitha added a comment - @Ralph: Your information helps a bit. I had one problem in the integration. The log4j-api-2.8.3 has some problem with Android because Android does not support Java9 yet(expected in mere future) and I think log4j-api-2.8.3 has classes compiled in that version of java like ClassNamePredicate.class. So I took 2.8.2 which was compatible and then took log4j-core-2.8.3 . Hence i think there is some problem.Can you give me some additional pointers to integrate with android? And one small question. Do you have any sample application for Android with log4j application that I can refer to?
          Hide
          jvz Matt Sicker added a comment -

          I don't know much about Android other than the fact that it's missing a lot of standard JDK classes. However, the symptoms you're describing sound like while you're compiling fine against log4j-core, it doesn't appear to be packaged in your application or there is an initialization error that should output a status log message indicating so.

          Show
          jvz Matt Sicker added a comment - I don't know much about Android other than the fact that it's missing a lot of standard JDK classes. However, the symptoms you're describing sound like while you're compiling fine against log4j-core, it doesn't appear to be packaged in your application or there is an initialization error that should output a status log message indicating so.
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          I guess you are saying you have to compile the log4j code, which makes sense. We should be able to modify the build to not compile the Java 9 stuff if Java 9 isn't present (or if the target is Android).

          I don't know of anyone who has used Log4j on Android. I was under the impression that Andriod made it difficult to use a third party logging framework and you were stuck with java.util.logging.

          Do you have the debug output from Log4j?

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - I guess you are saying you have to compile the log4j code, which makes sense. We should be able to modify the build to not compile the Java 9 stuff if Java 9 isn't present (or if the target is Android). I don't know of anyone who has used Log4j on Android. I was under the impression that Andriod made it difficult to use a third party logging framework and you were stuck with java.util.logging. Do you have the debug output from Log4j?
          Hide
          Ajitha Ajitha added a comment -

          @Ralph et all :

          1) Android does not support java 9 yet. So I m not able to use the jar file. It wil take another few months for the support to be added.

          2) since the api jar file is not compatible, I was not able to use it. So I thought of using 2.8.2 version of jar for log4j api and 2.8.3 version of jar for log4j core. This is where I got the class cast exception. I guess log4j does not allow incompatible core and api jars.

          I m working from India. So tomorrow during my day, I shall try taking the source code of 2.8.3 api and 2.8.3 core and try compiling it from Android studio directly.

          I will get back to you in this thread in case of issues.

          Usually we - Android developers used to take the links from maven repo and put it in gradle . Now we will take the source from apache repo and try building it with the Java compiler that comes with Android SDK.

          If there is any modifications that I need to do for avoiding java 9 classes let me know.

          Show
          Ajitha Ajitha added a comment - @Ralph et all : 1) Android does not support java 9 yet. So I m not able to use the jar file. It wil take another few months for the support to be added. 2) since the api jar file is not compatible, I was not able to use it. So I thought of using 2.8.2 version of jar for log4j api and 2.8.3 version of jar for log4j core. This is where I got the class cast exception. I guess log4j does not allow incompatible core and api jars. I m working from India. So tomorrow during my day, I shall try taking the source code of 2.8.3 api and 2.8.3 core and try compiling it from Android studio directly. I will get back to you in this thread in case of issues. Usually we - Android developers used to take the links from maven repo and put it in gradle . Now we will take the source from apache repo and try building it with the Java compiler that comes with Android SDK. If there is any modifications that I need to do for avoiding java 9 classes let me know.
          Hide
          mikaelstaldal Mikael Ståldal added a comment -

          Can it be so that ProGuard is removing some classes from log4j-core which are needed?

          Can you try to disable ProGuard, or configure it to keep everything in org.apache.logging.log4j.*?

          Show
          mikaelstaldal Mikael Ståldal added a comment - Can it be so that ProGuard is removing some classes from log4j-core which are needed? Can you try to disable ProGuard, or configure it to keep everything in org.apache.logging.log4j.* ?
          Hide
          mikaelstaldal Mikael Ståldal added a comment -

          Android actually has it's own logging "framework": https://developer.android.com/reference/android/util/Log.html

          Show
          mikaelstaldal Mikael Ståldal added a comment - Android actually has it's own logging "framework": https://developer.android.com/reference/android/util/Log.html
          Hide
          Ajitha Ajitha added a comment -

          I have not enabled proguard.

          Ya am aware of Androids log class. I will check build and update this ticket.

          Show
          Ajitha Ajitha added a comment - I have not enabled proguard. Ya am aware of Androids log class. I will check build and update this ticket.
          Hide
          mikaelstaldal Mikael Ståldal added a comment -
          Show
          mikaelstaldal Mikael Ståldal added a comment - Is this a duplicate of https://issues.apache.org/jira/browse/LOG4J2-461 ?
          Hide
          Ajitha Ajitha added a comment - - edited

          Yes it is exactly 460 rather than 461...
          https://issues.apache.org/jira/browse/LOG4J2-460.

          I am running a problem because of which this ticket is open:
          >In Android, we refer repositories directly from maven. https://mvnrepository.com/artifact/org.apache.logging.log4j
          >The issue "https://issues.apache.org/jira/browse/LOG4J2-1920" was fixed in 2.8.3 and not yet deployed in maven. I took 2.8.3 from apache snapshot. But when I take from Apache snapshot, it is not properly linked with Android and few cases are failing. Because of that I suspect this issue to occur. Initialisation was successful, but getting logger context is crashing. I closed bug 1920 as init was fine.

          So, if you deploy the fix for "LOG4J2-1920" in maven repository, I would be able to retest and close this issue. I am helpless.

          Show
          Ajitha Ajitha added a comment - - edited Yes it is exactly 460 rather than 461... https://issues.apache.org/jira/browse/LOG4J2-460 . I am running a problem because of which this ticket is open: >In Android, we refer repositories directly from maven. https://mvnrepository.com/artifact/org.apache.logging.log4j >The issue "https://issues.apache.org/jira/browse/LOG4J2-1920" was fixed in 2.8.3 and not yet deployed in maven. I took 2.8.3 from apache snapshot. But when I take from Apache snapshot, it is not properly linked with Android and few cases are failing. Because of that I suspect this issue to occur. Initialisation was successful, but getting logger context is crashing. I closed bug 1920 as init was fine. So, if you deploy the fix for " LOG4J2-1920 " in maven repository, I would be able to retest and close this issue. I am helpless.
          Hide
          garydgregory Gary Gregory added a comment -

          Note that the current SNAPSHOT version was updated to 2.9-SNAPSHOT since our next release will be 2.9.

          Show
          garydgregory Gary Gregory added a comment - Note that the current SNAPSHOT version was updated to 2.9-SNAPSHOT since our next release will be 2.9.
          Hide
          Ajitha Ajitha added a comment -

          Thanks. But when can I expect it to get updated in "https://mvnrepository.com"

          Show
          Ajitha Ajitha added a comment - Thanks. But when can I expect it to get updated in "https://mvnrepository.com"
          Hide
          garydgregory Gary Gregory added a comment - - edited

          That site does not pick up SNAPSHOT builds. You need to point your build to https://repository.apache.org/content/repositories/snapshots if you want to test SNAPSHOT builds.

          Show
          garydgregory Gary Gregory added a comment - - edited That site does not pick up SNAPSHOT builds. You need to point your build to https://repository.apache.org/content/repositories/snapshots if you want to test SNAPSHOT builds.
          Hide
          Ajitha Ajitha added a comment - - edited

          ok, in my Android gradle build, I added

          repositories {
          mavenCentral()
          maven

          { url 'https://repository.apache.org/content/repositories/snapshots' }

          }

          and tried accessing it like:

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

          And I am getting :
          Error:Failed to resolve: org.apache.logging.log4j:log4j-api:2.9

          What else should I do to include the snapshot build?

          Show
          Ajitha Ajitha added a comment - - edited ok, in my Android gradle build, I added repositories { mavenCentral() maven { url 'https://repository.apache.org/content/repositories/snapshots' } } and tried accessing it like: compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.9' compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9' And I am getting : Error:Failed to resolve: org.apache.logging.log4j:log4j-api:2.9 What else should I do to include the snapshot build?
          Hide
          garydgregory Gary Gregory added a comment -

          It's 2.9-SNAPSHOT, not 2.9

          Show
          garydgregory Gary Gregory added a comment - It's 2.9-SNAPSHOT , not 2.9
          Hide
          Ajitha Ajitha added a comment -

          No luck it says:

          Error:Failed to resolve: org.apache.logging.log4j:log4j-api:2.9-SNAPSHOT

          Show
          Ajitha Ajitha added a comment - No luck it says: Error:Failed to resolve: org.apache.logging.log4j:log4j-api:2.9-SNAPSHOT
          Hide
          garydgregory Gary Gregory added a comment - - edited

          How about:

          maven { 
            url 'https://repository.apache.org/content/repositories/snapshots'
            snapshot true
          }
          

          ?

          Show
          garydgregory Gary Gregory added a comment - - edited How about: maven { url 'https://repository.apache.org/content/repositories/snapshots' snapshot true } ?
          Hide
          Ajitha Ajitha added a comment -
          Error:(11, 0) Could not find method snapshot() for arguments [true] on object of type org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository.
          
          
          Show
          Ajitha Ajitha added a comment - Error:(11, 0) Could not find method snapshot() for arguments [ true ] on object of type org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository.
          Hide
          Ajitha Ajitha added a comment -

          My bad! Firewall block. It identified the build now. Will update you shortly!

          Show
          Ajitha Ajitha added a comment - My bad! Firewall block. It identified the build now. Will update you shortly!
          Hide
          Ajitha Ajitha added a comment - - edited

          Android does not support Java 9 classes. It throws error in StackLocator.class:

          AGPBI: {"kind":"error","text":"Error converting bytecode to dex:\nCause: Dex cannot parse version 53 byte code.\nThis is caused by library dependencies that have been compiled using Java 8 or above.\nIf you are using the \u0027java\u0027 gradle plugin in a library submodule add \ntargetCompatibility \u003d \u00271.7\u0027\nsourceCompatibility \u003d \u00271.7\u0027\nto that submodule\u0027s build.gradle file.\n...while parsing META-INF/versions/9/org/apache/logging/log4j/util/StackLocator.class","sources":[{}],"original":"PARSE ERROR:\nunsupported class file version 53.0\n...while parsing META-INF/versions/9/org/apache/logging/log4j/util/StackLocator.class\n","tool":"Dex"}
          AGPBI: {"kind":"error","text":"1 error; aborting","sources":[{}]}
          
          Show
          Ajitha Ajitha added a comment - - edited Android does not support Java 9 classes. It throws error in StackLocator.class: AGPBI: { "kind" : "error" , "text" : "Error converting bytecode to dex:\nCause: Dex cannot parse version 53 byte code.\nThis is caused by library dependencies that have been compiled using Java 8 or above.\nIf you are using the \u0027java\u0027 gradle plugin in a library submodule add \ntargetCompatibility \u003d \u00271.7\u0027\nsourceCompatibility \u003d \u00271.7\u0027\nto that submodule\u0027s build.gradle file.\n... while parsing META-INF/versions/9/org/apache/logging/log4j/util/StackLocator.class" , "sources" :[{}], "original" : "PARSE ERROR:\nunsupported class file version 53.0\n... while parsing META-INF/versions/9/org/apache/logging/log4j/util/StackLocator.class\n" , "tool" : "Dex" } AGPBI: { "kind" : "error" , "text" : "1 error; aborting" , "sources" :[{}]}
          Hide
          garydgregory Gary Gregory added a comment -

          ATM, the 2.9-SNAPSHOT log4j-api jar includes a Java 9 classes. We are discussing how to deal with Java 9 and Android on the development mailing list.

          Show
          garydgregory Gary Gregory added a comment - ATM, the 2.9-SNAPSHOT log4j-api jar includes a Java 9 classes. We are discussing how to deal with Java 9 and Android on the development mailing list.
          Hide
          Ajitha Ajitha added a comment -

          Thanks for the interim update.

          Show
          Ajitha Ajitha added a comment - Thanks for the interim update.
          Hide
          Ajitha Ajitha added a comment - - edited

          Is there a update on this? Can you please find a way to support Android as well? We have been using apache from many days now Replacing the logging framework is quite huge at this moment for us. So, if you give us an alternative, it would be great.

          Show
          Ajitha Ajitha added a comment - - edited Is there a update on this? Can you please find a way to support Android as well? We have been using apache from many days now Replacing the logging framework is quite huge at this moment for us. So, if you give us an alternative, it would be great.
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          I created a branch named "android" which has a log4j-api-android module in it. I believe it should work in android but I don't have an environment to test it. If you could check out that branch and test it I would then be more comfortable merging it to master. Note that you would use the log4j-api-andoid jar instead of the log4j-api jar.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - I created a branch named "android" which has a log4j-api-android module in it. I believe it should work in android but I don't have an environment to test it. If you could check out that branch and test it I would then be more comfortable merging it to master. Note that you would use the log4j-api-andoid jar instead of the log4j-api jar.
          Hide
          Ajitha Ajitha added a comment -

          Can you guide me how to pull it via gradle?

          Show
          Ajitha Ajitha added a comment - Can you guide me how to pull it via gradle?
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          Gradie? We use Maven as our build tool and git for source control. You can do git clone -b android https://github.com/apache/logging-log4j2. Once you have it checked out run mvn clean install in the root directory.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - Gradie? We use Maven as our build tool and git for source control. You can do git clone -b android https://github.com/apache/logging-log4j2 . Once you have it checked out run mvn clean install in the root directory.
          Hide
          Ajitha Ajitha added a comment -

          I will update you in a day.

          Show
          Ajitha Ajitha added a comment - I will update you in a day.
          Hide
          Ajitha Ajitha added a comment -

          Hi Team,

          I did as mentioned above. I built logging-log4j2\log4j-api-android from the given branch. It got built successfuly and generated jar. But log4j-core from same branch was not compiling successfully. It was failing. So, I took latest log4j-core jar from "https://repository.apache.org/content/groups/snapshots/org/apache/logging/log4j/log4j-core/2.9-SNAPSHOT/" . I put both the jar in my Android project and then tried to build.

          Luckily the issue with java 9 classes were not coming this time. Thumbs up guys!

          But after running it on Android device, I got the "ClassCastException" that is seen in this problem original statement.

          My Android side implementation is exactly similar to the application here : https://github.com/loune/log4j2-android

          One change in above example is made:

          https://github.com/loune/log4j2-android/blob/master/Log4jExampleApp/app/src/main/java/net/loune/log4j2android/AndroidContextSelector.java

          Line no 34:

          Configuration config = org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getInstance().getConfiguration(source);
          

          Replaced with:

          Configuration config = org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getInstance().getConfiguration(context, source);
          

          Please check if we are missing something while initialising the Log4j. Log4j has helped us a lot of times Expecting your support. Thanks.

          -Ajitha Yasmin S

          Show
          Ajitha Ajitha added a comment - Hi Team, I did as mentioned above. I built logging-log4j2\log4j-api-android from the given branch. It got built successfuly and generated jar. But log4j-core from same branch was not compiling successfully. It was failing. So, I took latest log4j-core jar from "https://repository.apache.org/content/groups/snapshots/org/apache/logging/log4j/log4j-core/2.9-SNAPSHOT/" . I put both the jar in my Android project and then tried to build. Luckily the issue with java 9 classes were not coming this time . Thumbs up guys! But after running it on Android device, I got the "ClassCastException" that is seen in this problem original statement. My Android side implementation is exactly similar to the application here : https://github.com/loune/log4j2-android One change in above example is made: https://github.com/loune/log4j2-android/blob/master/Log4jExampleApp/app/src/main/java/net/loune/log4j2android/AndroidContextSelector.java Line no 34: Configuration config = org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getInstance().getConfiguration(source); Replaced with: Configuration config = org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getInstance().getConfiguration(context, source); Please check if we are missing something while initialising the Log4j. Log4j has helped us a lot of times Expecting your support. Thanks. -Ajitha Yasmin S
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          You should not have needed Log4J-core.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - You should not have needed Log4J-core.
          Hide
          remkop@yahoo.com Remko Popma added a comment -

          My understanding is that log4j-api-android is an implementation of the Log4j2 API that delegates logging calls to the logging facilities built into Android. So you don't need the "normal" implementation (which is log4j-core).

          Show
          remkop@yahoo.com Remko Popma added a comment - My understanding is that log4j-api-android is an implementation of the Log4j2 API that delegates logging calls to the logging facilities built into Android. So you don't need the "normal" implementation (which is log4j-core).
          Hide
          mikaelstaldal Mikael Ståldal added a comment -

          log4j-core does not support Android, and is not needed when you use log4j-api-android.

          Show
          mikaelstaldal Mikael Ståldal added a comment - log4j-core does not support Android, and is not needed when you use log4j-api-android.
          Hide
          Ajitha Ajitha added a comment -

          Ok, I am rewritting my implementation and checking. Shall update you by my EOD (IST).

          Thanks team!

          Show
          Ajitha Ajitha added a comment - Ok, I am rewritting my implementation and checking. Shall update you by my EOD (IST). Thanks team!
          Hide
          Ajitha Ajitha added a comment - - edited

          Friends,

          I want to give a background on the requirement, so that you can understand my problem better.

          We were mainly using log4j for following purposes:
          >I dont want logs to be written to the default Android's adb which is what "android.util.Log.d" is doing. My end user will share that file via email for me to debug their issues/problems.
          >We wanted to ALWAYS redirect the logs to a file rather than default logging console of Android.
          >We have two layers - application layer and middleware layer. Log4j was thread-safe to post logs
          >All other platforms like Windows were using log4j and we were aligned with other platforms of our application.

          The code we follow is exactly like in "https://github.com/loune/log4j2-android" . Our application was using v2.3 of log4j-api and log4j-core. And it was fine and due to some issue we wanted to upgrade and we faced the problem.

          We were doing following:
          1)On the app launch, we initialised log4j as in the example app using my "xml configuration" file as shown in https://github.com/loune/log4j2-android/blob/master/Log4jExampleApp/app/src/main/java/net/loune/log4jexampleapp/App.java

          AndroidLog4jHelper.initialise(this.getApplicationContext(), R.raw.log4j_debug);
          

          2) After that user can anytime change the logging levels through a menu option and when the user changes the log level, we used below code to update log levels.

                  LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
                  Configuration config = ctx.getConfiguration();
                  LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
                  loggerConfig.setLevel(loglvl);
                  ctx.updateLoggers();
          

          This was my total use-case.

          In latest release of log4j-api-android , I am not able to see any methods which can get my config file and give me a output file with logs written. As much as I understand it writes logs to the android logging console which is not my use-case.

          I am not able to proceed. Is it just possible to remove the "java 9" classes from log4j-core and give us a release addressing our use-case.

          Sorry to trouble you guys. We have been benefitted by your library greatly which was very much robust. Please help us here.

          -Ajitha Yasmin S

          Show
          Ajitha Ajitha added a comment - - edited Friends, I want to give a background on the requirement, so that you can understand my problem better. We were mainly using log4j for following purposes: >I dont want logs to be written to the default Android's adb which is what "android.util.Log.d" is doing. My end user will share that file via email for me to debug their issues/problems. >We wanted to ALWAYS redirect the logs to a file rather than default logging console of Android. >We have two layers - application layer and middleware layer. Log4j was thread-safe to post logs >All other platforms like Windows were using log4j and we were aligned with other platforms of our application. The code we follow is exactly like in "https://github.com/loune/log4j2-android" . Our application was using v2.3 of log4j-api and log4j-core. And it was fine and due to some issue we wanted to upgrade and we faced the problem. We were doing following: 1)On the app launch, we initialised log4j as in the example app using my "xml configuration" file as shown in https://github.com/loune/log4j2-android/blob/master/Log4jExampleApp/app/src/main/java/net/loune/log4jexampleapp/App.java AndroidLog4jHelper.initialise( this .getApplicationContext(), R.raw.log4j_debug); 2) After that user can anytime change the logging levels through a menu option and when the user changes the log level, we used below code to update log levels. LoggerContext ctx = (LoggerContext) LogManager.getContext( false ); Configuration config = ctx.getConfiguration(); LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); loggerConfig.setLevel(loglvl); ctx.updateLoggers(); This was my total use-case. In latest release of log4j-api-android , I am not able to see any methods which can get my config file and give me a output file with logs written. As much as I understand it writes logs to the android logging console which is not my use-case. I am not able to proceed. Is it just possible to remove the "java 9" classes from log4j-core and give us a release addressing our use-case. Sorry to trouble you guys. We have been benefitted by your library greatly which was very much robust. Please help us here. -Ajitha Yasmin S
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment - - edited

          Above you mention you use xml configuration but I don't see one in that app. Can you provide that? Log4j-API-android is providing an implementation that we believe makes sense on android but without knowing what your logging configuration does we cannot know for sure.

          Log4J-core does not Have any Java 9 classes.

          Log4J 2.3 was the last release to support java 6. Is there something in log4j-core that uses Java 7 that android does not allow? Probably a lot actually.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - - edited Above you mention you use xml configuration but I don't see one in that app. Can you provide that? Log4j-API-android is providing an implementation that we believe makes sense on android but without knowing what your logging configuration does we cannot know for sure. Log4J-core does not Have any Java 9 classes. Log4J 2.3 was the last release to support java 6. Is there something in log4j-core that uses Java 7 that android does not allow? Probably a lot actually.
          Show
          Ajitha Ajitha added a comment - Refer sample configuration file: https://github.com/loune/log4j2-android/tree/master/Log4jExampleApp/app/src/main/res/raw
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          OK. The Logcat appender does essentially what log4j-api-android does. But log4j-api-android doesn't have support for the rolling file appender, although it should be possible to include it or something like it. We have made a lot of changes to log4j-core since 2.3 and I have no idea if it can still be made to work in Android.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - OK. The Logcat appender does essentially what log4j-api-android does. But log4j-api-android doesn't have support for the rolling file appender, although it should be possible to include it or something like it. We have made a lot of changes to log4j-core since 2.3 and I have no idea if it can still be made to work in Android.
          Hide
          Ajitha Ajitha added a comment -

          Ok Ralph. In that case, we will also look for alternatives. Thanks for your extensive support. Happy weekend!

          Show
          Ajitha Ajitha added a comment - Ok Ralph. In that case, we will also look for alternatives. Thanks for your extensive support. Happy weekend!
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          Although I'm not sure when I will have the time I'd love the see how we can support your sample project. It looks like I should just be able to load it into Android Developer Studio (or Intellij) and test it. But not having ever worked with Android I am certain there will be a learning curve.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - Although I'm not sure when I will have the time I'd love the see how we can support your sample project. It looks like I should just be able to load it into Android Developer Studio (or Intellij) and test it. But not having ever worked with Android I am certain there will be a learning curve.
          Hide
          Ajitha Ajitha added a comment - - edited

          We are working on alternatives. But If you are interested in Android support,

          1)Install Studio
          2)Follow the wizard and allow your machine to download latest SDK as wizard says.
          3)Import the attachment project (I have attached in this JIRA) and
          4) You should be able to run the app on any android device
          /Users/ajitha3008/StudioProjects/Log4jExample_using_v2.3.zip
          P.S: Enable "USB debugging" in device. Google has many links to do the same.
          The output log file will be seen in location: /Android/data/com.braingalore.log4jexample/files/logs/app.log

          You can open this with notepad. Use ESFileExplorer to reach to the location to take the log file.

          Now we are trying to achieve the upgradation of log4j using the instructions that support grade from https://logging.apache.org/log4j/2.x/maven-artifacts.html (Android supports cradle build). The configurations of gradle can be found in the file app/build.gradle . It may be pointing to the "maven" repo of the apache libs. You can change it to point to apache repo and try to solve the issues that you face. My comments in this bug on 06/Jul/17 09:02 will tell how I tried to achieve the changing of maven repo to apache repo.

          This could be a learning curve definitely and logToFileWriter is a great need and when this is ready-made people would love to use your app taking "apache" brand in consideration.

          -Ajitha Yasmin

          Show
          Ajitha Ajitha added a comment - - edited We are working on alternatives. But If you are interested in Android support, 1)Install Studio 2)Follow the wizard and allow your machine to download latest SDK as wizard says. 3)Import the attachment project (I have attached in this JIRA) and 4) You should be able to run the app on any android device /Users/ajitha3008/StudioProjects/Log4jExample_using_v2.3.zip P.S: Enable "USB debugging" in device. Google has many links to do the same. The output log file will be seen in location: /Android/data/com.braingalore.log4jexample/files/logs/app.log You can open this with notepad. Use ESFileExplorer to reach to the location to take the log file. Now we are trying to achieve the upgradation of log4j using the instructions that support grade from https://logging.apache.org/log4j/2.x/maven-artifacts.html (Android supports cradle build). The configurations of gradle can be found in the file app/build.gradle . It may be pointing to the "maven" repo of the apache libs. You can change it to point to apache repo and try to solve the issues that you face. My comments in this bug on 06/Jul/17 09:02 will tell how I tried to achieve the changing of maven repo to apache repo. This could be a learning curve definitely and logToFileWriter is a great need and when this is ready-made people would love to use your app taking "apache" brand in consideration. -Ajitha Yasmin
          Hide
          william_f_au William Ferguson added a comment -

          I am also interested in using log4j2 on Android.
          And like Ajitha I'm wanting to log using a RollingFileAppender instead of Android Log (as Android Log is a circular buffer that is shared among all apps/components on the phone)

          I've had a look at the android branch and it seems to just stand up a Logger that will redirect to Android Log.
          What work needs to be done to enhance that branch so that on an Android VM we can use RollingFileAppenders?

          Show
          william_f_au William Ferguson added a comment - I am also interested in using log4j2 on Android. And like Ajitha I'm wanting to log using a RollingFileAppender instead of Android Log (as Android Log is a circular buffer that is shared among all apps/components on the phone) I've had a look at the android branch and it seems to just stand up a Logger that will redirect to Android Log. What work needs to be done to enhance that branch so that on an Android VM we can use RollingFileAppenders?
          Hide
          mounicakodela Mounica Kodela added a comment -

          Hi Team,

          I did not find any log4j-api-android in the below github link to try the above steps to make it work in my android app.

          https://github.com/apache/logging-log4j2

          Please help me

          Show
          mounicakodela Mounica Kodela added a comment - Hi Team, I did not find any log4j-api-android in the below github link to try the above steps to make it work in my android app. https://github.com/apache/logging-log4j2 Please help me
          Hide
          ralph.goers@dslextreme.com Ralph Goers added a comment -

          It is on a branch named "android". But please read everything above as it may not do what you want.

          Show
          ralph.goers@dslextreme.com Ralph Goers added a comment - It is on a branch named "android". But please read everything above as it may not do what you want.
          Hide
          mounicakodela Mounica Kodela added a comment - - edited

          Ralph Goers We do not have the exact issue. We have an issue using 2.9.0 for our android application which i reported in LOG4J2-2038

          Previously, we were using 2.3. We have a problem with oreo devices with v2.3 which is resolved in 2.9.0 as per the status of LOG4J2-1920. So, we need to use 2.9.0 to avoid crashing for Android oreo devices. We need help to resolve this.

          Thanks,
          Mounica

          Show
          mounicakodela Mounica Kodela added a comment - - edited Ralph Goers We do not have the exact issue. We have an issue using 2.9.0 for our android application which i reported in LOG4J2-2038 Previously, we were using 2.3. We have a problem with oreo devices with v2.3 which is resolved in 2.9.0 as per the status of LOG4J2-1920 . So, we need to use 2.9.0 to avoid crashing for Android oreo devices. We need help to resolve this. Thanks, Mounica

            People

            • Assignee:
              Unassigned
              Reporter:
              Ajitha Ajitha
            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:

                Development