Configuring Log4j involves setting up a configuration file to define how logging should behave in your application. The configuration file is typically named `log4j2.xml` or `log4j2.properties`. Below are examples for both formats:
### log4j2.xml Example:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="logs/app.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>```
### log4j2.properties Example:
```properties
status = error
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
appender.file.type = File
appender.file.name = file
appender.file.fileName = logs/app.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
rootLogger.level = debug
rootLogger.appenderRefs = console, file
rootLogger.appenderRef.console.ref = console
rootLogger.appenderRef.file.ref = file
```
In these examples:
- **`Appenders`:** Define where log entries should go (e.g., console, file).
- **`PatternLayout`:** Specifies the format of log entries.
- **`Loggers`:** Define log levels for specific packages or classes.
- **`Root`:** Configures the root logger with a specific log level and references appenders.
Adjust the configurations based on your specific needs. Ensure that the configuration file is placed in the classpath of your application, and Log4j will use it to control logging behavior.