Skip to content

Commit b97323e

Browse files
authored
BAEL-5124 - Adding a module with examples of printing thread info in the log file using log4j (#11704)
* adding a example module for printing thread info in log file using log4j * - fix: removing submodule * - fix: Adding submodule * - fix: Removing `.gitignore`, `README.md`, and `application.properties`. - fix: Removing Spring boot and adding log4j2 from Maven Central * - fix: Changing module name as suggested to log4j2-thread-info. - fix: Fixing log path in log4j2 configuration * - feat: Removing log4j2-thread-info module - feat: Adding example code into existing log4j2 module * - fix: Removing unnecessary logging * - fix: Separating thread info in the logging pattern * - fix: package name to lowercase * lowercasing package name and variable name * changing package name in configuration * - fix: formatting * - fix: formatting imports
1 parent dbdb4cc commit b97323e

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.logging.log4j2threadinfo;
2+
3+
import java.util.stream.IntStream;
4+
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
8+
public class Log4j2ThreadInfo {
9+
private static final Logger logger = LogManager.getLogger(Log4j2ThreadInfo.class);
10+
11+
public static void main(String[] args) {
12+
IntStream.range(0, 5).forEach(i -> {
13+
Runnable runnable = () -> logger.info("Logging info");
14+
Thread thread = new Thread(runnable);
15+
thread.start();
16+
});
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Properties>
4+
<Property name="LOG_PATTERN">
5+
%d{yyyy-MM-dd HH:mm:ss.SSS} --- thread_id="%tid" thread_name="%tn" thread_priority="%tp" --- [%p] %m%n
6+
</Property>
7+
</Properties>
8+
<Appenders>
9+
<RollingFile name="RollingName" filename="logs/app.log"
10+
filePattern="$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log">
11+
<PatternLayout>
12+
<Pattern>${LOG_PATTERN}</Pattern>
13+
</PatternLayout>
14+
<Policies>
15+
<SizeBasedTriggeringPolicy size="20MB"/>
16+
</Policies>
17+
</RollingFile>
18+
<Console name="Console" target="SYSTEM_OUT">
19+
<PatternLayout pattern="${LOG_PATTERN}"/>
20+
</Console>
21+
</Appenders>
22+
<Loggers>
23+
<Logger name="com.baeldung.logging.log4j2threadinfo" level="trace">
24+
<AppenderRef ref="Console"/>
25+
</Logger>
26+
<Root level="error">
27+
<AppenderRef ref="RollingName"/>
28+
</Root>
29+
</Loggers>
30+
</Configuration>

0 commit comments

Comments
 (0)