Have you ever wanted to emphasize some console log output with colors? Well, I thought I never would have needed to, but while tinkering with some side project, I found a trick to colorize my console log output. The magic trick is using ANSI Escape Codes. My use-case was to highlight some part of my Groovy task’s output as a yellow colored warning and some errors as red, while working with my Android project. The code is quite simple, so lets dive into its implementation right away!
First, create a colored_console.gradle
file at the root of your project directory and populate it with content shown below. You can copy past it as it is:
|
|
Next, include this file inside the build.gradle
file where you want to want to use the functions that facilitate colored console output. i.e app module’s build.gradle file.
Include the below line of code at the top of build.gradle file where plugins are defined.
|
|
The setup part is now done and you can start using the defined functions to enable colored console output.
In order to test this out, lets create a task named coloredHelloWorld
, as shown below:
|
|
Now we should execute this task by running the below command in a terminal window at the root of the project:
|
|
and its output:
Pretty neat ๐ Now go use it in your projects and add a splash of color to your console logs ๐
You can find all code in a functional Android project here.