The basis of the configuration is the default Spring Boot configuration. I just adapted as I needed. The two important changes are:
- Added [%mdc] to the CONSOLE_LOG_PATTERN and the FILE_LOG_PATTERN
- The log file is only written if the LOG_FILE variable (system variable or environment variable) is set. With the system variable it's possible to have several microservices running on the same machine, which will write the output into the specified log file.
For example if you call http://localhost:9090/shopping-cart/3 following output will be generated distributed over 3 processes:
# frontend-service
2015-08-09 21:46:03.337 INFO 10868 --- [nio-9090-exec-2] a.r.c.m.f.c.ShoppingCartController : [caller=/shopping-cart/3 <- 127.0.0.1, uuid=23badb29-6cb3-4f02-a786-2c15cbcaf302] getShoppingCart(3)
# shopping-cart-service
2015-08-09 21:46:03.340 INFO 17732 --- [o-auto-1-exec-1] a.r.c.m.user.rest.ShoppingCartService : [caller=frontend-service <- /shopping-cart/3 <- 127.0.0.1, uuid=23badb29-6cb3-4f02-a786-2c15cbcaf302] getShoppingCart(3)
# product-service
2015-08-09 21:46:03.360 INFO 20148 --- [o-auto-1-exec-4] a.r.c.m.product.rest.ProductService : [caller=frontend-service <- /shopping-cart/3 <- 127.0.0.1, uuid=23badb29-6cb3-4f02-a786-2c15cbcaf302] getproduct(3)
2015-08-09 21:46:03.337 INFO 10868 --- [nio-9090-exec-2] a.r.c.m.f.c.ShoppingCartController : [caller=/shopping-cart/3 <- 127.0.0.1, uuid=23badb29-6cb3-4f02-a786-2c15cbcaf302] getShoppingCart(3)
# shopping-cart-service
2015-08-09 21:46:03.340 INFO 17732 --- [o-auto-1-exec-1] a.r.c.m.user.rest.ShoppingCartService : [caller=frontend-service <- /shopping-cart/3 <- 127.0.0.1, uuid=23badb29-6cb3-4f02-a786-2c15cbcaf302] getShoppingCart(3)
# product-service
2015-08-09 21:46:03.360 INFO 20148 --- [o-auto-1-exec-4] a.r.c.m.product.rest.ProductService : [caller=frontend-service <- /shopping-cart/3 <- 127.0.0.1, uuid=23badb29-6cb3-4f02-a786-2c15cbcaf302] getproduct(3)
This output makes two things very easy:
- Find all log output which belongs together. Just find all log entries with the same uuid.
- See the flow of the initial request through the microservices.
Last but not least it's nice that you don't have to copy the logback.xml file into each service. With Maven it's possible to pack the logback.xml file into an artifact and then unpack it into the microservices. If you change the log configuration you just need to change the file once. Depending on your setup it might be required to change the version of the artifact so that the new version is unpacked.