'Log4j2 Logs are not printed neither on the console or in the RollingFile when executed on Jenkins or in Command line

I pushed my automation framework to Github and integrated it with Jenkins. I used Log4j2 to capture the logs in the runtime. This works perfectly fine when I run in Eclipse but when I try to run the code from Command line or from Jenkins, Logs are not printed either on the console or on the Log file

My log4j2.xml is as following,

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
    <Property name="logDir">logs</Property>
</Properties>
<Appenders>
    <RollingFile name="RollingFile" fileName="${logDir}/app.log"
        filePattern="${logDir}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout>
            <Pattern>%d{dd:mm:yyy HH:mm:ss.SSS} %p %c{2} [%t] - %m%n</Pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="20MB" />
        </Policies>
    </RollingFile>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout  pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n" />
    </Console>
</Appenders>
<Loggers>
    <Logger name ="com.trifacta.stepDefinition" level="trace" additivity="true">
        <AppenderRef ref="Console"/>
    </Logger>
    <Logger name ="com.trifacta.stepDefinition" level="trace" additivity="true">
        <AppenderRef ref="RollingFile"/>
    </Logger>
    <Root level="trace">
        <AppenderRef ref="Console" />
    </Root>
</Loggers> </Configuration>

Class file is as below,

    package com.trifacta.stepDefinition;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;

import com.trifacta.pageObjects.Google;
import com.trifacta.utilities.BaseClass;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;

public class GoogleSearch extends BaseClass {
    
    Google go;
    Logger log = (Logger) LogManager.getLogger(GoogleSearch.class.getName());

    @Given("^user launches google$")
    public void user_launches_google() {
        driver.get(URL);
      log.info("Google is launched successfully");
    }

    @Then("^Enters \"([^\"]*)\" in search bar$")
    public void enters_in_search_bar(String value) {
        go = new Google();
        go.txtSearchBar(value);
        log.info("Search value is provided");
    }

    @Then("^Clicks enter$")
    public void clicks_enter() throws InterruptedException {
        go.hitEnter();
        log.info("Search Results are obtained");
        Thread.sleep(5000);
    }
    
}

Feature file

Feature: Launch Google

@google
Scenario: Launch Google in browser

Given user launches google
Then Enters "Selenium" in search bar
Then Clicks enter

Page

package com.trifacta.pageObjects;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.trifacta.utilities.BaseClass;

public class Google extends BaseClass {
    
    

    @FindBy(xpath = "//input[@class='gLFyf gsfi']")
    WebElement SearchBar;
    
        
    public Google() {
        PageFactory.initElements(driver, this);
    }
    
    public void txtSearchBar(String text) {
        SearchBar.sendKeys(text);
    }
    

    public void hitEnter() {
        SearchBar.sendKeys(Keys.ENTER);
    }
}

enter image description here

When I run this in eclipse logs are captured without any error, but when I run this in Jenkins or Command line logs are not captured. Can you please help me in getting this fix?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source