Skip to content

Commit

Permalink
Create Logging JSON Codestart
Browse files Browse the repository at this point in the history
  • Loading branch information
jantosi authored and xumk committed Oct 14, 2020
1 parent a5902b2 commit ababff3
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
== Logging JSON

Guide: https://quarkus.io/guides/logging

=== Example customization

Here are a few examples of customization of logging as JSON.
Place these in your src/main/resources/application.properties.

* Enable "pretty printing" of the JSON record. Note that some JSON parsers will fail to read pretty printed output.
```properties
quarkus.log.console.json.pretty-print=true
```

* Use a custom date format.
```properties
quarkus.log.console.json.date-format=YYYY-MM-dd HH:mm:ss
```

* More detailed exceptions in JSON logs
```properties
quarkus.log.console.json.exception-output-type=detailed-and-formatted
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: logging-json-example
ref: logging-json
type: code
tags: example
language:
base:
dependencies:
- io.quarkus:quarkus-resteasy
- io.quarkus:quarkus-logging-json
test-dependencies:
- io.rest-assured:rest-assured
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.acme.logging.json;

import org.jboss.logging.Logger;
import org.jboss.resteasy.spi.NotImplementedYetException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/logging-json")
public class LoggingJsonExampleResource {

private static final Logger LOG = Logger.getLogger(LoggingJsonExampleResource.class);

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
LOG.info("Reached LoggingJsonExampleResource hello() method");
return "hello";
}

@GET
@Path("/goodbye")
@Produces(MediaType.TEXT_PLAIN)
public String goodbye() {
LOG.info("Reached LoggingJsonExampleResource goodbye() method, about to throw an Exception");
throw new ServerErrorException(
Response.Status.INTERNAL_SERVER_ERROR,
new NotImplementedYetException("goodbye() not implemented yet"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.acme.logging.json;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
public class NativeExampleResourceIT extends ExampleResourceTest {

// Execute the same tests but in native mode.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.acme.logging.json;

import io.quarkus.test.junit.QuarkusTest;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@QuarkusTest
class ExampleResourceTest {

@Test
void testHelloEndpoint() {
given()
.when().get("/logging-json/")
.then()
.statusCode(200)
.body(is("hello"));
}

@Test
void testGoodbyeEndpoint() {
given()
.when().get("/logging-json/goodbye")
.then()
.statusCode(500)
.body(is(Matchers.emptyString()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.acme.logging.json

import org.jboss.logging.Logger
import org.jboss.resteasy.spi.NotImplementedYetException
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.ServerErrorException
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response

@Path("/logging-json")
class LoggingJsonExampleResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
fun hello(): String {
LOG.info("Reached LoggingJsonExampleResource hello() method")
return "hello"
}

@GET
@Path("/goodbye")
@Produces(MediaType.TEXT_PLAIN)
fun goodbye(): String {
LOG.info("Reached LoggingJsonExampleResource goodbye() method, about to throw an Exception")
throw ServerErrorException(
Response.Status.INTERNAL_SERVER_ERROR,
NotImplementedYetException("goodbye() not implemented yet"))
}

companion object {
private val LOG = Logger.getLogger(LoggingJsonExampleResource::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.acme.logging.json

import io.quarkus.test.junit.NativeImageTest

@NativeImageTest
class NativeExampleResourceIT : ExampleResourceTest() { // Execute the same tests but in native mode.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.acme.logging.json

import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import org.hamcrest.CoreMatchers
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test

@QuarkusTest
class ExampleResourceTest {
@Test
fun testHelloEndpoint() {
RestAssured.given()
.`when`().get("/logging-json/")
.then()
.statusCode(200)
.body(CoreMatchers.`is`("hello"))
}

@Test
fun testGoodbyeEndpoint() {
RestAssured.given()
.`when`().get("/logging-json/goodbye")
.then()
.statusCode(500)
.body(CoreMatchers.`is`(Matchers.emptyString()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ metadata:
categories:
- "core"
status: "preview"
guide: "https://quarkus.io/guides/logging#json-logging"
guide: "https://quarkus.io/guides/logging#json-logging"
codestart: logging-json
13 changes: 13 additions & 0 deletions independent-projects/tools/codestarts/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ This will be appended to all different selected examples.
The Foo Example shows...
----

4. Run the generator you've created

Use the QuarkusCodestartRunSingleIT
(`integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/QuarkusCodestartRunSingleIT.java`)
integration test to generate a fresh project from your codestart.

Customize the `Set<String> INCLUDED` to add your codestart, and any other you might want to bundle.

This will give you the experience similar to the code.quarkus.io - and you will be able to build and run the examples
you added. After running the test, see `integration-tests/devtools/target/codestarts-run-test/your-codestart-name` to
inspect the results.


=== Tips for writing extension example code

- Your example must/should be independent from buildtool and dockerfiles
Expand Down

0 comments on commit ababff3

Please sign in to comment.