> For the complete documentation index, see [llms.txt](https://qatesting.gitbook.io/qa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qatesting.gitbook.io/qa/automation-testing/flow/selenium/frameworks/log4j.md).

# Log4j

## 📚 Log4j in Selenium

Log4j is a popular logging framework used in Selenium.

### 🔎 Logging in Selenium

* Logs provide visibility into test execution.
* Help debug failures and issues.
* Log4j is integrated with Selenium binding.

### 🪵 Log4j Architecture

* 📜 Loggers - Capture log statements from code
* 🗒️ Appenders - Publish logs to destinations like file, console etc.
* 🔧 Layouts - Formats logging output
* 📊 Levels - Specify logging granularity like INFO, DEBUG etc.

### 👍 Benefits

* 💡 Debugging capabilities
* 📊 Granular control of logging
* 🔁 Asynchronous logging
* 📁 Log to multiple destinations

### 📃 Log4j Example for CNBC

```java
//Set up logger
private static final Logger log = LogManager.getLogger(CNBCLoginTest.class);

public void testValidLogin() {

  log.info("Navigating to CNBC login page"); //Simple log statement

  //Log variable data
  log.debug("Input username: " + username);  

  //Log exceptions
  try {
    login(username,password);
  } catch(Exception e) {
    log.error("Login failed!", e); 
  }

  //Check if logged in
  log.info("Login successful.");

}
```

This shows how Log4j can be used for logging in Selenium tests for CNBC.
