Skip to content

Commit 8955dce

Browse files
authored
[JAVA-41272] Moving some article links on Github - log4j (#18373)
1 parent b8badb9 commit 8955dce

File tree

16 files changed

+168
-74
lines changed

16 files changed

+168
-74
lines changed

logging-modules/log4j/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [Introduction to Java Logging](http://www.baeldung.com/java-logging-intro)
33
- [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback)
44
- [A Guide to Rolling File Appenders](http://www.baeldung.com/java-logging-rolling-file-appenders)
5-
- [Logging Exceptions Using SLF4J](https://www.baeldung.com/slf4j-log-exceptions)
6-
- [Log4j Warning: “No Appenders Could Be Found for Logger”](https://www.baeldung.com/log4j-no-appenders-found)
75
- [A Guide to Log4j and the log4j.properties File in Java](https://www.baeldung.com/java-log4j-properties-guide)
6+
- [Log4j 2 Configuration Using a Properties File](https://www.baeldung.com/java-log4j2-config-with-prop-file)
7+
- [Get Log Output in JSON](http://www.baeldung.com/java-log-json-output)
8+
- [Intro to Log4j2 – Appenders, Layouts and Filters](http://www.baeldung.com/log4j2-appenders-layouts-filters)

logging-modules/log4j/pom.xml

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
<artifactId>apache-log4j-extras</artifactId>
2626
<version>${log4j.version}</version>
2727
</dependency>
28-
2928
<!--log4j2 dependencies -->
3029
<dependency>
3130
<groupId>org.apache.logging.log4j</groupId>
@@ -42,20 +41,59 @@
4241
<artifactId>log4j-slf4j2-impl</artifactId>
4342
<version>${log4j-slf4j.version}</version>
4443
</dependency>
45-
4644
<!--disruptor for log4j2 async logging -->
4745
<dependency>
4846
<groupId>com.lmax</groupId>
4947
<artifactId>disruptor</artifactId>
5048
<version>${disruptor.version}</version>
5149
</dependency>
50+
<!-- This is used by JSONLayout. -->
51+
<dependency>
52+
<groupId>com.fasterxml.jackson.core</groupId>
53+
<artifactId>jackson-databind</artifactId>
54+
<version>${jackson.version}</version>
55+
</dependency>
56+
<!-- This is used by JDBC Appender. -->
57+
<dependency>
58+
<groupId>com.h2database</groupId>
59+
<artifactId>h2</artifactId>
60+
<version>${h2.version}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.apache.commons</groupId>
64+
<artifactId>commons-dbcp2</artifactId>
65+
<version>${commons-dbcp2.version}</version>
66+
</dependency>
5267
</dependencies>
5368

69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-compiler-plugin</artifactId>
74+
<configuration>
75+
<proc>none</proc>
76+
</configuration>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-surefire-plugin</artifactId>
81+
<version>${maven-surefire-plugin.version}</version>
82+
<configuration>
83+
<systemPropertyVariables>
84+
<test.mime>json</test.mime>
85+
<logging.folder.path>${java.io.tmpdir}/${maven.build.timestamp}/logfile.json</logging.folder.path>
86+
</systemPropertyVariables>
87+
</configuration>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
5492
<properties>
5593
<log4j-core.version>2.23.1</log4j-core.version>
5694
<log4j-api.version>2.23.1</log4j-api.version>
5795
<log4j-slf4j.version>2.23.1</log4j-slf4j.version>
58-
96+
<commons-dbcp2.version>2.9.0</commons-dbcp2.version>
5997
<disruptor.version>3.3.6</disruptor.version>
6098
</properties>
6199

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.logging.log4j2.consoleandfile;
1+
package com.baeldung.log4j2;
22

33
import org.apache.logging.log4j.LogManager;
44
import org.apache.logging.log4j.Logger;

logging-modules/log4j/src/main/resources/log4j.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@
9090
<appender-ref ref="roll-by-time-and-size"/>
9191
</category>
9292

93-
<logger name="com.baeldung.log4j.NoAppenderExample"/>
94-
9593
<root>
9694
<level value="DEBUG"/>
9795
<appender-ref ref="stdout"/>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
appender.console.type = Console
2+
appender.console.name = STDOUT
3+
appender.console.layout.type = PatternLayout
4+
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
5+
6+
appender.file.type = File
7+
appender.file.name = LOGFILE
8+
appender.file.fileName=logs/log4j.log
9+
appender.file.layout.type=PatternLayout
10+
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
11+
appender.file.filter.threshold.type = ThresholdFilter
12+
appender.file.filter.threshold.level = info
13+
14+
rootLogger=debug, STDOUT, LOGFILE
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.logging.log4j2.tests;
1+
package com.baeldung.logging.log4j2;
22

33
import static org.junit.Assert.assertTrue;
44

@@ -17,7 +17,7 @@
1717
import org.junit.runner.RunWith;
1818
import org.junit.runners.JUnit4;
1919

20-
import com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory;
20+
import com.baeldung.logging.log4j2.jdbc.ConnectionFactory;
2121

2222
@RunWith(JUnit4.class)
2323
public class CustomLoggingIntegrationTest {
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package com.baeldung.logging.log4j2.tests;
1+
package com.baeldung.logging.log4j2;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.CharArrayWriter;
6+
import java.io.Writer;
27

3-
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
4-
import com.fasterxml.jackson.databind.JsonNode;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
68
import org.apache.logging.log4j.LogManager;
79
import org.apache.logging.log4j.Logger;
810
import org.apache.logging.log4j.core.Appender;
@@ -12,10 +14,8 @@
1214
import org.junit.Before;
1315
import org.junit.Test;
1416

15-
import java.io.CharArrayWriter;
16-
import java.io.Writer;
17-
18-
import static org.junit.Assert.assertTrue;
17+
import com.fasterxml.jackson.databind.JsonNode;
18+
import com.fasterxml.jackson.databind.ObjectMapper;
1919

2020
public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
2121

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.logging.log4j2;
2+
3+
import java.lang.reflect.Field;
4+
5+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
6+
import org.junit.AfterClass;
7+
8+
public class Log4j2BaseIntegrationTest {
9+
@AfterClass
10+
public static void tearDown() throws Exception {
11+
Field factories = ConfigurationFactory.class.getDeclaredField("factories");
12+
factories.setAccessible(true);
13+
factories.set(null, null);
14+
ConfigurationFactory.resetConfigurationFactory();
15+
16+
}
17+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package com.baeldung.logging.log4j2.tests.jdbc;
2-
3-
import org.apache.commons.dbcp2.BasicDataSource;
4-
import org.h2.Driver;
1+
package com.baeldung.logging.log4j2.jdbc;
52

63
import java.sql.Connection;
74
import java.sql.SQLException;
85

6+
import org.apache.commons.dbcp2.BasicDataSource;
7+
import org.h2.Driver;
8+
99
public class ConnectionFactory {
1010
private interface Singleton {
1111
ConnectionFactory INSTANCE = new ConnectionFactory();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
3+
packages="com.baeldung" status="WARN">
4+
<Appenders>
5+
<Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
6+
<JsonTemplateLayout eventTemplateUri="classpath:JsonLayout.json">
7+
<EventTemplateAdditionalField key="myCustomField" value="myCustomValue"/>
8+
</JsonTemplateLayout>
9+
</Console>
10+
<!-- <Syslog name="Syslog" format="RFC5424" host="localhost" port="514"
11+
protocol="TCP" facility="local3" connectTimeoutMillis="10000" reconnectionDelayMillis="5000"
12+
mdcId="mdc" includeMDC="true" /> <Failover name="FailoverAppender" primary="Syslog">
13+
<Failovers> <AppenderRef ref="ConsoleAppender" /> </Failovers> </Failover> -->
14+
<JDBC name="JDBCAppender" tableName="logs">
15+
<ConnectionFactory
16+
class="com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory"
17+
method="getConnection"/>
18+
<Column name="when" isEventTimestamp="true"/>
19+
<Column name="logger" pattern="%logger"/>
20+
<Column name="level" pattern="%level"/>
21+
<Column name="message" pattern="%message"/>
22+
<Column name="throwable" pattern="%ex{full}"/>
23+
</JDBC>
24+
</Appenders>
25+
<RollingFile name="XMLRollingfileAppender" fileName="target/logfile.xml"
26+
filePattern="target/logfile-%d{yyyy-MM-dd}-%i.log.gz">
27+
<XMLLayout/>
28+
<Policies>
29+
<SizeBasedTriggeringPolicy
30+
size="17 kB"/>
31+
</Policies>
32+
</RollingFile>
33+
<Loggers>
34+
<!-- <Logger name="FAIL_OVER_SYSLOG_APPENDER" level="INFO" additivity="false">
35+
<AppenderRef ref="FailoverAppender" /> </Logger> -->
36+
<Logger name="CONSOLE_JSON_APPENDER" level="TRACE"
37+
additivity="false">
38+
<AppenderRef ref="ConsoleJSONAppender"/>
39+
</Logger>
40+
<Logger name="XML_ROLLING_FILE_APPENDER" level="INFO"
41+
additivity="false">
42+
<AppenderRef ref="XMLRollingfileAppender"/>
43+
</Logger>
44+
</Loggers>
45+
</Configuration>

0 commit comments

Comments
 (0)