From 41918b1decb38ca98f552b9eb3f51b3edcd2e0ea Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Fri, 19 Jul 2019 12:49:06 +0100 Subject: [PATCH] Add loglevel config --- .../pages/corefunctionality/configuration.rst | 13 +++++++++--- .../specto/hoverfly/junit/core/Hoverfly.java | 4 ++++ .../core/config/HoverflyConfiguration.java | 9 ++++++++- .../core/config/LocalHoverflyConfig.java | 15 ++++++++++++-- .../hoverfly/junit/core/config/LogLevel.java | 11 ++++++++++ .../junit/core/HoverflyConfigTest.java | 13 ++++++++++++ .../hoverfly/junit/core/HoverflyTest.java | 20 +++++++++++++++++++ src/test/resources/logback.xml | 2 +- 8 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/specto/hoverfly/junit/core/config/LogLevel.java diff --git a/docs/pages/corefunctionality/configuration.rst b/docs/pages/corefunctionality/configuration.rst index ced5936c..17fe4c95 100644 --- a/docs/pages/corefunctionality/configuration.rst +++ b/docs/pages/corefunctionality/configuration.rst @@ -55,8 +55,8 @@ If you are developing behind a cooperate proxy, you can configure Hoverfly to us Logging ------- -Hoverfly logs to SLF4J by default, meaning that you can control log level using standard logging configuration. Here is an example ``logback.xml`` that set -Hoverfly log level to WARN: +Hoverfly logs to SLF4J by default, meaning that you have control of Hoverfly logs using JAVA logging framework. +Here is an example ``logback.xml`` that directs Hoverfly WARN logs to the console: .. code-block:: xml @@ -74,7 +74,7 @@ Hoverfly log level to WARN: - + @@ -93,6 +93,13 @@ Or change the log output directly to stdout: localConfigs().logToStdOut() +Hoverfly by default generates ``INFO`` logs regardless of the external SLF4J logger configs. To get debug logging, you need +to set the log level explicitly: + +.. code-block:: java + + localConfigs().logLevel(LogLevel.DEBUG) + Middleware ---------- diff --git a/src/main/java/io/specto/hoverfly/junit/core/Hoverfly.java b/src/main/java/io/specto/hoverfly/junit/core/Hoverfly.java index ffad6fde..cf1d64b6 100644 --- a/src/main/java/io/specto/hoverfly/junit/core/Hoverfly.java +++ b/src/main/java/io/specto/hoverfly/junit/core/Hoverfly.java @@ -209,6 +209,10 @@ private void startHoverflyProcess() { commands.add("json"); } + if (hoverflyConfig.getLogLevel().isPresent()) { + commands.add("-log-level"); + commands.add(hoverflyConfig.getLogLevel().get().name().toLowerCase()); + } if (hoverflyConfig.isMiddlewareEnabled()) { final String path = hoverflyConfig.getLocalMiddleware().getPath(); diff --git a/src/main/java/io/specto/hoverfly/junit/core/config/HoverflyConfiguration.java b/src/main/java/io/specto/hoverfly/junit/core/config/HoverflyConfiguration.java index 0bb37fd9..09445230 100644 --- a/src/main/java/io/specto/hoverfly/junit/core/config/HoverflyConfiguration.java +++ b/src/main/java/io/specto/hoverfly/junit/core/config/HoverflyConfiguration.java @@ -34,6 +34,7 @@ public class HoverflyConfiguration { private boolean plainHttpTunneling; private String upstreamProxy; private Logger hoverflyLogger; + private LogLevel logLevel; private boolean statefulCapture; private SimulationPreprocessor simulationPreprocessor; private String binaryNameFormat; @@ -55,7 +56,7 @@ public class HoverflyConfiguration { final boolean webServer, final boolean statefulCapture, final SimulationPreprocessor preprocessor) { - this(proxyPort, adminPort, proxyLocalHost, destination, proxyCaCertificate, captureHeaders, webServer, null, statefulCapture, preprocessor); + this(proxyPort, adminPort, proxyLocalHost, destination, proxyCaCertificate, captureHeaders, webServer, null, null, statefulCapture, preprocessor); setScheme(scheme); setHost(host); this.authToken = authToken; @@ -74,6 +75,7 @@ public HoverflyConfiguration(final int proxyPort, final List captureHeaders, final boolean webServer, final Logger hoverflyLogger, + final LogLevel logLevel, final boolean statefulCapture, final SimulationPreprocessor preprocessor ) { @@ -85,6 +87,7 @@ public HoverflyConfiguration(final int proxyPort, this.captureHeaders = captureHeaders; this.webServer = webServer; this.hoverflyLogger = hoverflyLogger; + this.logLevel = logLevel; this.statefulCapture = statefulCapture; this.simulationPreprocessor = preprocessor; } @@ -239,6 +242,10 @@ public Optional getHoverflyLogger() { return Optional.ofNullable(hoverflyLogger); } + public Optional getLogLevel() { + return Optional.ofNullable(logLevel); + } + public boolean isStatefulCapture() { return statefulCapture; } diff --git a/src/main/java/io/specto/hoverfly/junit/core/config/LocalHoverflyConfig.java b/src/main/java/io/specto/hoverfly/junit/core/config/LocalHoverflyConfig.java index 407d23a7..b488fa6f 100644 --- a/src/main/java/io/specto/hoverfly/junit/core/config/LocalHoverflyConfig.java +++ b/src/main/java/io/specto/hoverfly/junit/core/config/LocalHoverflyConfig.java @@ -21,7 +21,6 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import java.util.Optional; /** * Config builder interface for settings specific to {@link Hoverfly} managed internally @@ -36,6 +35,7 @@ public class LocalHoverflyConfig extends HoverflyConfig { private LocalMiddleware localMiddleware; private String upstreamProxy; private Logger hoverflyLogger = LoggerFactory.getLogger("hoverfly"); + private LogLevel logLevel; private List commands = new LinkedList<>(); /** @@ -46,6 +46,7 @@ public class LocalHoverflyConfig extends HoverflyConfig { */ public LocalHoverflyConfig sslCertificatePath(String sslCertificatePath) { this.sslCertificatePath = sslCertificatePath; + return this; } @@ -125,6 +126,16 @@ public LocalHoverflyConfig logToStdOut() { return this; } + /** + * Set the log level of Hoverfly. The default level is INFO. + * @param logLevel {@link LogLevel} to set + * @return the {@link HoverflyConfig} for further customizations + */ + public LocalHoverflyConfig logLevel(LogLevel logLevel) { + this.logLevel = logLevel; + return this; + } + /** * Set additional commands for starting Hoverfly. * @param commands More Hoverfly command flags. @@ -139,7 +150,7 @@ public LocalHoverflyConfig addCommands(String... commands) { @Override public HoverflyConfiguration build() { HoverflyConfiguration configs = new HoverflyConfiguration(proxyPort, adminPort, proxyLocalHost, destination, - proxyCaCert, captureHeaders, webServer, hoverflyLogger, statefulCapture, simulationPreprocessor); + proxyCaCert, captureHeaders, webServer, hoverflyLogger, logLevel, statefulCapture, simulationPreprocessor); configs.setSslCertificatePath(sslCertificatePath); configs.setSslKeyPath(sslKeyPath); configs.setTlsVerificationDisabled(tlsVerificationDisabled); diff --git a/src/main/java/io/specto/hoverfly/junit/core/config/LogLevel.java b/src/main/java/io/specto/hoverfly/junit/core/config/LogLevel.java new file mode 100644 index 00000000..17d3521d --- /dev/null +++ b/src/main/java/io/specto/hoverfly/junit/core/config/LogLevel.java @@ -0,0 +1,11 @@ +package io.specto.hoverfly.junit.core.config; + +public enum LogLevel { + + DEBUG, + INFO, + WARN, + ERROR, + FATAL, + PANIC +} diff --git a/src/test/java/io/specto/hoverfly/junit/core/HoverflyConfigTest.java b/src/test/java/io/specto/hoverfly/junit/core/HoverflyConfigTest.java index 93711eab..a7f78e25 100644 --- a/src/test/java/io/specto/hoverfly/junit/core/HoverflyConfigTest.java +++ b/src/test/java/io/specto/hoverfly/junit/core/HoverflyConfigTest.java @@ -2,9 +2,13 @@ import io.specto.hoverfly.junit.core.config.HoverflyConfiguration; import java.net.InetSocketAddress; +import java.util.Optional; + +import io.specto.hoverfly.junit.core.config.LogLevel; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.EnvironmentVariables; +import org.slf4j.LoggerFactory; import static io.specto.hoverfly.junit.core.HoverflyConfig.localConfigs; import static io.specto.hoverfly.junit.core.HoverflyConfig.remoteConfigs; @@ -35,6 +39,8 @@ public void shouldHaveDefaultSettings() { assertThat(configs.isWebServer()).isFalse(); assertThat(configs.isTlsVerificationDisabled()).isFalse(); assertThat(configs.isStatefulCapture()).isFalse(); + assertThat(configs.getLogLevel()).isNotPresent(); + assertThat(configs.getHoverflyLogger()).isEqualTo(Optional.of(LoggerFactory.getLogger("hoverfly"))); } @Test @@ -173,4 +179,11 @@ public void shouldAddCommands() { assertThat(configs.getCommands()).containsExactly("-log-level", "error", "-disable-cache", "-generate-ca-cert"); } + + @Test + public void shouldSetLogLevel() { + HoverflyConfiguration configs = localConfigs().logLevel(LogLevel.DEBUG).build(); + + assertThat(configs.getLogLevel()).isEqualTo(Optional.of(LogLevel.DEBUG)); + } } diff --git a/src/test/java/io/specto/hoverfly/junit/core/HoverflyTest.java b/src/test/java/io/specto/hoverfly/junit/core/HoverflyTest.java index 258cb84a..b746a9a3 100644 --- a/src/test/java/io/specto/hoverfly/junit/core/HoverflyTest.java +++ b/src/test/java/io/specto/hoverfly/junit/core/HoverflyTest.java @@ -13,6 +13,7 @@ import io.specto.hoverfly.junit.api.model.ModeArguments; import io.specto.hoverfly.junit.api.view.HoverflyInfoView; import io.specto.hoverfly.junit.core.config.LocalHoverflyConfig; +import io.specto.hoverfly.junit.core.config.LogLevel; import io.specto.hoverfly.junit.core.model.DelaySettings; import io.specto.hoverfly.junit.core.model.RequestFieldMatcher; import io.specto.hoverfly.junit.core.model.RequestResponsePair; @@ -30,6 +31,9 @@ import org.mockito.Mockito; import org.powermock.reflect.Whitebox; import org.slf4j.LoggerFactory; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; import org.zeroturnaround.exec.StartedProcess; import javax.net.ssl.SSLContext; @@ -66,6 +70,22 @@ public void shouldStartHoverflyOnConfiguredPort() { assertThat(hoverfly.getHoverflyConfig().getProxyPort()).isEqualTo(EXPECTED_PROXY_PORT); } + @Test + public void shouldSetDebugLogging() { + systemOut.enableLog(); + hoverfly = new Hoverfly(localConfigs().logToStdOut().logLevel(LogLevel.DEBUG), SIMULATE); + hoverfly.start(); + + RestTemplate restTemplate = new RestTemplate(); + + try { + restTemplate.getForEntity("https://test.api", Void.class); + } catch (RestClientException ignored) { + } + + assertThat(systemOut.getLogWithNormalizedLineSeparator()).contains("Checking cache for request"); + } + @Test public void shouldLogToStdOut() { final Appender appender = Mockito.mock(Appender.class); diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml index 1fb872de..25f97bf7 100644 --- a/src/test/resources/logback.xml +++ b/src/test/resources/logback.xml @@ -13,7 +13,7 @@ - +