Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log validation errors in Spring boot application

I'm using SpringBoot (spring-boot-starter-web 1.5.16.RELEASE) and hibernate-validator 6.0.11.Final in my REST service.

DTO objects annotated with proper validation constrains (UPDATE: I'm using @Valid annotation on controller's parameter) and validation works as expected.

I'd like to have all validation errors (that were send to the clients) logged in log files.

I assume there should be some simple way to enable that kind of messages (e.g. flag in app properties) but I can't find it anywhere.

Any suggestions would be appreciated.

like image 930
igor Avatar asked Oct 16 '18 17:10

igor


1 Answers

It turned out that org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver does log the validation errors by default (and that is what I exactly need), e.g.

2018-10-18 12:54:25.552  WARN 22760 --- [nio-8092-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for 

The reason why I haven't seen these messages was in log aggregation tool filters (i.e. I haven't seen messages in Kibana but they appear on local machine).

For those who will not see messages locally - check logging configuration and make sure that Spring logs are not muted there.

like image 128
igor Avatar answered Nov 15 '22 07:11

igor