我为 Http 请求编写了自己的日志记录类并将其用于@ControllerAdvice
public class HttpLogger {
public static void logging(Object handlerClass,
HttpServletRequest req,
HttpServletResponse resp,
Exception e) {
Log.logger.info("{} catch and try to resolve exception;" +
"\nException message: {};" +
"\nException class: {};" +
"\nTime creating exception: {};", handlerClass.getClass(), e.getMessage(), e.getClass(), ZonedDateTime.now(ZoneId.of("Z")));
Log.logger.info("HTTP Method - {}" +
"\nHTTP Status - {}" +
"\nRequestURI {}", req.getMethod(), resp.getStatus(), req.getRequestURI());
}
}
我不使用 SpringBoot,但我想做以下事情:在application.properties添加
httpLogger.enabled=true. 当值为真时 - 发生日志记录,false- 分别为否。
一个想法浮现在脑海:
- 扫描
application.properties - 找到您的设置
httpLogger.enabled - 取决于要记录的值。
该方法将具有 +- 像这样
if(enabled){
//логировать
} else {
//ничего не делать
}
实际上是问题本身:我知道这将正常工作,但也许有一些更优雅的解决方案?我不想重新发明轮子。
我也考虑过 BeanPostProcessor,通过 MBean 控制一切。
我不知道这是否是最好的选择,但我这样做了: