Skip to content

Commit

Permalink
Initial merge of Rob's logging changes
Browse files Browse the repository at this point in the history
tomakehurst committed Feb 27, 2013
2 parents fa22e67 + 1647644 commit 4a4f022
Showing 13 changed files with 194 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -42,14 +42,15 @@ bin
.classpath
.settings
.gradle
**/target

# IDEA #
#################
out
wiremock.iml
wiremock.ipr
wiremock.iws

**/*.iml


# Misc #
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -100,7 +100,6 @@ task jarAll(type: Jar, dependsOn: jar) {
rule pattern: "com.google.common.**", result: "wiremock.com.google.common.@1"
rule pattern: "org.codehaus.jackson.**", result: "wiremock.org.codehaus.jackson.@1"
rule pattern: "org.apache.http.**", result: "wiremock.org.apache.http.@1"
rule pattern: "org.apache.log4j.**", result: "wiremock.org.apache.log4j.@1"
rule pattern: "org.apache.commons.**", result: "wiremock.org.apache.commons.@1"
rule pattern: "joptsimple.**", result: "wiremock.joptsimple.@1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.tomakehurst.wiremock;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.github.tomakehurst.wiremock.common.Log4jNotifier;

import static org.apache.log4j.Level.ERROR;
import static org.apache.log4j.Level.INFO;
import static org.apache.log4j.Level.TRACE;

public class Log4jConfiguration {
public static void configureLogging(boolean verbose) {
ConsoleAppender appender = new ConsoleAppender();
appender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %m%n"));
if (verbose) {
appender.setThreshold(INFO);
} else {
appender.setThreshold(ERROR);
}

appender.activateOptions();
Logger.getRootLogger().addAppender(appender);
Logger.getRootLogger().setLevel(TRACE);
if (verbose) {
Logger.getLogger(Log4jNotifier.class).info("Verbose logging enabled");
}
}
}
18 changes: 6 additions & 12 deletions src/main/java/com/github/tomakehurst/wiremock/WireMockServer.java
Original file line number Diff line number Diff line change
@@ -55,15 +55,15 @@ public class WireMockServer {
private Server jettyServer;
private RequestDelayControl requestDelayControl;
private final FileSource fileSource;
private final Log4jNotifier notifier;
private final Notifier notifier;
private final int port;
private final Integer httpsPort;

public WireMockServer(int port, Integer httpsPort, FileSource fileSource, boolean enableBrowserProxying, ProxySettings proxySettings) {
notifier = new Log4jNotifier();
public WireMockServer(int port, Integer httpsPort, FileSource fileSource, boolean enableBrowserProxying, ProxySettings proxySettings, Notifier notifier) {
this.fileSource = fileSource;
this.port = port;
this.httpsPort = httpsPort;
this.notifier = notifier;

requestDelayControl = new ThreadSafeRequestDelayControl();

@@ -74,10 +74,11 @@ public WireMockServer(int port, Integer httpsPort, FileSource fileSource, boolea
new StubResponseRenderer(fileSource.child(FILES_ROOT),
wireMockApp.getGlobalSettingsHolder(),
new ProxyResponseRenderer(proxySettings)));

}

public WireMockServer(int port, FileSource fileSource, boolean enableBrowserProxying, ProxySettings proxySettings) {
this(port, null, fileSource, enableBrowserProxying, proxySettings);
this(port, null, fileSource, enableBrowserProxying, proxySettings, new Log4jNotifier());
}

public WireMockServer(int port, FileSource fileSource, boolean enableBrowserProxying) {
@@ -89,7 +90,7 @@ public WireMockServer(int port) {
}

public WireMockServer(int port, Integer httpsPort) {
this(port, httpsPort, new SingleRootFileSource("src/test/resources"), false, ProxySettings.NO_PROXY);
this(port, httpsPort, new SingleRootFileSource("src/test/resources"), false, ProxySettings.NO_PROXY, new Log4jNotifier());
}

public WireMockServer() {
@@ -104,13 +105,6 @@ public void addMockServiceRequestListener(RequestListener listener) {
stubRequestHandler.addRequestListener(listener);
}

public void setVerboseLogging(boolean verbose) {
notifier.setVerbose(verbose);
if (verbose) {
notifier.info("Verbose logging enabled");
}
}

public void enableRecordMappings(FileSource mappingsFileSource, FileSource filesFileSource) {
addMockServiceRequestListener(
new StubMappingJsonRecorder(mappingsFileSource, filesFileSource, wireMockApp));
Original file line number Diff line number Diff line change
@@ -15,41 +15,12 @@
*/
package com.github.tomakehurst.wiremock.common;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import static org.apache.log4j.Level.*;

