I'm using a small Groovy application with Spring Social to consume a stream from Twitter. I now want to log every Tweet I receive, including some detail.
If the Tweet
class would be inside my own code, i can easily do this to get a nice log output:
import groovy.transform.ToString
@ToString
class Tweet {
// fields...
}
But since the class I want to log is inside Spring Social, I can't do that. Is there a "Groovy" way of overriding the toString
method?
Adding a toString
Closure to the metaClass
does not really help, since I don't want to call toString
explicitly at every log output.
org.springframework.social.twitter.api.Tweet.metaClass.toString = {
"${delegate.field1}, ${delegate.field2}"
}
// still prints "received tweet [org.springframework.social.twitter.api.Tweet@637c1991]"
log.info("received tweet [{}]", tweet)
// prints "received tweet [field1 field]"
log.info("received tweet [{}]", tweet.toString())