Last active
June 26, 2020 10:49
-
-
Save grenade/7c328849be8a2bf2a7c28c05d2226920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
retries: 0 | |
created: '2020-06-25T11:39:24.518Z' | |
deadline: '2020-06-28T11:59:03.691Z' | |
expires: '2021-06-29T11:59:03.691Z' | |
provisionerId: relops | |
workerType: win2019 | |
priority: highest | |
tags: {} | |
scopes: [] | |
payload: | |
command: | |
- >- | |
git clone https://gist.github.com/7c328849be8a2bf2a7c28c05d2226920.git | |
.\test | |
- powershell -File .\test\colored-log-test.ps1 | |
maxRunTime: 60 | |
extra: {} | |
metadata: | |
name: test logging in ansi color | |
description: just some debugging | |
owner: [email protected] | |
source: 'https://gist.github.com/7c328849be8a2bf2a7c28c05d2226920' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[console]::OutputEncoding = [Text.Encoding]::Utf8; | |
function Coalesce { | |
param ( | |
[string] $value, | |
[string] $fallback | |
) | |
process { | |
if (($value -ne $null) -or (($value -is [string]) -and (-not ([string]::IsNullOrWhitespace($value))))) { | |
return $value; | |
} else { | |
return $fallback; | |
} | |
} | |
} | |
#New-Alias -Name '??' -Value 'Coalesce' -Option ReadOnly -Force | |
#(Coalesce -value $([IO.Path]::GetFileNameWithoutExtension($MyInvocation.PSCommandPath)) -fallback ) | |
function Write-Log { | |
param ( | |
[string] $script = 'build-machine-image', | |
[string] $source, | |
[string] $message, | |
[object[]] $tokens = $null, | |
[object] $exception = $null, | |
[ValidateSet('trace', 'debug', 'info', 'warn', 'error')] | |
[string] $severity = 'info', | |
[hashtable] $colors = @{ | |
'trace' = 'Blue'; | |
'debug' = 'Gray'; | |
'info' = 'White'; | |
'warn' = 'Yellow'; | |
'error' = 'Red'; | |
'default' = 'White'; | |
'exception' = 'Yellow' | |
} | |
) | |
Write-Host -NoNewLine -ForegroundColor $colors.trace -Object '['; | |
Write-Host -NoNewLine -ForegroundColor $colors.trace -Object ('{0} {1} ' -f $script, (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.fffZ')); | |
Write-Host -NoNewLine -ForegroundColor $colors[$severity] -Object $severity; | |
Write-Host -NoNewLine -ForegroundColor $colors.trace -Object ']'; | |
Write-Host -NoNewLine -ForegroundColor $colors.default -Object (' {0} :: ' -f $source); | |
if (($tokens) -and ($tokens.Length)) { | |
if (($tokens | % { ($_ -is [hashtable]) -and ($_.ContainsKey('value')) -and ($_.ContainsKey('severity')) }) -contains $false) { | |
Write-Host -ForegroundColor $colors.default -Object ($message -f $tokens); | |
} else { | |
$segments = $message.Split(@('{}'), [System.StringSplitOptions]::None); | |
if (($segments.Length -eq $tokens.Length) -or (($segments.Length - 1) -eq $tokens.Length)) { | |
for ($i = 0; $i -lt $tokens.Length; $i++) { | |
Write-Host -NoNewLine -ForegroundColor $colors.default -Object $segments[$i]; | |
Write-Host -NoNewLine -ForegroundColor $colors[$tokens[$i].severity] -Object $tokens[$i].value; | |
} | |
if ($segments.Length -gt $tokens.Length) { | |
Write-Host -NoNewLine -ForegroundColor $colors.default -Object $segments[($segments.Length - 1)]; | |
} | |
} else { | |
Write-Host -NoNewLine -ForegroundColor $colors.error -Object ('tokens: {0}, segments: {1}' -f $tokens.Length, $segments.Length); | |
} | |
} | |
} else { | |
Write-Host -ForegroundColor $colors.default -Object $message; | |
} | |
if (($exception) -and ($exception.Message)) { | |
Write-Host -ForegroundColor $colors.exception -Object $exception.Message; | |
} | |
} | |
Write-Log -source 'my-bland-method' -message 'a line without string format token replacement' -severity 'info' | Out-Host; | |
Write-Log -source 'my-bland-method' -message 'a line without string format token replacement' -severity 'info' | Out-Host; | |
Write-Log -source 'my-bland-method' -message 'a line without string format token replacement' -severity 'info' | Out-Host; | |
Write-Log -source 'my-complex-method' -message 'a line with normal string format token replacement - trace: {0}, debug: {1}, info: {2}, warn: {3}, error: {4}, default: {5}' -tokens @('blue', 'gray', 'white', 'yellow', 'red', 'white') -severity 'info' | Out-Host; | |
Write-Log -source 'my-colourful-method' -message 'a line with coloured string format token replacement - trace: {}, debug: {}, info: {}, warn: {}, error: {}, default: {}' -tokens @(@{'value' = 'blue'; 'severity' = 'trace'}, @{'value' = 'gray'; 'severity' = 'debug'}, @{'value' = 'white'; 'severity' = 'info'}, @{'value' = 'yellow'; 'severity' = 'warn'}, @{'value' = 'red'; 'severity' = 'error'}, @{'value' = 'white'; 'severity' = 'default'}) -severity 'info' | Out-Host; | |
Write-Log -source 'my-ansi-method' -message 'a line with coloured string format token replacement - trace: {}, debug: {}, info: {}, warn: {}, error: {}, default: {}' -tokens @(@{'value' = '\u001b[34mblue\u001b[0m'; 'severity' = 'trace'}, @{'value' = 'gray\u001b[0m'; 'severity' = 'debug'}, @{'value' = '\u001b[37mwhite\u001b[0m'; 'severity' = 'info'}, @{'value' = '\u001b[33myellow\u001b[0m'; 'severity' = 'warn'}, @{'value' = '\u001b[31mred\u001b[0m'; 'severity' = 'error'}, @{'value' = '\u001b[37mwhite\u001b[0m'; 'severity' = 'default'}) -severity 'info' | Out-Host; | |
Write-Log -source 'my-bland-method' -message 'a line without string format token replacement' -severity 'info' | Out-Host; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment