Centrilogic

Centrilogic is a global provider of IT transformation solutions that empower organizations to…

Follow publication

Adding colour to Azure Pipeline logs

First things first, I am Canadian and this is how we spell colour.

If you are anything like me you like/need some way to visually parse logs when you are reading them.

When I write scripts I’m a fan of using colour to make it easier to read output. For example here is my Reset Iterations script I posted way back.

$projectName = "Parts Unlimited"
$Sprints = "\Parts Unlimited\Iteration\Sprints\Sprint"
$lastMonday =
(Get-Date).AddDays((-1*(Get-Date).DayOfWeek.Value)+1).Date
write-host "Last Monday - $lastMonday" -ForegroundColor Cyan
$startDate = $lastMonday.AddDays(-14)
$finishDate = $startDate.AddDays(11)
For ($i=1; $i -le 8; $i++) {
write-host "Sprint $i" -foreground Yellow
write-host "Start - $startDate" -foreground Green
write-host "Finish - $finishDate" -foreground Green

$out = az boards iteration project update --path "$Sprints $i"
--org "$org" --project "$projectName"
--start-date "$startDate" --finish-date "$finishDate"
$startDate = $startDate.AddDays(14)
$finishDate = $startDate.AddDays(11)
}

Here I’m using the -foreground option on write-host to show colour in the console, which results in this.

Which is so much easier to read than the default.

If you work with Azure DevOps a lot you have noticed that -foreground is ignored in the Azure Pipelines consol.

In the past I have used dash’s, equals or comments to separate log messages in the console from my pipeline scripts.

For ($i=1; $i -le 8; $i++) {
write-host “###########################”
write-host "Sprint $i"
write-host "Start - $startDate"
write-host "Finish - $finishDate"
$out = az boards iteration project update --path "$Sprints $i"
--org "$org" --project "$projectName"
--start-date "$startDate" --finish-date "$finishDate"
$startDate = $startDate.AddDays(14)
$finishDate = $startDate.AddDays(11)
}

Which would result in something like this.

###########################
Sprint 1
Start - 07/11/2022 00:00:00
Finish - 07/22/2022 00:00:00
###########################
Sprint 2
Start - 07/25/2022 00:00:00
Finish - 08/05/2022 00:00:00
###########################
Sprint 3
Start - 08/08/2022 00:00:00
Finish - 08/19/2022 00:00:00

It’s ok, it does make it easier to parse out each sprint and it’s dates, however the coloured output is better.

Then I learned how to include ‘colour’ and ‘collapsing groups’ in Azure Pipeline Scripts using these formatting commands.

##[group]Beginning of a group
##[warning]Warning message
##[error]Error message
##[section]Start of a section
##[debug]Debug text
##[command]Command-line being run
##[endgroup]
Which result in this

When I change my script to use these commands…

$lastMonday = 
(Get-Date).AddDays((-1 * (Get-Date).DayOfWeek.Value__) + 1).Date
write-host "##[section] Reset Sprint Dates for $projectName"
write-host "##[section] Last Monday - $lastMonday"
$startDate = $lastMonday.AddDays(-14)
$finishDate = $startDate.AddDays(11)
For ($i=1; $i -le 8; $i++) {
write-host "##[group] Sprint $i"
write-host "##[command] Start - $startDate"
write-host "##[command] Finish - $finishDate"

$out = az boards iteration project update --path "$Sprints $i"
--org "$org" --project "$projectName"
--start-date "$startDate" --finish-date "$finishDate"
$startDate = $startDate.AddDays(14)
$finishDate = $startDate.AddDays(11)
write-host "##[endgroup]"
}

…I get this. I love the collapsible groups.

And if I have a Warning, Error or Debug log message. I can use these commands to make them stand out differently from the rest of the logs.

##[warning]Warning message
##[error]Error message
##[debug]Debug text

Here is the link if you want to read the official documentation on formatting commands.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Centrilogic
Centrilogic

Published in Centrilogic

Centrilogic is a global provider of IT transformation solutions that empower organizations to realize their full digital potential. Armed with capabilities that span the stack, we build and manage end-to-end digital solutions that help companies reshape the role of their technolo

Dave Lloyd
Dave Lloyd

Written by Dave Lloyd

I have been writing software and teaching/coaching developers for 40 years. I love sharing knowledge and experience.

Responses (1)

Write a response