Private/Log/Write-LogToStdOut.ps1

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function Write-LogToStdOut() {
    <#
    .SYNOPSIS
        Outputs the Message to stdout using colors.
    
    .EXAMPLE
        Write-LogToStdOut -Header "Header" -Message "Message" -Severity $Severity
    #>


    [CmdletBinding()]
    [OutputType([void])]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')]
    param(
        [Parameter(Mandatory=$false)]
        [string] 
        $Header, 
        
        [Parameter(Mandatory=$false)]
        [string[]] 
        $Message, 
        
        [Parameter(Mandatory=$false)]
        [int] 
        $Severity, 
        
        [Parameter(Mandatory=$false)]
        [switch] 
        $Emphasize,

        [Parameter(Mandatory=$false)]
        [switch] 
        $PassThru
    )

    if ($PassThru) {
        return
    }
    
    $color = switch ($Severity) {
        3 { [ConsoleColor]::Red }
        2 { [ConsoleColor]::Yellow }
        1 { 
            if ($Emphasize) { [ConsoleColor]::Cyan } else { [ConsoleColor]::White } 
        }
        0 { [ConsoleColor]::Gray }
        default { [ConsoleColor]::Red }
    }

    if ($Header) { 
        Write-Host -Object $Header.Substring(0, 3) -NoNewline -Fore $color
        Write-Host -Object $Header.Substring(3) -NoNewline -Fore "Gray"
    }

    if ($Message) { 
        foreach ($msg in $Message) {
            Write-Host $msg -Fore $color
        }
    }
}