Skip to content

Commit fd34219

Browse files
ppkarwaszvy
andauthored
Revamp the Web Applications page (#2692)
Co-authored-by: Volkan Yazıcı <[email protected]>
1 parent 466ac59 commit fd34219

27 files changed

+1301
-415
lines changed

log4j-jakarta-web/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<description>The Apache Log4j support for Jakarta EE 9+ web servlet containers</description>
3333

3434
<properties>
35+
<maven.javadoc.skip>false</maven.javadoc.skip>
3536

3637
<!--
3738
~ OSGi and JPMS options
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"Configuration": {
3+
"Appenders": {
4+
// tag::servlet[]
5+
"Servlet": {
6+
"name": "SERVLET",
7+
"PatternLayout": {
8+
"pattern": "%m%n",
9+
"alwaysWriteExceptions": false // <1>
10+
}
11+
}
12+
// end::servlet[]
13+
},
14+
"Loggers": {
15+
"Root": {
16+
"level": "INFO",
17+
"AppenderRef": {
18+
"ref": "SERVLET"
19+
}
20+
}
21+
}
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to you under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
##
18+
# tag::servlet[]
19+
appender.0.type = Servlet
20+
appender.0.name = SERVLET
21+
appender.0.layout.type = PatternLayout
22+
appender.0.layout.pattern = %m%n
23+
# <1>
24+
appender.0.layout.alwaysWriteExceptions = false
25+
# end::servlet[]
26+
27+
rootLogger.level = INFO
28+
rootLogger.appenderRef.0.ref = SERVLET
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
<Configuration xmlns="https://logging.apache.org/xml/ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="
20+
https://logging.apache.org/xml/ns
21+
https://logging.apache.org/xml/ns/log4j-config-2.xsd">
22+
<Appenders>
23+
<!-- tag::servlet[] -->
24+
<Servlet name="SERVLET">
25+
<PatternLayout pattern="%m%n" alwaysWriteExceptions="false"/> <!--1-->
26+
</Servlet>
27+
<!-- end::servlet[] -->
28+
</Appenders>
29+
<Loggers>
30+
<Root level="INFO">
31+
<AppenderRef ref="SERVLET"/>
32+
</Root>
33+
</Loggers>
34+
</Configuration>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to you under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
Configuration:
18+
Appenders:
19+
# tag::servlet[]
20+
Servlet:
21+
name: "SERVLET"
22+
PatternLayout:
23+
pattern: "%m%n"
24+
alwaysWriteExceptions: false # <1>
25+
# end::servlet[]
26+
Loggers:
27+
Root:
28+
level: "INFO"
29+
AppenderRef:
30+
ref: "SERVLET"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package example;
18+
19+
import javax.servlet.AsyncContext;
20+
import javax.servlet.annotation.WebServlet;
21+
import javax.servlet.http.HttpServlet;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
import org.apache.logging.log4j.LogManager;
25+
import org.apache.logging.log4j.Logger;
26+
import org.apache.logging.log4j.web.Log4jWebSupport;
27+
import org.apache.logging.log4j.web.WebLoggerContextUtils;
28+
29+
@WebServlet(urlPatterns = "/async/*", asyncSupported = true)
30+
public class AsyncServlet extends HttpServlet {
31+
32+
private final Logger logger = LogManager.getLogger();
33+
34+
@Override
35+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
36+
// tag::manual[]
37+
AsyncContext asyncContext = req.startAsync();
38+
Log4jWebSupport webSupport = WebLoggerContextUtils.getWebLifeCycle(getServletContext());
39+
asyncContext.start(() -> {
40+
try {
41+
webSupport.setLoggerContext();
42+
// Put your logic here
43+
} finally {
44+
webSupport.clearLoggerContext();
45+
}
46+
});
47+
// end::manual[]
48+
}
49+
50+
@Override
51+
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
52+
// tag::automatic[]
53+
AsyncContext asyncContext = req.startAsync();
54+
asyncContext.start(WebLoggerContextUtils.wrapExecutionContext(getServletContext(), () -> {
55+
// Put your logic here
56+
}));
57+
// end::automatic[]
58+
}
59+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
21+
version="5.0">
22+
<!-- tag::jndi[] -->
23+
<context-param>
24+
<param-name>isLog4jContextSelectorNamed</param-name>
25+
<param-value>true</param-value>
26+
</context-param>
27+
<context-param>
28+
<param-name>log4jContextName</param-name>
29+
<param-value>your_application_name</param-value>
30+
</context-param>
31+
<env-entry>
32+
<env-entry-name>log4j/context-name</env-entry-name>
33+
<env-entry-value>your_application_name</env-entry-value>
34+
<env-entry-type>java.lang.String</env-entry-type>
35+
</env-entry>
36+
<!-- end::jndi[] -->
37+
</web-app>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"Configuration": {
3+
"Appenders": {
4+
// tag::global[]
5+
"File": {
6+
"name": "GLOBAL",
7+
"fileName": "logs/global.log",
8+
"JsonTemplateLayout": {
9+
"EventTemplateAdditionalField": {
10+
"key": "contextName",
11+
"value": "$${web:contextName}"
12+
}
13+
}
14+
},
15+
// end::global[]
16+
// tag::routing[]
17+
"Routing": {
18+
"name": "ROUTING",
19+
"Routes": {
20+
"pattern": "$${web:contextName:-common}",
21+
"Route": {
22+
"File": {
23+
"name": "${web:contextName:-common}",
24+
"fileName": "logs/${web:contextName:-common}.log",
25+
"PatternLayout": {
26+
"pattern": "d [%t] %-5p %c - %m%n"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
// end::routing[]
33+
},
34+
"Loggers": {
35+
"Route": {
36+
"level": "INFO",
37+
"AppenderRef": [
38+
{
39+
"ref": "GLOBAL"
40+
},
41+
{
42+
"ref": "ROUTING"
43+
}
44+
]
45+
}
46+
}
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to you under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
##
18+
# tag::global[]
19+
appender.0.type = File
20+
appender.0.name = GLOBAL
21+
appender.0.fileName = logs/global
22+
appender.0.layout.type = JsonTemplateLayout
23+
appender.0.layout.0.type = EventTemplateAdditionalField
24+
appender.0.layout.0.key = contextName
25+
appender.0.layout.0.value = $${web:contextName}
26+
# end::global[]
27+
# tag::routing[]
28+
appender.1.type = Routing
29+
appender.1.name = ROUTING
30+
appender.1.route.type = Routes
31+
appender.1.route.pattern = $${web:contextName:-common}
32+
appender.1.route.0.type = Route
33+
appender.1.route.0.appender.type = File
34+
appender.1.route.0.appender.name = ${web:contextName:-common}
35+
appender.1.route.0.appender.fileName = logs/${web:contextName:-common}.log
36+
appender.1.route.0.appender.layout.type = PatternLayout
37+
appender.1.route.0.appender.layout.pattern = %d [%t] %-5p %c - %m%n
38+
# end::routing[]
39+
rootLogger.level = INFO
40+
rootLogger.appenderRef.0.ref = GLOBAL
41+
rootLogger.appenderRef.1.ref = ROUTING
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-config-2.xsd">
21+
<Appenders>
22+
<!-- tag::global[] -->
23+
<File name="GLOBAL" fileName="logs/global.log">
24+
<JsonTemplateLayout>
25+
<EventTemplateAdditionalField key="contextName"
26+
value="$${web:contextName}"/>
27+
</JsonTemplateLayout>
28+
</File>
29+
<!-- end::global[] -->
30+
<!-- tag::routing[] -->
31+
<Routing name="ROUTING">
32+
<Routes pattern="$${web:contextName:-common}">
33+
<Route>
34+
<File name="${web:contextName:-common}"
35+
fileName="logs/${web:contextName:-common}.log">
36+
<PatternLayout pattern="%d [%t] %-5p %c - %m%n"/>
37+
</File>
38+
</Route>
39+
</Routes>
40+
</Routing>
41+
<!-- end::routing[] -->
42+
</Appenders>
43+
<Loggers>
44+
<Root level="INFO">
45+
<AppenderRef ref="GLOBAL"/>
46+
<AppenderRef ref="ROUTING"/>
47+
</Root>
48+
</Loggers>
49+
</Configuration>

0 commit comments

Comments
 (0)