Skip to content

Commit

Permalink
Adjust examples / docs
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Jan 20, 2025
1 parent b19628a commit 6db6a9e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Features:
* Selective 'on-demand' logging for unexpected web server behaviour
* capture full logs for problematic requests (i.e. not only WARN or ERROR, but also all sibling INFO log statements)
* reduce cost of logging considerably
* only supported for sync calls for now
* Unit testing
* Always assert against machine-readable JSON 'under the hood', regardless what is printed to console during local development
* Supported frameworks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.entur.example.web.rest;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/async-document")
public class AsyncDocumentEndpoint {

private final static Logger logger = LoggerFactory.getLogger(AsyncDocumentEndpoint.class);

@PostMapping("/some/method")
public CompletableFuture<MyEntity> someMessage(@RequestBody MyEntity entity) {
logger.trace("Hello entity with secret / trace");
logger.debug("Hello entity with secret / debug");
logger.info("Hello entity with secret / info");
logger.warn("Hello entity with secret / warn");
logger.error("Hello entity with secret / error");

entity.setName("Entur response");
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return entity;
});
}

@PostMapping("/some/error")
public CompletableFuture<ResponseEntity> errorMethod(@RequestBody MyEntity entity) throws InterruptedException {
System.out.flush();
System.out.println("System out before endpoint logging on thread " + Thread.currentThread().getName());

logger.trace("This message should be ignored / trace");
logger.debug("This message should be ignored / debug");
logger.info("This message should be delayed / info");
logger.warn("This message should be logged / warn");

Thread.sleep(1000);
System.out.println("System out after endpoint logging + 1000ms");

return CompletableFuture.supplyAsync(() -> {
System.out.println("Complete future on thread " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new ResponseEntity(HttpStatus.NOT_FOUND);
});
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.entur.example.web;

import no.entur.logging.cloud.logback.logstash.test.CompositeConsoleOutputControl;
import no.entur.logging.cloud.logback.logstash.test.CompositeConsoleOutputControlClosable;
import org.entur.example.web.rest.MyEntity;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.Assertions.assertThat;

/**
*
* Note: Async does not currently work with on-demand logging.
*
*/

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"entur.logging.http.ondemand.enabled=true", "entur.logging.http.ondemand.failure.http.statusCode.equalOrHigherThan=400"})
public class OndemandWebLoggingHttpNotFound1AsyncTest {

@LocalServerPort
private int randomServerPort;

@Autowired
private TestRestTemplate restTemplate;

@Test
@Disabled
public void useHumanReadablePlainEncoderExpectFullLogging() {
MyEntity entity = new MyEntity();
entity.setName("Entur");
entity.setSecret("mySecret");

ResponseEntity<MyEntity> response = restTemplate.postForEntity("/api/async-document/some/error", entity, MyEntity.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
@Disabled
public void useHumanReadableJsonEncoderExpectFullLogging() {
try (CompositeConsoleOutputControlClosable c = CompositeConsoleOutputControl.useHumanReadableJsonEncoder()) {
MyEntity entity = new MyEntity();
entity.setName("Entur");
entity.setSecret("mySecret");

ResponseEntity<MyEntity> response = restTemplate.postForEntity("/api/async-document/some/error", entity, MyEntity.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
}

@Test
@Disabled
public void useMachineReadableJsonEncoderExpectFullLogging() {
try (CompositeConsoleOutputControlClosable c = CompositeConsoleOutputControl.useMachineReadableJsonEncoder()) {
MyEntity entity = new MyEntity();
entity.setName("Entur");
entity.setSecret("mySecret");

ResponseEntity<MyEntity> response = restTemplate.postForEntity("/api/async-document/some/error", entity, MyEntity.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
}

}

0 comments on commit 6db6a9e

Please sign in to comment.