18
Jul

Log automation run results

In order to collect information detailing timestamp of execution and steps performed during Automation execution, logging the steps is critical and can serve as a basis for tracking it for later use.

Log4j is an open source logging API available to meet following requirements of logging:

1. Categorize log type to provide whether logged step is FYI, Fatal or a warning. Log4j provides following classification of logs
Warn, Error, Fatal, Debug, Info, Trace
2. Ability to write logs to console, files or databases. With minimum or no efforts, one can write logs consistently across supported medias

Following example demonstrate how to use Log4j in your automation script:

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class SampleScript {
static Logger logger = LogManager.getLogger(this.getClass().getSimpleName());
BasicConfigurator.configure();

// Performing an action with WebDriver one can log steps as follow
logger.info(“Starting execution of xxxx functionality….”);

// if certain response deviates from expectation, we can log as
logger.warn(“Functionality xxxx mismatch observed….”);

}
}

Example to log supported log types with Log4j

logger.trace(“Trace Message …”);
logger.debug(“Debug Message …”);
logger.info(“Info Message …”);
logger.warn(“Warn Message …”);
logger.error(“Error Message …”);
logger.fatal(“Fatal Message …”);

Following table demonstrates log level and their visibility in log

Log4j level and their visibility in logs