This page looks best with JavaScript enabled

Rainbow console logs in Groovy

 ·  โ˜• 3 min read  ·  โœ๏ธ Nishant
Rainbow console logs in Groovy

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Colored Console logs in Groovy
def printlnWithColor(color, str) {
    // Map of ANSI Escape color codes
    final colorsMap = [black  : 30, red: 31, green: 32, yellow: 33,
                       blue   : 34, magenta: 35, cyan: 36, white: 37,
                       default: 39]
    // Create the escape sequence so string is color formatted                   
    final style = "${(char) 27}[${colorsMap[color]}" + "m"
    // Apply the escape sequence to the string by prepending it
    final styledStr = style + str
    // finally use println to print this escaped string
    println(styledStr)
}

// Using ext.method_name so it is exported and available 
// to be used outside the file it is declared in
ext.printlnBlack = { str -> printlnWithColor("black", str) }

ext.printlnRed = { str -> printlnWithColor("red", str) }

ext.printlnGreen = { str -> printlnWithColor("green", str) }

ext.printlnYellow = { str -> printlnWithColor("yellow", str) }

ext.printlnBlue = { str -> printlnWithColor("blue", str) }

ext.printlnCyan = { str -> printlnWithColor("magenta", str) }

ext.printlnWhite = { str -> printlnWithColor("white", str) }

ext.printlnDefault = { str -> printlnWithColor("default", str) }

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.

1
apply from: "$rootProject.projectDir/colored_console.gradle"

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:

1
2
3
4
5
task coloredHelloWorld {
    printlnGreen("============")
    printlnBlue("Hello World!")
    printlnRed("============")
}

Now we should execute this task by running the below command in a terminal window at the root of the project:

1
./gradlew coloredHelloWorld

and its output:

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.

meme

Share on
Support the author with

Nishant Srivastava
WRITTEN BY
Nishant
๐Ÿ‘จโ€๐Ÿ’ป Android Engineer/๐Ÿงข Opensource enthusiast

What's on this Page