It is working now. I removed log4j2.xml from the project and instead added log4j2.component.properties and pointed log4j2.xml path value in it.
log4j2.component.properties
log4j.configurationFile=/com/logs/test/log4j2.xml