我正在尝试使用 slfj4 MDC 类在日志中打印已登录的用户名及其 ID。这是我的代码,请告诉我我错在哪里。
JAVA 文件 -
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public classResponse getuser(@PathVariable("user_id") Integer user_id, @RequestParam(value = "department_id", required = false) Integer department_id) {
JSONObject response = null;
String username = getUserName();
logger.debug("Testing MDC.");
MDC.put("FirstName", "First");
MDC.put("LastName", "Last");
logger.info(":::ServiceContoller.getuser starts for userid" + user_id);
logger.info(":::ServiceContoller.getCreateEnvironment:Ends for username" + username);
try {
response = classServiceImpl.getUser1(username, user_id, department_id);
logger.info("Sending Environment details - {}", response);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ClassResponse(Constants.STR_ERROR, "Error getting USERDetails");
}
return new ClassResponse(Constants.STR_SUCCESS, response);
}
XML 属性文件 - logback.xml -
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<Pattern>%-4r [%thread] %-5level C:%X{FirstName} N:%X{LastName} - %m%n</Pattern>
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
请告诉我我还需要什么或我错在哪里。任何帮助表示赞赏。先感谢您。