Skip to content

Commit e071587

Browse files
Terryxxxlittleaj
authored andcommitted
Adding trace log4j1.2 test case (#648)
* add trace log4j1.2 test case * update trace log4j1.2 test case * update servlet name
1 parent 7e37691 commit e071587

File tree

11 files changed

+256
-1
lines changed

11 files changed

+256
-1
lines changed

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ include ':test:smoke:testApps:PerfTestApp'
5252
//include ':test:smoke:testApps:SimpleCalculator'
5353
include ':test:smoke:testApps:CoreAndFilter'
5454
include ':test:smoke:testApps:AutoPerfCounters'
55+
include ':test:smoke:testApps:TraceLog4j1_2'
5556

5657
if (System.env.'COLLECTD_HOME') {
5758
include 'collectd'
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
jbosseap6
12
jbosseap7
23
tomcat7
34
tomcat8
45
tomcat85
5-
jbosseap6jetty9
6+
jetty9
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'war'
2+
3+
dependencies {
4+
compile aiCoreJar
5+
compile aiWebJar
6+
compile aiLog4j1_2Jar
7+
compile 'com.google.guava:guava:20.0'
8+
9+
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
10+
11+
providedRuntime 'mysql:mysql-connector-java:5.1.44'
12+
13+
smokeTestCompile 'com.google.guava:guava:23.0'
14+
15+
testCompile 'com.google.guava:guava:23.0' // VSCODE intellisense bug workaround
16+
17+
compile group: 'log4j', name: 'log4j', version: '1.2.17'
18+
}
19+
20+
compileJava.sourceCompatibility = 1.7
21+
compileJava.targetCompatibility = 1.7
22+
compileSmokeTestJava.sourceCompatibility = 1.8
23+
compileSmokeTestJava.targetCompatibility = 1.8
24+
25+
ext.testAppArtifactDir = war.destinationDir
26+
ext.testAppArtifactFilename = war.archiveName
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.microsoft.ajl.simplecalc;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
public class ServletFuncs {
11+
12+
protected static void geRrenderHtml(HttpServletRequest request, HttpServletResponse response)
13+
throws ServletException, IOException {
14+
response.setContentType("text/html;charset=UTF-8");
15+
renderHtml(request, response.getWriter());
16+
}
17+
18+
private static void renderHtml(HttpServletRequest req, PrintWriter writer) {
19+
writer.println("<html>");
20+
writer.println("<head><title>Calculation Result</title></head>");
21+
writer.println("<body>");
22+
writer.printf("<h1>%s</h1>", req.getRequestURI());
23+
writer.println("<h2>OK!</h2>");
24+
writer.println("</body></html>");
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.microsoft.ajl.simplecalc;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
import org.apache.log4j.LogManager;
12+
import org.apache.log4j.Logger;
13+
14+
/**
15+
* Servlet implementation class SimpleTestTraceLog4j1_2
16+
*/
17+
@WebServlet(description = "calls log4j1.2", urlPatterns = "/traceLog4j1_2")
18+
public class SimpleTestTraceLog4j1_2Servlet extends HttpServlet {
19+
20+
private static final long serialVersionUID = 4271118116251185099L;
21+
22+
/**
23+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
24+
*/
25+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
26+
throws ServletException, IOException {
27+
ServletFuncs.geRrenderHtml(request, response);
28+
29+
Logger logger = LogManager.getRootLogger();
30+
logger.trace("This is log4j1.2 trace.");
31+
logger.debug("This is log4j1.2 debug.");
32+
logger.info("This is log4j1.2 info.");
33+
logger.warn("This is log4j1.2 warn.");
34+
logger.error("This is log4j1.2 error.");
35+
logger.fatal("This is log4j1.2 fatal.");
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" schemaVersion="2014-05-30">
3+
4+
5+
<!-- The key from the portal: -->
6+
7+
<InstrumentationKey>00000000-0000-0000-0000-0FEEDDADBEEF</InstrumentationKey>
8+
9+
<!-- HTTP request component (not required for bare API) -->
10+
11+
<TelemetryModules>
12+
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebRequestTrackingTelemetryModule" />
13+
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebSessionTrackingTelemetryModule" />
14+
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebUserTrackingTelemetryModule" />
15+
</TelemetryModules>
16+
17+
<!-- Events correlation (not required for bare API) -->
18+
<!-- These initializers add context data to each event -->
19+
20+
<TelemetryInitializers>
21+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationIdTelemetryInitializer" />
22+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationNameTelemetryInitializer" />
23+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebSessionTelemetryInitializer" />
24+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserTelemetryInitializer" />
25+
<Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserAgentTelemetryInitializer" />
26+
</TelemetryInitializers>
27+
28+
<Channel>
29+
<!-- <EndpointAddress>http://localhost:60606/v2/track</EndpointAddress> -->
30+
<EndpointAddress>http://fakeingestion:60606/v2/track</EndpointAddress>
31+
</Channel>
32+
<PerformanceCounters>
33+
<UseBuiltIn>False</UseBuiltIn>
34+
</PerformanceCounters>
35+
</ApplicationInsights>
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+
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
4+
5+
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
6+
<appender name="test" class="com.microsoft.applicationinsights.log4j.v1_2.ApplicationInsightsAppender">
7+
<param name="instrumentationKey" value="00000000-0000-0000-0000-0FEEDDADBEEF" />
8+
<layout class="org.apache.log4j.PatternLayout">
9+
<param name="ConversionPattern" value="%-5p %c - %m%n"/>
10+
</layout>
11+
</appender>
12+
<appender name="console" class="org.apache.log4j.ConsoleAppender">
13+
<layout class="org.apache.log4j.PatternLayout">
14+
<param name="ConversionPattern" value="%-5p %c - %m%n"/>
15+
</layout>
16+
</appender>
17+
18+
<logger name="com.microsoft.applicationinsights.core.dependencies.http">
19+
<level value="off" />
20+
</logger>
21+
22+
<logger name="com.microsoft.applicationinsights.web.dependencies.http">
23+
<level value="off" />
24+
</logger>
25+
26+
<root>
27+
<priority value ="trace" />
28+
<appender-ref ref="test" />
29+
<!-- <appender-ref ref="console" /> -->
30+
</root>
31+
</log4j:configuration>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="SimpleCalculator_1" version="3.0">
3+
<display-name>SimpleTelemetry</display-name>
4+
<filter>
5+
<filter-name>ApplicationInsightsWebFilter</filter-name>
6+
<filter-class>com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter</filter-class>
7+
</filter>
8+
<filter-mapping>
9+
<filter-name>ApplicationInsightsWebFilter</filter-name>
10+
<url-pattern>/*</url-pattern>
11+
</filter-mapping>
12+
<welcome-file-list>
13+
<welcome-file>index.jsp</welcome-file>
14+
<!--
15+
<welcome-file>index.html</welcome-file>
16+
<welcome-file>index.htm</welcome-file>
17+
<welcome-file>default.html</welcome-file>
18+
<welcome-file>default.htm</welcome-file>
19+
<welcome-file>default.jsp</welcome-file>
20+
-->
21+
</welcome-file-list>
22+
</web-app>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
2+
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<title>Simple Trace Log4j1.2</title>
7+
</head>
8+
<body>
9+
<h1>Simple Trace Log4j1.2</h1>
10+
11+
</body>
12+
</html>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.microsoft.applicationinsights.smoketest;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import com.microsoft.applicationinsights.internal.schemav2.MessageData;
6+
import com.microsoft.applicationinsights.internal.schemav2.SeverityLevel;
7+
8+
import org.junit.Assume;
9+
import org.junit.Test;
10+
11+
public class TraceLog4j1_2Test extends AiSmokeTest {
12+
@Test
13+
@TargetUri("/traceLog4j1_2")
14+
public void testTraceLog4j1_2() {
15+
// this doesn't work with jbosseap6; under investigation
16+
Assume.assumeFalse(currentImageName.contains("jbosseap6"));
17+
18+
assertEquals(1, mockedIngestion.getCountForType("RequestData"));
19+
assertEquals(6, mockedIngestion.getCountForType("MessageData"));
20+
21+
MessageData md1 = getTelemetryDataForType(0, "MessageData");
22+
assertEquals("This is log4j1.2 trace.", md1.getMessage());
23+
assertEquals(SeverityLevel.Verbose, md1.getSeverityLevel());
24+
assertEquals("Log4j", md1.getProperties().get("SourceType"));
25+
assertEquals("TRACE", md1.getProperties().get("LoggingLevel"));
26+
27+
MessageData md2 = getTelemetryDataForType(1, "MessageData");
28+
assertEquals("This is log4j1.2 debug.", md2.getMessage());
29+
assertEquals(SeverityLevel.Verbose, md2.getSeverityLevel());
30+
assertEquals("Log4j", md2.getProperties().get("SourceType"));
31+
assertEquals("DEBUG", md2.getProperties().get("LoggingLevel"));
32+
33+
MessageData md3 = getTelemetryDataForType(2, "MessageData");
34+
assertEquals("This is log4j1.2 info.", md3.getMessage());
35+
assertEquals(SeverityLevel.Information, md3.getSeverityLevel());
36+
assertEquals("Log4j", md3.getProperties().get("SourceType"));
37+
assertEquals("INFO", md3.getProperties().get("LoggingLevel"));
38+
39+
MessageData md4 = getTelemetryDataForType(3, "MessageData");
40+
assertEquals("This is log4j1.2 warn.", md4.getMessage());
41+
assertEquals(SeverityLevel.Warning, md4.getSeverityLevel());
42+
assertEquals("Log4j", md4.getProperties().get("SourceType"));
43+
assertEquals("WARN", md4.getProperties().get("LoggingLevel"));
44+
45+
46+
MessageData md5 = getTelemetryDataForType(4, "MessageData");
47+
assertEquals("This is log4j1.2 error.", md5.getMessage());
48+
assertEquals(SeverityLevel.Error, md5.getSeverityLevel());
49+
assertEquals("Log4j", md5.getProperties().get("SourceType"));
50+
assertEquals("ERROR", md5.getProperties().get("LoggingLevel"));
51+
52+
MessageData md6 = getTelemetryDataForType(5, "MessageData");
53+
assertEquals("This is log4j1.2 fatal.", md6.getMessage());
54+
assertEquals(SeverityLevel.Critical, md6.getSeverityLevel());
55+
assertEquals("Log4j", md6.getProperties().get("SourceType"));
56+
assertEquals("FATAL", md6.getProperties().get("LoggingLevel"));
57+
}
58+
}

0 commit comments

Comments
 (0)