Skip to content

Commit efff246

Browse files
committed
Add Log4j modules
1 parent 09901ca commit efff246

File tree

13 files changed

+80
-3
lines changed

13 files changed

+80
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and contains two modules `pl.tfij.java9modules.app` and `pl.tfij.java9modules.gr
77
The first one has a dependency to the second one.
88
Each module contains only one class with a one single-line method.
99

10+
This project has been modified from the original to demonstrate Log4j working as modules in Java 9.
11+
1012
Included scripts allow to easily run the code.
1113
To compile the code just start a `compile.sh` script.
1214
Then execute the `package.sh` to build jar files.

compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
javac -d build --module-source-path src $(find src -name "*.java")
2+
javac -d build --module-path lib --module-source-path src $(find src -name "*.java")

lib/log4j-api-2.10.1-SNAPSHOT.jar

254 KB
Binary file not shown.

lib/log4j-core-2.10.1-SNAPSHOT.jar

1.52 MB
Binary file not shown.

lib/log4j-slf4j-impl-2.10.0.jar

23.6 KB
Binary file not shown.

lib/slf4j-api-1.8.0-alpha2.jar

42.9 KB
Binary file not shown.

package.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
22
mkdir -p mods
33
jar --create --file=mods/[email protected] --module-version=1.0 --main-class=pl.tfij.java9modules.app.ModuleApp -C build/pl.tfij.java9modules.app .
4+
jar --create --file=mods/[email protected] --module-version=1.0 --main-class=pl.tfij.java9modules.slf4japp.SLF4JApp -C build/pl.tfij.java9modules.slf4japp .
45
jar --create --file=mods/[email protected] --module-version=1.0 -C build/pl.tfij.java9modules.greetings .

resources/log4j2.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<Configuration status="ERROR">
20+
<Appenders>
21+
<Console name="Console" target="SYSTEM_OUT">
22+
<PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable" />
23+
</Console>
24+
</Appenders>
25+
<Loggers>
26+
<Logger name="org.foo" level="DEBUG" />
27+
<Root level="TRACE">
28+
<AppenderRef ref="Console" />
29+
</Root>
30+
</Loggers>
31+
</Configuration>

run.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
#!/usr/bin/env bash
2-
java --module-path mods --module pl.tfij.java9modules.app
2+
echo "Running Log4j2 application"
3+
java --module-path mods:lib -cp $PWD/resources --module pl.tfij.java9modules.app
4+
5+
echo "Running SLF4J application"
6+
java --module-path mods:lib -cp $PWD/resources --module pl.tfij.java9modules.slf4japp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module pl.tfij.java9modules.app {
22
requires pl.tfij.java9modules.greetings;
3+
requires org.apache.logging.log4j;
34
}

src/pl.tfij.java9modules.app/pl/tfij/java9modules/app/ModuleApp.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
import pl.tfij.java9modules.greetings.Greeting;
44

5+
import org.apache.logging.log4j.Logger;
6+
import org.apache.logging.log4j.LogManager;
7+
8+
59
public class ModuleApp {
610

11+
private static Logger logger = LogManager.getLogger(ModuleApp.class)
12+
;
713
public static void main(String[] args) {
8-
System.out.println(new Greeting().regular("World"));
14+
System.out.println("Class path: " + System.getProperty("java.class.path"));
15+
System.out.println("Module path:" + System.getProperty("jdk.module.path"));
16+
String greeting = new Greeting().regular("World");
17+
System.out.println(greeting);
18+
logger.info(greeting);
919
}
1020

1121

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module pl.tfij.java9modules.slf4japp {
2+
requires pl.tfij.java9modules.greetings;
3+
requires org.slf4j;
4+
requires org.apache.logging.log4j;
5+
requires org.apache.logging.log4j.slf4j.impl;
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pl.tfij.java9modules.slf4japp;
2+
3+
import pl.tfij.java9modules.greetings.Greeting;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
9+
public class SLF4JApp {
10+
11+
private static Logger logger = LoggerFactory.getLogger(SLF4JApp.class);
12+
13+
public static void main(String[] args) {
14+
System.out.println("Class path: " + System.getProperty("java.class.path"));
15+
System.out.println("Module path:" + System.getProperty("jdk.module.path"));
16+
String greeting = new Greeting().regular("World");
17+
System.out.println(greeting);
18+
logger.info(greeting);
19+
}
20+
21+
22+
}

0 commit comments

Comments
 (0)