You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/07/07 04:47:37 UTC

[logging-log4j2] branch master updated: Include thread context data

This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new d39f1eb  Include thread context data
d39f1eb is described below

commit d39f1eb3d11fb75af38e73c849502ccd5f2d0b55
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Jul 6 21:47:18 2020 -0700

    Include thread context data
---
 .../spring/cloud/config/sample/controller/SampleController.java   | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/log4j-spring-cloud-config/log4j-spring-cloud-config-samples/log4j-spring-cloud-config-sample-application/src/main/java/org/apache/logging/log4j/spring/cloud/config/sample/controller/SampleController.java b/log4j-spring-cloud-config/log4j-spring-cloud-config-samples/log4j-spring-cloud-config-sample-application/src/main/java/org/apache/logging/log4j/spring/cloud/config/sample/controller/SampleController.java
index 3d47218..7657503 100644
--- a/log4j-spring-cloud-config/log4j-spring-cloud-config-samples/log4j-spring-cloud-config-sample-application/src/main/java/org/apache/logging/log4j/spring/cloud/config/sample/controller/SampleController.java
+++ b/log4j-spring-cloud-config/log4j-spring-cloud-config-samples/log4j-spring-cloud-config-sample-application/src/main/java/org/apache/logging/log4j/spring/cloud/config/sample/controller/SampleController.java
@@ -25,6 +25,8 @@ import java.util.concurrent.TimeUnit;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.util.UuidUtil;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
@@ -52,6 +54,7 @@ public class SampleController {
         }
         String msg = "";
         if (threads == 1) {
+            ThreadContext.put("requestId", UuidUtil.getTimeBasedUuid().toString());
             Timer timer = new Timer("sample");
             timer.start();
             for (int n = 0; n < count; ++n) {
@@ -61,6 +64,7 @@ public class SampleController {
             StringBuilder sb = new StringBuilder("Elapsed time = ");
             timer.formatTo(sb);
             msg = sb.toString();
+            ThreadContext.clearMap();
         } else {
             ExecutorService service = Executors.newFixedThreadPool(threads);
             Timer timer = new Timer("sample");
@@ -85,6 +89,7 @@ public class SampleController {
 
     @GetMapping("/exception")
     public ResponseEntity<String> exception() {
+        ThreadContext.put("requestId", UuidUtil.getTimeBasedUuid().toString());
         Throwable t = new Throwable("This is a test");
         LOGGER.info("This is a test", t);
         ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -92,6 +97,7 @@ public class SampleController {
         t.printStackTrace(ps);
         String stackTrace = os.toString();
         stackTrace = stackTrace.replaceAll("\n", "<br>");
+        ThreadContext.clearMap();
 
         //LOGGER.info("Hello, World");
         return ResponseEntity.ok(stackTrace);
@@ -109,9 +115,11 @@ public class SampleController {
 
         public void run() {
             String prefix = "Thread " + id + " record ";
+            ThreadContext.put("requestId", UuidUtil.getTimeBasedUuid().toString());
             for (int i = 0; i < count; ++i) {
                 LOGGER.info(prefix + i);
             }
+            ThreadContext.clearMap();
         }
     }
 }