2

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

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

4

1 回答 1

0

你可以试试这个.. 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>
于 2017-02-03T12:50:39.247 回答