-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for access log in Vert.x HTTP
Fixes #5556
- Loading branch information
1 parent
3df1f24
commit 7b9c133
Showing
53 changed files
with
3,202 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...-http/deployment/src/test/java/io/quarkus/vertx/http/accesslog/AccessLogFileTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2018 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.quarkus.vertx.http.accesslog; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.awaitility.core.ThrowingRunnable; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.bootstrap.util.IoUtils; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
/** | ||
* Tests writing the access log to a file | ||
* | ||
* @author Stuart Douglas | ||
*/ | ||
public class AccessLogFileTestCase { | ||
|
||
@RegisterExtension | ||
public static QuarkusUnitTest unitTest = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<JavaArchive>() { | ||
@Override | ||
public JavaArchive get() { | ||
Path logDirectory; | ||
try { | ||
logDirectory = Files.createTempDirectory("quarkus-tests"); | ||
//backslash is an escape char, we need this to be properly formatted for windows | ||
Properties p = new Properties(); | ||
p.setProperty("quarkus.http.access-log.enabled", "true"); | ||
p.setProperty("quarkus.http.access-log.log-to-file", "true"); | ||
p.setProperty("quarkus.http.access-log.base-file-name", "server"); | ||
p.setProperty("quarkus.http.access-log.log-directory", logDirectory.toAbsolutePath().toString()); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
p.store(out, null); | ||
|
||
return ShrinkWrap.create(JavaArchive.class) | ||
.add(new ByteArrayAsset(out.toByteArray()), | ||
"application.properties"); | ||
|
||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}); | ||
|
||
@ConfigProperty(name = "quarkus.http.access-log.log-directory") | ||
Path logDirectory; | ||
|
||
@BeforeEach | ||
public void before() throws IOException { | ||
Files.createDirectories(logDirectory); | ||
} | ||
|
||
@AfterEach | ||
public void after() throws IOException { | ||
IoUtils.recursiveDelete(logDirectory); | ||
} | ||
|
||
@Test | ||
public void testSingleLogMessageToFile() throws IOException, InterruptedException { | ||
RestAssured.get("/does-not-exist"); | ||
|
||
Awaitility.given().pollInterval(100, TimeUnit.MILLISECONDS) | ||
.atMost(10, TimeUnit.SECONDS) | ||
.untilAsserted(new ThrowingRunnable() { | ||
@Override | ||
public void run() throws Throwable { | ||
try (Stream<Path> files = Files.list(logDirectory)) { | ||
Assertions.assertEquals(1, (int) files.count()); | ||
} | ||
Path path = logDirectory.resolve("server.log"); | ||
Assertions.assertTrue(Files.exists(path)); | ||
String data = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); | ||
Assertions.assertTrue(data.contains("404")); | ||
Assertions.assertTrue(data.contains("/does-not-exist")); | ||
} | ||
}); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...sions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/AccessLogConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.vertx.http.runtime; | ||
|
||
import java.util.Optional; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class AccessLogConfig { | ||
|
||
/** | ||
* If access logging is enabled. By default this will log via the standard logging facility | ||
*/ | ||
@ConfigItem(defaultValue = "false") | ||
public boolean enabled; | ||
|
||
/** | ||
* The access log pattern: | ||
* | ||
* If this is the string 'common' or 'combined' then this will use one of the specified named formats: | ||
* | ||
* common: %h %l %u %t "%r" %s %b | ||
* combined: %h %l %u %t "%r" %s %b "%{i,Referer}" "%{i,User-Agent}" | ||
* | ||
* Otherwise consult the Quarkus documentation for the full list of variables that can be used. | ||
* | ||
*/ | ||
@ConfigItem(defaultValue = "common") | ||
public String pattern; | ||
|
||
/** | ||
* If logging should be done to a separate file. | ||
*/ | ||
@ConfigItem(defaultValue = "false") | ||
public boolean logToFile; | ||
|
||
/** | ||
* The access log file base name, defaults to 'quarkus' which will give a log file | ||
* name of 'quarkus.log'. | ||
* | ||
*/ | ||
@ConfigItem(defaultValue = "quarkus") | ||
public String baseFileName; | ||
|
||
/** | ||
* The log directory to use when logging access to a file | ||
* | ||
* If this is not set then the current working directory is used. | ||
*/ | ||
@ConfigItem | ||
public Optional<String> logDirectory; | ||
|
||
/** | ||
* The log file suffix | ||
*/ | ||
@ConfigItem(defaultValue = ".log") | ||
public String logSuffix; | ||
|
||
/** | ||
* The log category to use if logging is being done via the standard log mechanism (i.e. if base-file-name is empty). | ||
* | ||
*/ | ||
@ConfigItem(defaultValue = "io.quarkus.http.access-log") | ||
public String category; | ||
|
||
/** | ||
* If the log should be rotated daily | ||
*/ | ||
@ConfigItem(defaultValue = "true") | ||
public boolean rotate; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.