modules/Log/Log.psm1

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
function log {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $True, Position=1)]
        [ValidateNotNullOrEmpty()]
        [string]$Message,
        [Parameter(Position=0)]
        [Alias("level")]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Information','Warning','Error','Verbose','Debug')]
        [string]$Severity = 'Information'
    )

    Add-Content -Path ("Start-AzBasicLoadBalancerUpgrade.log") -Value ((Get-Date -Format 'yyyy-MM-dd hh:mm:ss.ffff') + " " + "[$Severity] - " + $Message) -Force
    $outputMessage = "[{0}]:{1}" -f $Severity,($Message -replace '\s\s+?','')
    If ($global:FollowLog) {
        #$outputMessage = "[{0}]:{1}" -f $Severity,$Message
        Write-Host $outputMessage
        switch ($severity) {
            "Error" {
                Write-Error $outputMessage
            }
            "Warning" {
                Write-Warning $outputMessage
            }
            "Information" {
                Write-Information $outputMessage -InformationAction SilentlyContinue
            }
            "Verbose" {
                Write-Verbose $outputMessage
            }
            "Debug" {
                Write-Debug $outputMessage
            }
        }
    }
    else {
        switch ($severity) {
            "Error" {
                Write-Error $outputMessage
            }
            "Warning" {
                Write-Warning $outputMessage
            }
        }
    }
}

Export-ModuleMember -Function log