handlers/console.psm1
function New-uLogConsole { param( [string] $Name = '', [string] $Source = $MyInvocation.ScriptName, [string] $Formatter = [LogFormatter]::ConsoleDefault, [string] $Level, [Switch] $Enabled = $true ) Begin{ if ($Source -eq ''){$Source = 'Console'} } Process{ $log = [PSCustomObject] @{Name = $Name; Enabled = $Enabled; Type = 'console'; Formatter = $Formatter; Source = $source } $log | Add-Member -MemberType ScriptMethod -Name WriteLog -Value { param($Record) if (-not $this.Enabled){return} if ($this.Formatter -notmatch '-'){$this.Formatter = 'Format-' + $this.Formatter} $FormattedMessage = & $this.Formatter -Record $Record switch ($Record.Level) { ([LogLevel]::WARNING) { Write-Host $FormattedMessage -ForegroundColor Yellow ; break } ([LogLevel]::WARN) { Write-Host $FormattedMessage -ForegroundColor Yellow ; break } ([LogLevel]::ERROR) { Write-Host $FormattedMessage -ForegroundColor Red ; break } ([LogLevel]::FATAL) { Write-Host $FormattedMessage -ForegroundColor Red ; break } ([LogLevel]::CRITICAL) { Write-Host $FormattedMessage -ForegroundColor Red ; break } ([LogLevel]::TRACE) { Write-Host $FormattedMessage -ForegroundColor Cyan ; break } ([LogLevel]::DEBUG) { if($DebugPreference -ne 'SilentlyContinue'){ Write-Debug $FormattedMessage -Debug } break } ([LogLevel]::SUCCESS) { Write-Host $FormattedMessage -ForegroundColor Green ; break } ([LogLevel]::VERBOSE) { if ($VerbosePreference -ne 'SilentlyContinue'){ Write-Host $FormattedMessage -ForegroundColor Cyan } break } default { Write-Host $FormattedMessage ; break } } } $log } } |