Using Integer value in MDC object

Viewed 56

I was using MDC for logging in my spring boot application. Along with that I was also maintaining a graph. I was fetching the values from MDC to draw the graph. It requires Integer values not the string values. I don't want to make use of String values and then type caste it explicitly after fetching the values for the graph reason being the code may break during type casting as it may contain null values (even though there would be no such scenario as I have kept the default value as "0" but still I want to avoid this).

If I try MDC.put("Speed", "0"); It works fine. But it shows error for MDC.put("Speed", 0);

Please tell how can I put an integer value in the MDC object.

1 Answers

MDC only supports string values as keys and values. What you can do instead, to avoid type casting errors for null values, is to use Integer class instead of the primitive int.

Related