【问题标题】:slfj4 logging - MDC.put function not printing anythingslf4j 日志记录 - MDC.put 函数不打印任何内容
【发布时间】:2017-06-20 20:21:22
【问题描述】:

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

请告诉我我还需要什么或者我哪里错了。任何帮助表示赞赏。提前谢谢你。

【问题讨论】:

    标签: java logging log4j slf4j mdc


    【解决方案1】:

    你能试试这个吗..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>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2012-03-11
    相关资源
    最近更新 更多