Write-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
Enum errorLevel
{
    INFO
    SUCCESS
    WARNING
    ERROR
    DEBUG
}

Function Write-Log
{
    param(
        [errorLevel][Parameter(Mandatory=$true)]$errorLevel,
        [string]$message,
        [string]$stack = $null
    )
    $date = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
   $errorTab =@(("[INFO]","Yellow"),("[SUCCESS]","Green"),("[WARNING]","Yellow"),("[ERROR]","Red"),("[DEBUG]","DarkYellow"))
   if([int]$errorLevel -gt $errorTab.Count)
   {
       Write-Host $message
   }
   else
   {
    $write = [string]::Format("{0} {1} {2} {3}",$date,$errorTab[[int]$errorLevel][0],$message,$stack)
    Write-Host $write -ForegroundColor $errorTab[[int]$errorLevel][1]
    $write | Out-File -append ([string]::Concat($($MyInvocation.ScriptName.TrimEnd('.ps1')),"_log.txt"))
   }
}
Export-ModuleMember -Function 'Write-Log'