/** * ============LICENSE_START======================================================= * org.onap.logging * ================================================================================ * Copyright © 2018 Amdocs * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.onap.logging.ref.slf4j; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.MDC; import org.slf4j.Marker; import org.slf4j.event.Level; /** * Extensible adapter for cheaply meeting ONAP logging obligations using * an SLF4J facade. * *
This can be used with any SLF4J-compatible logging provider, with * appropriate provider configuration.
* *The basics are that: *
Minimal usage is: *
... if you're happy for service information to be automatically derived as follows: *
... and if those defaults don't suit, then you can override using properties on * {@link #getServiceDescriptor()}, or by injecting your own adapter using * {@link #setServiceDescriptor(ServiceDescriptor)}, or by overriding * a protected methods like{@link #setEnteringMDCs}.
* *For everything else: *
If you call this variant, then YOU are assuming responsibility for * setting the requisite ONAP headers.
* * @param sync whether synchronous. * @return invocation ID to be passed with invocation. */ public UUID invoke(final ONAPLogConstants.InvocationMode sync) { final UUID invocationID = UUID.randomUUID(); // Derive SYNC/ASYNC marker. final Marker marker = (sync == null) ? ONAPLogConstants.Markers.INVOKE : sync.getMarker(); // Log INVOKE*, with the invocationID as the message body. // (We didn't really want this kind of behavior in the standard, // but is it worse than new, single-message MDC?) this.mLogger.info(marker, "{}", invocationID); return invocationID; } /** * Report pending invocation with INVOKE marker, * setting standard ONAP logging headers automatically. * * @param builder request builder, for setting headers. * @param sync whether synchronous, nullable. * @return invocation ID to be passed with invocation. */ public UUID invoke(final RequestBuilder builder, final ONAPLogConstants.InvocationMode sync) { // Sync can be defaulted. Builder cannot. checkNotNull(builder); // Log INVOKE, and retain invocation ID for header + return. final UUID invocationID = this.invoke(sync); // Set standard HTTP headers on (southbound request) builder. builder.setHeader(ONAPLogConstants.Headers.REQUEST_ID, defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID))); builder.setHeader(ONAPLogConstants.Headers.INVOCATION_ID, defaultToEmpty(invocationID)); builder.setHeader(ONAPLogConstants.Headers.PARTNER_NAME, defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME))); return invocationID; } /** * Report vanilla INVOKE marker. * * @param builder builder for downstream requests, if you want the * standard ONAP headers to be added automatically. * @return invocation ID to be passed with invocation. */ public UUID invoke(final RequestBuilder builder) { return this.invoke(builder, (ONAPLogConstants.InvocationMode)null); } /** * Get descriptor, for overriding service details. * @return non-null descriptor. */ public ServiceDescriptor getServiceDescriptor() { return checkNotNull(this.mServiceDescriptor); } /** * Override {@link ServiceDescriptor}. * @param d non-null override. * @return this. */ public ONAPLogAdapter setServiceDescriptor(final ServiceDescriptor d) { this.mServiceDescriptor = checkNotNull(d); return this; } /** * Get descriptor, for setting response details. * @return non-null descriptor. */ public ResponseDescriptor getResponseDescriptor() { return checkNotNull(this.mResponseDescriptor); } /** * Override {@link ResponseDescriptor}. * @param d non-null override. * @return this. */ public ONAPLogAdapter setResponseDescriptor(final ResponseDescriptor d) { this.mResponseDescriptor = checkNotNull(d); return this; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Protected methods. // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * Set MDCs that persist for the duration of an invocation. * *It would be better to roll this into {@link #entering}, like * with {@link #exiting}. Then it would be easier to do, but it * would mean more work.
* * @param request incoming HTTP request. * @return this. */ protected ONAPLogAdapter setEnteringMDCs(final RequestAdapter> request) { // Extract MDC values from standard HTTP headers. final String requestID = defaultToUUID(request.getHeader(ONAPLogConstants.Headers.REQUEST_ID)); final String invocationID = defaultToUUID(request.getHeader(ONAPLogConstants.Headers.INVOCATION_ID)); final String partnerName = defaultToEmpty(request.getHeader(ONAPLogConstants.Headers.PARTNER_NAME)); // Set standard MDCs. Override this entire method if you want to set // others, OR set them BEFORE or AFTER the invocation of #entering, // depending on where you need them to appear, OR extend the // ServiceDescriptor to add them. MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC) .format(DateTimeFormatter.ISO_INSTANT)); MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestID); MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationID); MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName); MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, defaultToEmpty(request.getClientAddress())); MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, defaultToEmpty(request.getServerAddress())); // Delegate to the service adapter, for service-related DMCs. this.mServiceDescriptor.setMDCs(); // Default the service name to the requestURI, in the event that // no value has been provided. if (MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME) == null || MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME).equalsIgnoreCase(EMPTY_MESSAGE)) { MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI()); } return this; } /** * Dependency-free nullcheck. * * @param in to be checked. * @paramIn most cases extension isn't required.
*/ public static class ServiceDescriptor { /** ServiceName. */ protected String mName; /** InstanceUUID. */ protected String mUUID = sInstanceUUID.toString(); /** * Set name. * @param name ServiceName. * @return this. */ public ServiceDescriptor setServiceName(final String name) { this.mName = name; return this; } /** * Set name. * @param uuid InstanceUUID. * @return this. */ public ServiceDescriptor setServiceUUID(final String uuid) { this.mUUID = uuid; return this; } /** * Set MDCs. Once set they remain set until everything is cleared. */ protected void setMDCs() { MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, defaultToEmpty(this.mName)); MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, defaultToEmpty(this.mUUID)); } } /** * Response is different in that response MDCs are normally only * reported once, for a single log message. (But there's no method * for clearing them, because this is only expected to be called * during #exiting.) */ public static class ResponseDescriptor { /** Response errorcode. */ protected String mCode; /** Response description. */ protected String mDescription; /** Response severity. */ protected Level mSeverity; /** Response status, of {COMPLETED, ERROR}. */ protected ONAPLogConstants.ResponseStatus mStatus; /** * Setter. * * @param code response (error) code. * @return this. */ public ResponseDescriptor setResponseCode(final String code) { this.mCode = code; return this; } /** * Setter. * * @param description response description. * @return this. */ public ResponseDescriptor setResponseDescription(final String description) { this.mDescription = description; return this; } /** * Setter. * * @param severity response outcome severity. * @return this. */ public ResponseDescriptor setResponseSeverity(final Level severity) { this.mSeverity = severity; return this; } /** * Setter. * * @param status response overall status. * @return this. */ public ResponseDescriptor setResponseStatus(final ONAPLogConstants.ResponseStatus status) { this.mStatus = status; return this; } /** * Overrideable method to set MDCs based on property values. */ protected void setMDCs() { MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, defaultToEmpty(this.mCode)); MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, defaultToEmpty(this.mDescription)); MDC.put(ONAPLogConstants.MDCs.RESPONSE_SEVERITY, defaultToEmpty(this.mSeverity)); MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, defaultToEmpty(this.mStatus)); } } /** * Adapter for reading information from an incoming HTTP request. * *Incoming is generally easy, because in most cases you'll be able to * get your hands on the HttpServletRequest.
* *Perhaps should be generalized to refer to constants instead of * requiring the implementation of specific methods.
* * @paramNo default implementation, because there's no HTTP client that's * sufficiently ubiquitous to warrant incurring a mandatory dependency.
* * @param