public class Log4jNotifier implements Notifier {

private static final Logger log = Logger.getLogger(Log4jNotifier.class);

private static ConsoleAppender appender;

static {
appender = new ConsoleAppender();
appender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %m%n"));
appender.activateOptions();
appender.setThreshold(ERROR);
Logger.getRootLogger().addAppender(appender);
Logger.getRootLogger().setLevel(TRACE);
}

public Log4jNotifier() {
setVerbose(false);
}

public void setVerbose(boolean verbose) {
if (verbose) {
appender.setThreshold(INFO);
} else {
appender.setThreshold(ERROR);
}

appender.activateOptions();
}

@Override
public void info(String message) {
log.info(message);
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
*/
package com.github.tomakehurst.wiremock.servlet;

import com.github.tomakehurst.wiremock.common.Log4jNotifier;
import com.github.tomakehurst.wiremock.Log4jConfiguration;
import com.github.tomakehurst.wiremock.common.ServletContextFileSource;
import com.github.tomakehurst.wiremock.core.WireMockApp;
import com.github.tomakehurst.wiremock.global.NotImplementedRequestDelayControl;
@@ -38,8 +38,7 @@ public void contextInitialized(ServletContextEvent sce) {
String fileSourceRoot = context.getInitParameter(FILE_SOURCE_ROOT_KEY);

ServletContextFileSource fileSource = new ServletContextFileSource(context, fileSourceRoot);
Log4jNotifier notifier = new Log4jNotifier();
notifier.setVerbose(true);
Log4jConfiguration.configureLogging(true);

WireMockApp wireMockApp = new WireMockApp(new NotImplementedRequestDelayControl(), false);
AdminRequestHandler adminRequestHandler = new AdminRequestHandler(wireMockApp, new BasicResponseRenderer());
Original file line number Diff line number Diff line change
@@ -15,8 +15,10 @@
*/
package com.github.tomakehurst.wiremock.standalone;

import com.github.tomakehurst.wiremock.Log4jConfiguration;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.common.Log4jNotifier;
import com.github.tomakehurst.wiremock.common.SingleRootFileSource;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
@@ -40,6 +42,7 @@ public void run(String fileSourcesRoot, String... args) {
out.println(options.helpText());
return;
}
Log4jConfiguration.configureLogging(options.verboseLoggingEnabled());

FileSource fileSource = new SingleRootFileSource(fileSourcesRoot);
fileSource.createIfNecessary();
@@ -50,17 +53,15 @@ public void run(String fileSourcesRoot, String... args) {

Integer httpsPort = options.specifiesHttpsPortNumber() ? options.httpsPortNumber() : null;
if (options.specifiesPortNumber()) {
wireMockServer = new WireMockServer(options.portNumber(), httpsPort, fileSource, options.browserProxyingEnabled(), options.getProxyVia());
wireMockServer = new WireMockServer(options.portNumber(), httpsPort, fileSource, options.browserProxyingEnabled(), options.getProxyVia(), new Log4jNotifier());
} else {
wireMockServer = new WireMockServer(DEFAULT_PORT, httpsPort, fileSource, options.browserProxyingEnabled(), options.getProxyVia());
wireMockServer = new WireMockServer(DEFAULT_PORT, httpsPort, fileSource, options.browserProxyingEnabled(), options.getProxyVia(), new Log4jNotifier());
}

if (options.recordMappingsEnabled()) {
wireMockServer.enableRecordMappings(mappingsFileSource, filesFileSource);
}

wireMockServer.setVerboseLogging(options.verboseLoggingEnabled());

wireMockServer.loadMappingsUsing(new JsonFileMappingsLoader(mappingsFileSource));

if (options.specifiesProxyUrl()) {
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@ public class AcceptanceTestBase {
@BeforeClass
public static void setupServer() {
wireMockServer = new WireMockServer();
wireMockServer.setVerboseLogging(true);
wireMockServer.start();
testClient = new WireMockTestClient();
WireMock.configure();
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ public class HttpsAcceptanceTest {
@BeforeClass
public static void setupServer() {
wireMockServer = new WireMockServer(HTTP_PORT, HTTPS_PORT);
wireMockServer.setVerboseLogging(true);
wireMockServer.start();
WireMock.configure();

Original file line number Diff line number Diff line change
@@ -131,6 +131,7 @@ public void failsVerificationWhenAbsentHeaderPresent() {
}

@Test
@SuppressWarnings("unchecked")
public void showsExpectedAndReceivedRequestsOnVerificationException() {
testClient.put("/some/request", withHeader("X-My-Stuff", "things"));

@@ -147,6 +148,7 @@ public void showsExpectedAndReceivedRequestsOnVerificationException() {
}

@Test
@SuppressWarnings("unchecked")
public void showsReceivedRequestsOnVerificationException() {
testClient.put("/some/request", withHeader("X-My-Stuff", "things"));

104 changes: 104 additions & 0 deletions testlogging/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>testlogging</groupId>
<artifactId>testlogging</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<slf4jversion>1.7.1</slf4jversion>
</properties>

<repositories>
<repository>
<id>version99</id>
<url>http://version99.qos.ch/</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- COMPILE -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jversion}</version>
<scope>compile</scope>
</dependency>

<!-- TEST -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>1.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4jversion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4jversion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>jul-to-slf4j-config</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4jversion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>99-empty</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>99-empty</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</project>
29 changes: 29 additions & 0 deletions testlogging/src/test/java/WiremockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.junit.Rule;
import org.junit.Test;
import com.github.tomakehurst.wiremock.junit.WireMockRule;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static org.junit.Assert.assertEquals;

public class WiremockTest {

@Rule
public WireMockRule wiremock = new WireMockRule();

@Test
public void useWireMock() throws IOException {
stubFor(get(urlMatching("/blah")).willReturn(aResponse().withStatus(200).withBody("body")));
URL uri = new URL("http://localhost:8080/blah");
InputStream content = uri.openConnection().getInputStream();
final String retrievedBody = IOUtils.toString(content);
assertEquals("body", retrievedBody);
}
}
14 changes: 14 additions & 0 deletions testlogging/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<jmxconfigurator/>
<contextlistener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit 4a4f022

Please sign in to comment.