gpt4 book ai didi

java - slf4j 日志记录 - MDC.put 函数不打印任何内容

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:08 31 4
gpt4 key购买 nike

我正在尝试使用 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>

请告诉我我还需要什么或者我错在哪里。任何帮助表示赞赏。预先感谢您。

最佳答案

你能试试这个吗.. https://logback.qos.ch/manual/appenders.html

<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>

关于java - slf4j 日志记录 - MDC.put 函数不打印任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42024642/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com