Logging Layer

With the token manger in place, we can start to introduce the other layer code that will reference the token manager. The first of these will be the logging manager. By moving our logging code to a layer, we can simply make logging calls and have the logging manager inject the tenant context. Not complicated, but good at hiding away tenant details from developers. This will also ensure that all of our log messages always include tenant context.

Below is a file that represents the code of our logging manager. The file gets the standard logger and wrappers it with two log calls that take different sets of parameters. Ultimately, we want to focus our attention on line XX which uses the token manager to acquire the tenant id before adding it to our log message.

package com.amazon.aws.partners.saasfactory;

import com.amazonaws.services.lambda.runtime.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class LoggingManager {

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

    public static void log(Map<String, Object> event, String message) {
        log(event, null, message);
    }

    public static void log(Map<String, Object> event, Context lambdaContext, String message) {
        String tenantId = TokenManager.getTenantId(event);
        LOG.info("Tenant ID [" + tenantId + "] " + message);
    }
}

Next, select the “Lab 4/layers/logging-manager“ folder in the navigation tree on the left. Right click on the folder and select “New file“. The will add an entry to the folder. Type the file name “logging-manager.java“ into the placeholder in the source tree the code above into this new file and save it as “logging-manager.java“ and press enter. Once the file has been created, double-click on the newly added file to open the empty file editor on in the right-hand pane. Now, paste the contents above into the editor and select “File | Save“ from the menu at the top left of the IDE.