Skip to content

Commit c27d157

Browse files
authored
Merge pull request #185 from companieshouse/feature/dsnd-2876_update_structured_logging
DSND-2876: Implement structured logging
2 parents 9b51143 + 20a2bd1 commit c27d157

20 files changed

+290
-504
lines changed

src/main/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiService.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uk.gov.companieshouse.pscdataapi.api;
22

33
import static java.time.ZoneOffset.UTC;
4+
import static uk.gov.companieshouse.pscdataapi.PscDataApiApplication.APPLICATION_NAME_SPACE;
45

56
import com.fasterxml.jackson.core.JsonProcessingException;
67
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -17,6 +18,7 @@
1718
import uk.gov.companieshouse.api.handler.chskafka.request.PrivateChangedResourcePost;
1819
import uk.gov.companieshouse.api.model.ApiResponse;
1920
import uk.gov.companieshouse.logging.Logger;
21+
import uk.gov.companieshouse.logging.LoggerFactory;
2022
import uk.gov.companieshouse.pscdataapi.exceptions.SerDesException;
2123
import uk.gov.companieshouse.pscdataapi.exceptions.ServiceUnavailableException;
2224
import uk.gov.companieshouse.pscdataapi.models.PscDeleteRequest;
@@ -27,6 +29,8 @@
2729
@Service
2830
public class ChsKafkaApiService {
2931

32+
private static final Logger LOGGER = LoggerFactory.getLogger(APPLICATION_NAME_SPACE);
33+
3034
private static final String RESOURCE_CHANGED_URI = "/private/resource-changed";
3135
private static final String PSC_URI = "/company/%s/persons-with-significant-control/%s/%s";
3236
private static final String CHANGED_EVENT_TYPE = "changed";
@@ -36,15 +40,12 @@ public class ChsKafkaApiService {
3640

3741
private final CompanyPscTransformer companyPscTransformer;
3842
private final Supplier<InternalApiClient> kafkaApiClientSupplier;
39-
private final Logger logger;
4043
private final ObjectMapper objectMapper;
4144

4245
public ChsKafkaApiService(CompanyPscTransformer companyPscTransformer,
43-
@Qualifier("kafkaApiClientSupplier") Supplier<InternalApiClient> kafkaApiClientSupplier, Logger logger,
44-
ObjectMapper objectMapper) {
46+
@Qualifier("kafkaApiClientSupplier") Supplier<InternalApiClient> kafkaApiClientSupplier, ObjectMapper objectMapper) {
4547
this.companyPscTransformer = companyPscTransformer;
4648
this.kafkaApiClientSupplier = kafkaApiClientSupplier;
47-
this.logger = logger;
4849
this.objectMapper = objectMapper;
4950
}
5051

@@ -133,12 +134,13 @@ private static String mapKind(String kind) {
133134
private ApiResponse<Void> handleApiCall(PrivateChangedResourcePost changedResourcePost) {
134135
try {
135136
return changedResourcePost.execute();
136-
} catch (ApiErrorResponseException exception) {
137-
logger.error("Unsuccessful call to /resource-changed endpoint", exception);
138-
throw new ServiceUnavailableException(exception.getMessage());
139-
} catch (RuntimeException exception) {
140-
logger.error("Error occurred while calling /resource-changed endpoint", exception);
141-
throw exception;
137+
} catch (ApiErrorResponseException ex) {
138+
final String msg = "Unsuccessful call to resource-changed endpoint";
139+
LOGGER.error(msg, ex);
140+
throw new ServiceUnavailableException(msg);
141+
} catch (RuntimeException ex) {
142+
LOGGER.error("Error occurred while calling resource-changed endpoint", ex);
143+
throw ex;
142144
}
143145
}
144146

src/main/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspect.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package uk.gov.companieshouse.pscdataapi.api;
22

3+
import static uk.gov.companieshouse.pscdataapi.PscDataApiApplication.APPLICATION_NAME_SPACE;
4+
35
import org.aspectj.lang.ProceedingJoinPoint;
46
import org.aspectj.lang.annotation.Around;
57
import org.aspectj.lang.annotation.Aspect;
68
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
79
import org.springframework.stereotype.Component;
810
import uk.gov.companieshouse.logging.Logger;
11+
import uk.gov.companieshouse.logging.LoggerFactory;
912
import uk.gov.companieshouse.pscdataapi.config.FeatureFlags;
13+
import uk.gov.companieshouse.pscdataapi.logging.DataMapHolder;
1014

1115
@Aspect
1216
@Component
1317
@ConditionalOnProperty(prefix = "feature", name = "seeding_collection_enabled")
1418
public class ResourceChangedApiServiceAspect {
1519

20+
private static final Logger LOGGER = LoggerFactory.getLogger(APPLICATION_NAME_SPACE);
21+
1622
private final FeatureFlags featureFlags;
17-
private final Logger logger;
1823

19-
public ResourceChangedApiServiceAspect(FeatureFlags featureFlags, Logger logger) {
24+
public ResourceChangedApiServiceAspect(FeatureFlags featureFlags) {
2025
this.featureFlags = featureFlags;
21-
this.logger = logger;
2226
}
2327

2428
/**
@@ -32,10 +36,10 @@ public ResourceChangedApiServiceAspect(FeatureFlags featureFlags, Logger logger)
3236
public Object invokeChsKafkaApi(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
3337

3438
if (featureFlags.isStreamHookDisabled()) {
35-
logger.debug("Stream hook disabled; not publishing change to chs-kafka-api");
39+
LOGGER.debug("Stream hook disabled; not publishing change to chs-kafka-api", DataMapHolder.getLogMap());
3640
return null;
3741
} else {
38-
logger.debug("Stream hook enabled; publishing change to chs-kafka-api");
42+
LOGGER.debug("Stream hook enabled; publishing change to chs-kafka-api", DataMapHolder.getLogMap());
3943
return proceedingJoinPoint.proceed();
4044
}
4145
}

src/main/java/uk/gov/companieshouse/pscdataapi/config/ExceptionHandlerConfig.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package uk.gov.companieshouse.pscdataapi.config;
22

3+
import static uk.gov.companieshouse.pscdataapi.PscDataApiApplication.APPLICATION_NAME_SPACE;
4+
35
import com.mongodb.MongoException;
46
import java.time.LocalDateTime;
57
import java.time.format.DateTimeParseException;
68
import java.util.LinkedHashMap;
79
import java.util.Map;
8-
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.dao.DataAccessException;
1011
import org.springframework.http.HttpStatus;
1112
import org.springframework.http.ResponseEntity;
@@ -16,6 +17,7 @@
1617
import org.springframework.web.context.request.WebRequest;
1718
import org.springframework.web.servlet.NoHandlerFoundException;
1819
import uk.gov.companieshouse.logging.Logger;
20+
import uk.gov.companieshouse.logging.LoggerFactory;
1921
import uk.gov.companieshouse.pscdataapi.exceptions.BadGatewayException;
2022
import uk.gov.companieshouse.pscdataapi.exceptions.BadRequestException;
2123
import uk.gov.companieshouse.pscdataapi.exceptions.ConflictException;
@@ -26,13 +28,7 @@
2628
@ControllerAdvice
2729
public class ExceptionHandlerConfig {
2830

29-
private final Logger logger;
30-
31-
@Autowired
32-
public ExceptionHandlerConfig(Logger logger) {
33-
this.logger = logger;
34-
}
35-
31+
private static final Logger LOGGER = LoggerFactory.getLogger(APPLICATION_NAME_SPACE);
3632
private static final String TIMESTAMP = "timestamp";
3733
private static final String MESSAGE = "message";
3834
private static final String EXCEPTION_ATTRIBUTE = "javax.servlet.error.exception";
@@ -46,8 +42,7 @@ public ExceptionHandlerConfig(Logger logger) {
4642
*/
4743
@ExceptionHandler(value = {Exception.class, SerDesException.class})
4844
public ResponseEntity<Object> handleException(Exception ex, WebRequest request) {
49-
logger.error(String.format("Unexpected exception, response code: %s",
50-
HttpStatus.INTERNAL_SERVER_ERROR), ex);
45+
LOGGER.error("Unexpected exception, response code: %s".formatted(HttpStatus.INTERNAL_SERVER_ERROR), ex);
5146

5247
Map<String, Object> responseBody = new LinkedHashMap<>();
5348
responseBody.put(TIMESTAMP, LocalDateTime.now());
@@ -65,8 +60,7 @@ public ResponseEntity<Object> handleException(Exception ex, WebRequest request)
6560
*/
6661
@ExceptionHandler(value = {IllegalArgumentException.class, NoHandlerFoundException.class})
6762
public ResponseEntity<Object> handleNotFoundException(Exception ex, WebRequest request) {
68-
logger.error(String.format("Resource not found, response code: %s",
69-
HttpStatus.NOT_FOUND), ex);
63+
LOGGER.error("Resource not found, response code: %s".formatted(HttpStatus.NOT_FOUND), ex);
7064

7165
Map<String, Object> responseBody = new LinkedHashMap<>();
7266
responseBody.put(TIMESTAMP, LocalDateTime.now());
@@ -84,10 +78,8 @@ public ResponseEntity<Object> handleNotFoundException(Exception ex, WebRequest r
8478
*/
8579
@ExceptionHandler(value = {MethodNotAllowedException.class,
8680
HttpRequestMethodNotSupportedException.class})
87-
public ResponseEntity<Object> handleMethodNotAllowedException(
88-
Exception ex, WebRequest request) {
89-
logger.error(String.format("Unable to process the request, response code: %s",
90-
HttpStatus.METHOD_NOT_ALLOWED), ex);
81+
public ResponseEntity<Object> handleMethodNotAllowedException(Exception ex, WebRequest request) {
82+
LOGGER.error("Method not allowed, response code: %s".formatted(HttpStatus.METHOD_NOT_ALLOWED), ex);
9183

9284
Map<String, Object> responseBody = new LinkedHashMap<>();
9385
responseBody.put(TIMESTAMP, LocalDateTime.now());
@@ -107,8 +99,7 @@ public ResponseEntity<Object> handleMethodNotAllowedException(
10799
DataAccessException.class, MongoException.class})
108100
public ResponseEntity<Object> handleServiceUnavailableException(Exception ex,
109101
WebRequest request) {
110-
logger.error(String.format("Service unavailable, response code: %s",
111-
HttpStatus.SERVICE_UNAVAILABLE), ex);
102+
LOGGER.error("Service unavailable, response code: %s".formatted(HttpStatus.SERVICE_UNAVAILABLE), ex);
112103

113104
Map<String, Object> responseBody = new LinkedHashMap<>();
114105
responseBody.put(TIMESTAMP, LocalDateTime.now());
@@ -127,7 +118,7 @@ public ResponseEntity<Object> handleServiceUnavailableException(Exception ex,
127118
@ExceptionHandler(value = {BadRequestException.class, DateTimeParseException.class,
128119
HttpMessageNotReadableException.class})
129120
public ResponseEntity<Object> handleBadRequestException(Exception ex, WebRequest request) {
130-
logger.error(String.format("Bad request, response code: %s", HttpStatus.BAD_REQUEST), ex);
121+
LOGGER.error("Bad request, response code: %s".formatted(HttpStatus.BAD_REQUEST), ex);
131122

132123
Map<String, Object> responseBody = new LinkedHashMap<>();
133124
responseBody.put(TIMESTAMP, LocalDateTime.now());
@@ -146,7 +137,7 @@ public ResponseEntity<Object> handleBadRequestException(Exception ex, WebRequest
146137
*/
147138
@ExceptionHandler(value = {ConflictException.class})
148139
public ResponseEntity<Object> handleConflictException(Exception ex, WebRequest request) {
149-
logger.error(String.format("Conflict, response code: %s", HttpStatus.CONFLICT), ex);
140+
LOGGER.error("Conflict, response code: %s".formatted(HttpStatus.CONFLICT), ex);
150141

151142
Map<String, Object> responseBody = new LinkedHashMap<>();
152143
responseBody.put(TIMESTAMP, LocalDateTime.now());
@@ -157,7 +148,7 @@ public ResponseEntity<Object> handleConflictException(Exception ex, WebRequest r
157148

158149
@ExceptionHandler(value = {BadGatewayException.class})
159150
public ResponseEntity<Object> handleBadGatewayException(Exception ex, WebRequest request) {
160-
logger.error(String.format("Bad Gateway, response code: %s", HttpStatus.BAD_GATEWAY), ex);
151+
LOGGER.error("Bad Gateway, response code: %s".formatted(HttpStatus.BAD_GATEWAY), ex);
161152

162153
Map<String, Object> responseBody = new LinkedHashMap<>();
163154
responseBody.put(TIMESTAMP, LocalDateTime.now());

src/main/java/uk/gov/companieshouse/pscdataapi/config/LoggingConfig.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)