Global function to print the response body

hello, I don’t know if I have to post it here, but I couldn’t find an answer and a way to do this.
I want to create a certain tests for api’s response body and check if it contains a certain value or property. The problem is that I want to create a global function in the collection and in each request to console log the whole response body ONLY if the test fails. I can’t find a proper way to do this and will appreciate if someone helps with this.