I want to embed any TraceId in the log

Summary of this article

――In batch processing, I want to quickly search where the same processing log is from when parallel processing or continuous processing is performed. -Write on the assumption that @ Slf4j is used

Immediate mounting method

--In Java class, register the information you want to output to the log using MDC (MDC.put) --At the end, remove the key with remove --You can get the key registered in MDC in logback.xml in the format of % X {<key>}.

logback.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>traceID: [%X{traceId}] message: %msg%n</pattern>
        </encoder>
    </appender>

    <root>
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

@Slf4j
public class Traceid {
    public static void main(String[] args) {
        log.info("dummy1");

        String uuid = UUID.randomUUID().toString();
        MDC.put("traceId", uuid.substring(0, uuid.indexOf("-")));
        log.info("dummy2");
        MDC.remove("traceId");

        log.info("dummy3");
    }
}

Console log


traceID: [] message: dummy1
traceID: [4f9c9243] message: dummy2
traceID: [] message: dummy3

Other

Since MDC.put is a static method, why not use the same value when using it in multiple threads? → Since the Map where MDC stores the key is stored in ThreadLocal, there is no need to worry about using the same value between threads!

Recommended Posts

I want to embed any TraceId in the log
I want to get the value in Ruby
I want to simplify the log output on Android
I want to set the conditions to be displayed in collection_check_boxes
I want to change the log output settings of UtilLoggingJdbcLogger
I want to transition to the same screen in the saved state
I want to simplify the conditional if-else statement in Java
I want to morphologically analyze the log in the DB and put it in the DB to classify messages 1
I want to place RadioButtons in the same RadioGroup at any position on the screen.
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
I want to use @Autowired in Servlet
I want to change the value of Attribute in Selenium of Ruby
[Android] I want to get the listener from the button in ListView
[Ruby] I want to output only the odd-numbered characters in the character string
I want to write JSP in Emacs more easily than the default.
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
I want to output the day of the week
I want to send an email in Java.
I tried to organize the session in Rails
I want to use arrow notation in Ruby
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to truncate after the decimal point
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I tried to organize the cases used in programming
[Java] I want to calculate the difference from the date
Tokoro I rewrote in the migration from Wicket 7 to 8
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to judge the range using the monthly degree
I want to use a little icon in Rails
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
I want to define a function in Rails Console
I want to stop snake case in table definition
I want to call the main method using reflection
If you want to recreate the instance in cloud9
I want to click a GoogleMap pin in RSpec
[Rough commentary] I want to marry the pluck method
I tried to implement the Euclidean algorithm in Java
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to add a delete function to the comment function
I want to see only the latest because the Docker log has become too large
I want to change the path after new registration after logging in with multiple devises.
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
[Rails] I want to display the link destination of link_to in a separate tab
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to convert characters ...
Ubuntu unable to log in
[Beginner] I want to modify the migration file-How to use rollback-
I want to find a relative path in a situation using Path