My problem:
I am currently running gradle in TeamCity and I would like to output special messages each time an error or warning is logged through gradle’s logging system. For example,
If logger.error() is used by any task, then a specially formatted message should be output.
If logger.warn() is used by any task, then a different specially formatted message should be output.
I can’t seem to find a way to intercept all log messages of a specific level in gradle.
Additionally, I can’t seem to find a way to include the log level in the actual output of the message, because that would also solve this problem since I could simply add a standard output listener that parses each message for the log level, and does something interesting with that information.
Any advice on how I could solve this problem or use any of the ideas above?
Unfortunately, the logging backend in Gradle is not really nicely configurable.
I think the only way (besides below one) to get the log level in the output is to enable debug logging.
To do “intercept all log messages”, you probably would have to provide your own logging backend that then handles all of the logging yourself using gradle.useLogger(...).
Using debug should work fine, I should be able to parse the messages for the log level that way.
On the gradle.useLogger(...) - I was experimenting around with this, and I had success getting it to output messages before tasks execute, after tasks execute, etc. However I was unable to get it to intercept ALL messages from the standard output.
I tried to create a custom logger class that implemented the StandardOutputListener interface (which I was able to successfully use as a listener rather than a custom logger), but when I try to use it as a custom logger, it does not seem to have any effect? Gradle just logs as it normally would.
For reference, here is code along the lines of what I tried:
useLogger(new CustomLogger())
public class CustomLogger implements StandardOutputListener {
void onOutput(CharSequence output) {
System.out.println output
System.out.println output
}
}
With the above, I expected each message from the standard output to be printed twice, but rather the behavior I saw was each message being printed out once (with rich color text) as if the logger wasn’t even being used.