5

I'm trying to integrate Log4j2 in Android but getting the following error message:

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

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.SyslogAppender;

  LoggerContext context = (LoggerContext) LogManager.getContext();
  Logger root = context.getLogger("MainActivity");
  SyslogAppender appender = obtainAppender();

  if (!appender.isStarted()) 
    appender.start();

  root.addAppender(appender);
  root.setAdditive(false);
  root.setLevel(Level.DEBUG);
  root.debug("This is a test message");
ADTC
  • 8,999
  • 5
  • 68
  • 93
Gkapoor
  • 840
  • 1
  • 13
  • 27

1 Answers1

1

You need to have both the core and api jar in the classpath. (See the log4j2 FAQ page.)

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • 1
    Yes, both log4j-api-2.0-beta9.jar & log4j-core-2.0-beta9.jar – Gkapoor Dec 04 '13 at 04:28
  • After trying the following solution now I'm getting this error <> and Logger LOG = LogManager.getLogger(MainActivity.class); LOG.error("Test Log4j2"); – Gkapoor Dec 04 '13 at 19:07
  • I am still suspecting some classpath issue. Have you tried configuring with ... ? This will print log4j2 internal log statements to the console. (Thanks for filing a JIRA for this in the log4j2 issue tracker! I am only answering your question here because the apache jira server seems to be down now. Better to continue in the issue tracker so that other log4j team members can help too.) – Remko Popma Dec 04 '13 at 21:54