Skip to content

Commit e302aaa

Browse files
Merge pull request eugenp#1278 from eugenp/pr-BAEL-41-Log4j2-layouts-appenders-filters
BAEL-41 - Log4J 2 Layout, Appenders, Filters
2 parents 52a3565 + 5708348 commit e302aaa

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

‎log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,78 +31,85 @@ public static void setup() throws Exception {
3131
}
3232

3333
@Test
34-
public void givenLoggerWithDefaultConfig_shouldLogToConsole() throws Exception {
34+
public void givenLoggerWithDefaultConfig_whenLogToConsole_thanOK() throws Exception {
3535
Logger logger = LogManager.getLogger(getClass());
3636
Exception e = new RuntimeException("This is only a test!");
37+
3738
logger.info("This is a simple message at INFO level. " + "It will be hidden.");
3839
logger.error("This is a simple message at ERROR level. " + "This is the minimum visible level.", e);
3940
}
4041

4142
@Test
42-
public void givenLoggerWithConsoleConfig_shouldLogToConsoleInColors() throws Exception {
43+
public void givenLoggerWithConsoleConfig_whenLogToConsoleInColors_thanOK() throws Exception {
4344
Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER");
45+
Exception e = new RuntimeException("This is only a test!");
46+
4447
logger.trace("This is a colored message at TRACE level.");
4548
logger.debug("This is a colored message at DEBUG level. " + "This is the minimum visible level.");
4649
logger.info("This is a colored message at INFO level.");
4750
logger.warn("This is a colored message at WARN level.");
48-
Exception e = new RuntimeException("This is only a test!");
4951
logger.error("This is a colored message at ERROR level.", e);
5052
logger.fatal("This is a colored message at FATAL level.");
5153
}
5254

5355
@Test
54-
public void givenLoggerWithConsoleConfig_shouldFilterByMarker() throws Exception {
56+
public void givenLoggerWithConsoleConfig_whenFilterByMarker_thanOK() throws Exception {
5557
Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER");
5658
Marker appError = MarkerManager.getMarker("APP_ERROR");
57-
logger.error(appError, "This marker message at ERROR level should be hidden.");
5859
Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE");
60+
61+
logger.error(appError, "This marker message at ERROR level should be hidden.");
5962
logger.trace(connectionTrace, "This is a marker message at TRACE level.");
6063
}
6164

6265
@Test
63-
public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception {
66+
public void givenLoggerWithConsoleConfig_whenFilterByThreadContext_thanOK() throws Exception {
6467
Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_THREAD_CONTEXT");
6568
ThreadContext.put("userId", "1000");
6669
logger.info("This is a log-visible user login. Maybe from an admin account?");
6770
ThreadContext.put("userId", "1001");
6871
logger.info("This is a log-invisible user login.");
69-
7072
}
7173

7274
@Test
73-
public void givenLoggerWithAsyncConfig_shouldLogToJsonFile() throws Exception {
75+
public void givenLoggerWithAsyncConfig_whenLogToJsonFile_thanOK() throws Exception {
7476
Logger logger = LogManager.getLogger("ASYNC_JSON_FILE_APPENDER");
77+
7578
final int count = 88;
7679
for (int i = 0; i < count; i++) {
7780
logger.info("This is async JSON message #{} at INFO level.", count);
7881
}
82+
7983
long logEventsCount = Files.lines(Paths.get("target/logfile.json"))
8084
.count();
8185
assertTrue(logEventsCount > 0 && logEventsCount <= count);
8286
}
8387

8488
@Test
85-
public void givenLoggerWithFailoverConfig_shouldLog() throws Exception {
89+
public void givenLoggerWithFailoverConfig_whenLog_thanOK() throws Exception {
8690
Logger logger = LogManager.getLogger("FAIL_OVER_SYSLOG_APPENDER");
91+
Exception e = new RuntimeException("This is only a test!");
92+
8793
logger.trace("This is a syslog message at TRACE level.");
8894
logger.debug("This is a syslog message at DEBUG level.");
8995
logger.info("This is a syslog message at INFO level. This is the minimum visible level.");
9096
logger.warn("This is a syslog message at WARN level.");
91-
Exception e = new RuntimeException("This is only a test!");
9297
logger.error("This is a syslog message at ERROR level.", e);
9398
logger.fatal("This is a syslog message at FATAL level.");
9499
}
95100

96101
@Test
97-
public void givenLoggerWithJdbcConfig_shouldLogToDataSource() throws Exception {
102+
public void givenLoggerWithJdbcConfig_whenLogToDataSource_thanOK() throws Exception {
98103
Logger logger = LogManager.getLogger("JDBC_APPENDER");
104+
99105
final int count = 88;
100106
for (int i = 0; i < count; i++) {
101107
logger.info("This is JDBC message #{} at INFO level.", count);
102108
}
103109
Connection connection = ConnectionFactory.getConnection();
104110
ResultSet resultSet = connection.createStatement()
105-
.executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs");
111+
.executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs");
112+
106113
int logCount = 0;
107114
if (resultSet.next()) {
108115
logCount = resultSet.getInt("ROW_COUNT");
@@ -111,8 +118,9 @@ public void givenLoggerWithJdbcConfig_shouldLogToDataSource() throws Exception {
111118
}
112119

113120
@Test
114-
public void givenLoggerWithRollingFileConfig_shouldLogToXMLFile() throws Exception {
121+
public void givenLoggerWithRollingFileConfig_whenLogToXMLFile_thanOK() throws Exception {
115122
Logger logger = LogManager.getLogger("XML_ROLLING_FILE_APPENDER");
123+
116124
final int count = 88;
117125
for (int i = 0; i < count; i++) {
118126
logger.info("This is rolling file XML message #{} at INFO level.", i);

‎log4j2/src/test/resources/log4j2.xml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
<Async name="AsyncAppender" bufferSize="80">
2020
<AppenderRef ref="JSONLogfileAppender" />
2121
</Async>
22-
<Syslog name="Syslog" format="RFC5424" host="localhost" port="514" protocol="TCP" facility="local3" connectTimeoutMillis="10000" reconnectionDelayMillis="5000" />
22+
<!--
23+
<Syslog name="Syslog" format="RFC5424" host="localhost" port="514" protocol="TCP" facility="local3" connectTimeoutMillis="10000" reconnectionDelayMillis="5000" mdcId="mdc" includeMDC="true" />
2324
<Failover name="FailoverAppender" primary="Syslog">
2425
<Failovers>
2526
<AppenderRef ref="ConsoleAppender" />
2627
</Failovers>
2728
</Failover>
29+
-->
2830
<JDBC name="JDBCAppender" tableName="logs">
2931
<ConnectionFactory class="com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory" method="getConnection" />
3032
<Column name="when" isEventTimestamp="true" />
@@ -53,9 +55,11 @@
5355
<Logger name="ASYNC_JSON_FILE_APPENDER" level="INFO" additivity="false">
5456
<AppenderRef ref="AsyncAppender" />
5557
</Logger>
58+
<!--
5659
<Logger name="FAIL_OVER_SYSLOG_APPENDER" level="INFO" additivity="false">
5760
<AppenderRef ref="FailoverAppender" />
5861
</Logger>
62+
-->
5963
<Logger name="JDBC_APPENDER" level="INFO" additivity="false">
6064
<AppenderRef ref="JDBCAppender" />
6165
</Logger>

0 commit comments

Comments
 (0)