# 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.


---

# Agent Instructions: 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:

```
GET https://qatesting.gitbook.io/qa/automation-testing/flow/selenium/frameworks/log4j.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
