> For the complete documentation index, see [llms.txt](https://n-saikiran.gitbook.io/selenium-2-web-driver-basics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://n-saikiran.gitbook.io/selenium-2-web-driver-basics/4.-selenium-prospects/4.4-generate-logs.md).

# 4.4 Generate Logs

If you have more than 30-40 tests to run in a suite, then it will be very difficult to track which script is being executed at what time. Also error warnings, time elapsed, execution status etc. cannot be tracked. In order to overcome these limitations we introduce logging functionality to our automation framework.

You need to download log4j jar from [Apache log4j 1.2 – Download Apache log4j 1.2](http://logging.apache.org/log4j/1.2/download.html). Place the log4j jar in your lib folder and add the same to build path. create a package with name testListener in your src folder. Create a class with name TestMonitor in this package and copy the below code to this class.

```
package testListener;

package testListener;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.testng.ITestContext;
import org.testng.ITestResult;

public class TestMonitor implements org.testng.ITestListener {

    public Logger log = Logger.getLogger(this.getClass().getName());

    @Override
    public void onFinish(ITestContext arg0) {
        log.info("Test " + arg0.getName() + " Ends");
        log.info("---------------------------------------------------------");
    }

    @Override
    public void onStart(ITestContext arg0) {
        PropertyConfigurator.configure("src/log4j.properties");
        log.info("Test " + arg0.getName() + " Starts");
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
        log.info("Test failed within success Percentage");
    }

    @Override
    public void onTestFailure(ITestResult arg0) {
        log.info("Test run has failed");
        log.error(arg0.getThrowable().getMessage());

        StringWriter sw = new StringWriter();
        arg0.getThrowable().printStackTrace(new PrintWriter(sw));
        String stacktrace = sw.toString();
        log.error(stacktrace.trim());
    }

    @Override
    public void onTestSkipped(ITestResult arg0) {
        log.info("Test skipped");
    }

    @Override
    public void onTestStart(ITestResult arg0) {
        log.info("Test Method " + arg0.getMethod().getMethodName()
                + " executing...");
    }

    @Override
    public void onTestSuccess(ITestResult arg0) {
        log.info("Test run is successful");
    }

}
```

Now create a file name log4j.properties in the src folder and copy paste the below lines to this file.

```
# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logsSeleniumLogs.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
```

When your run this suite, the logs will be generated as and when a script is taken up for execution. these logs can be found in the directory seleniumLogs.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://n-saikiran.gitbook.io/selenium-2-web-driver-basics/4.-selenium-prospects/4.4-generate-logs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
