Getting javalangClassCastException orgapacheloggingslf4jSLF4JLoggerContext cannot be cast to orgapachelogginglog4jcoreLoggerContext exception while executing simple Logging script | Selenium Forum
S
Sundar Srinivasan Posted on 13/12/2018
Hello Ashish,

I am learning Loggings from Module 7 and being heldup with this issue. I have installed the latest Log4j jar files from apache and created properties file like mentioned in the video. But am getting "ClassCastException" when executing below script. If am not wrong the latest jar files are not compatible with the properties file we made. Could you please help me with this issue? Thanks in Advance!

Log4j.properties

#Application Logs
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=C:\\Logs\\Log4j\\Application.log
#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=false

My code:
import org.apache.log4j.Logger;
public class test_Log4j
{
public static void main(String[] args)
{
Logger myLogger = Logger.getLogger("devpinoyLogger");
myLogger.debug("starting testcase");
myLogger.debug("logging in the app");
myLogger.debug("ending test");
}
}

Error:
Exception in thread "main" java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
at org.apache.log4j.Logger$PrivateManager.getContext(Logger.java:59)
at org.apache.log4j.Logger.getLogger(Logger.java:37)
at test_Log4j.main(test_Log4j.java:8)

A
Ashish Thakur Replied on 14/12/2018

Can you zip and attach project
Everything looks ok here


S
Sundar Srinivasan Replied on 14/12/2018

Attaching the Project as requested.


S
Sundar Srinivasan Replied on 14/12/2018

PFA


A
Ashish Thakur Replied on 16/12/2018

Please remove all the jars
You do not need so many
Please use attached 1 jar - log 4k jar


S
Sundar Srinivasan Replied on 18/12/2018

Hello,

Thanks for the informations. The script runs fine and the logs are being created. Could you please advise why this is working with old version of jar(1.2.17) that you provided but not with the latest version 2.11.1 ? I am trying to keep all the jar upto date so your advise would help us a lot. Thanks in advance!


A
Ashish Thakur Replied on 20/12/2018

Because the log file prop is supported by 1.2


S
Sundar Srinivasan Replied on 21/12/2018

Thanks again for the information. Is there any sources we can refer to update the property file that supports the latest jar files? I am working at my best exploring and learning but just thought of asking you.


A
Ashish Thakur Replied on 21/12/2018


Logs are used to generate a file so that we know what happens when script was running. However these days extent reports have come up and they hae very good logs. So practically Log4J is not required because extent reports do everything. Its explained in frameworks. I am soon going to remove log4j videos.


S
Sundar Srinivasan Replied on 21/12/2018

That makes perfect sense. Thanks for the clarification. This ticket shall be closed.