Java slf4j Logger setRequestId(String requestId)

Here you can find the source of setRequestId(String requestId)

Description

Set the request ID in the logging MDC.

License

Open Source License

Declaration

public static void setRequestId(String requestId) 

Method Source Code


//package com.java2s;
/*//  ww w . j a v  a2 s  .co  m
 * Copyright 2015 VMware, Inc. 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.
 */

import org.slf4j.MDC;

public class Main {
    public static final String REQUEST_ID_KEY = "requestId";
    public static final String REQUEST_KEY = "request";

    /**
     * Set the request ID in the logging MDC. Normally you should not
     * need to do this.
     */
    public static void setRequestId(String requestId) {
        MDC.put(REQUEST_KEY, formatRequestIdLogSection(requestId));
        MDC.put(REQUEST_ID_KEY, requestId);
    }

    /**
     * Create a string that can be used as a prefix in a log message that has
     * the request ID embedded. This is used by the Xenon logging method in
     * ServiceUtils. It does not use the MDC: the request ID must be passed
     * as an argument.
     */
    public static String formatRequestIdLogSection(String requestId) {
        return String.format(" [Req: %s]", requestId);
    }
}

Related

  1. setClusterShard(String cluster, String shard)
  2. setContext(String value)
  3. setDefaultLogger()
  4. setLogger(Logger logger)
  5. setOutputFile(String filename)
  6. sleep(int milliseconds)
  7. sleepMs(Logger logger, long timeMs)
  8. stacktrace(Logger log, Throwable t)
  9. startTestLogging(String name)