Skip to content

Commit

Permalink
CIRC-1709: Add support for logging variables (#1321)
Browse files Browse the repository at this point in the history
* CIRC-1709 Populate logging context

* CIRC-1709 Do not clear logging context

* CIRC-1709 Add logging

* CIRC-1709 Remove redundant info from log

* CIRC-1709 Make public constant private

* CIRC-1709 Do not clear logging context
  • Loading branch information
OleksandrVidinieiev authored Sep 16, 2023
1 parent 76bbd2c commit d741346
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/folio/circulation/CirculationVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public void start(Promise<Void> startFuture) {

this.server = vertx.createHttpServer();

router.route().handler(rc -> LogHelper.logRequest(rc, log));
router.route()
.handler(LogHelper::populateLoggingContext)
.handler(rc -> LogHelper.logRequest(rc, log));

new HealthResource().register(router);
new TenantActivationResource().register(router);
Expand Down
39 changes: 26 additions & 13 deletions src/main/java/org/folio/circulation/support/logging/LogHelper.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
package org.folio.circulation.support.logging;

import static java.util.stream.Collectors.toList;
import static org.folio.circulation.support.http.OkapiHeader.REQUEST_ID;
import static org.folio.circulation.support.http.OkapiHeader.TENANT;

import io.vertx.ext.web.RoutingContext;
import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.function.Function;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.MultipleRecords;
import org.folio.circulation.support.http.client.Response;
import org.folio.circulation.support.results.Result;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.okapi.common.logging.FolioLoggingContext;
import org.folio.rest.RestVerticle;

import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.RoutingContext;

public class LogHelper {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private static final String MODULE_NAME = "mod-circulation";

private LogHelper() {
throw new UnsupportedOperationException("Do not instantiate");
}

private static String null2empty(String s) {
return (s == null) ? "" : s;
}

public static void logRequest(RoutingContext rc, Logger logger) {
if (logger.isInfoEnabled()) {
logger.info("[{}] [{}] {} {}",
null2empty(rc.request().getHeader(REQUEST_ID)),
null2empty(rc.request().getHeader(TENANT)),
rc.request().method(), rc.request().path());
logger.info("Invoking {} {}", rc.request().method(), rc.request().path());
}
rc.next();
}
Expand Down Expand Up @@ -73,4 +69,21 @@ public static <T> String asString(Collection<T> collection, Function<T, String>
collection.size(),
collection.stream().map(elementMapper).collect(toList()));
}

public static void populateLoggingContext(RoutingContext routingContext) {
final HttpServerRequest request = routingContext.request();
String tenantId = request.getHeader(RestVerticle.OKAPI_HEADER_TENANT);
String requestId = request.getHeader(RestVerticle.OKAPI_REQUESTID_HEADER);
String userId = request.getHeader(RestVerticle.OKAPI_USERID_HEADER);

log.debug("populateLoggingContext:: populating context: tenantId={}, requestId={}, userId={}",
tenantId, requestId, userId);

FolioLoggingContext.put(FolioLoggingContext.TENANT_ID_LOGGING_VAR_NAME, tenantId);
FolioLoggingContext.put(FolioLoggingContext.REQUEST_ID_LOGGING_VAR_NAME, requestId);
FolioLoggingContext.put(FolioLoggingContext.USER_ID_LOGGING_VAR_NAME, userId);
FolioLoggingContext.put(FolioLoggingContext.MODULE_ID_LOGGING_VAR_NAME, MODULE_NAME);

routingContext.next();
}
}

0 comments on commit d741346

Please sign in to comment.