Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Last active November 7, 2019 21:28
Show Gist options
  • Save wreulicke/8d3750f3226d34603eadddec9b1703e5 to your computer and use it in GitHub Desktop.
Save wreulicke/8d3750f3226d34603eadddec9b1703e5 to your computer and use it in GitHub Desktop.
運用を支えるためのログを出すにはどうするかのソース
{
"timestamp": "2018-07-25T09:50:04.637+09:00",
"level": "INFO",
"thread": "pool-2-thread-3",
"requestId": "dbc64d80-163a-47b8-8b6b-da42b92c2dba", // トランザクションに紐づくID(後述)
"req.requestURI": "/buy", // リクエストされたURI
// 他にもあるけど省略
"logger": "com.github.wreulicke.PaymentService",
"message": "決済が完了しました"
}
class PaymentService {
public void purchase(Product product) {
// なんかいい感じの処理がある
log.info("決済が完了しました");
}
}
public class RequestIdFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain)
throws java.io.IOException, ServletException {
MDC.put("RequestId", UUID.randomUUID().toString());
try {
chain.doFilter(request, response);
} finally {
MDC.remove("RequestId");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment