handlers/console.psm1
function New-uLogConsole { param( [string] $Name = 'Console', [string] $Source = $MyInvocation.ScriptName, [string] $Formatter = [LogFormatter]::ConsoleDefault, [string] $Level = [LogLevel]::SUCCESS, [Switch] $Enabled = $true ) Begin{ if ($Source -eq ''){$Source = 'Console'} } Process{ $log = [PSCustomObject] @{Name = $Name; Enabled = $Enabled; Type = 'console'; Formatter = $Formatter; Level = $Level; 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]::INFO) { if ($this.Level -le [LogLevel]::INFO){ Write-Host $FormattedMessage } break } ([LogLevel]::INFORMATION) { if ($this.Level -le [LogLevel]::INFORMATION){ Write-Host $FormattedMessage } break } ([LogLevel]::WARNING) { if ($this.Level -le [LogLevel]::WARNING){ Write-Host $FormattedMessage -ForegroundColor Yellow } break } ([LogLevel]::WARN) { if ($this.Level -le [LogLevel]::WARN){ Write-Host $FormattedMessage -ForegroundColor Yellow } break } ([LogLevel]::ERROR) { if ($this.Level -le [LogLevel]::ERROR){ Write-Host $FormattedMessage -ForegroundColor Red } break } ([LogLevel]::FATAL) { if ($this.Level -le [LogLevel]::FATAL){ Write-Host $FormattedMessage -ForegroundColor Red } break } ([LogLevel]::CRITICAL) { if ($this.Level -le [LogLevel]::CRITICAL){ Write-Host $FormattedMessage -ForegroundColor Red } break } ([LogLevel]::TRACE) { if ($this.Level -le [LogLevel]::TRACE){ Write-Host $FormattedMessage -ForegroundColor Cyan } break } ([LogLevel]::DEBUG) { if ($this.Level -le [LogLevel]::DEBUG){ if($DebugPreference -ne 'SilentlyContinue'){ Write-Debug $FormattedMessage -Debug } } break } ([LogLevel]::SUCCESS) { if ($this.Level -le [LogLevel]::SUCCESS){ Write-Host $FormattedMessage -ForegroundColor Green } break } ([LogLevel]::VERBOSE) { if ($this.Level -le [LogLevel]::VERBOSE){ if ($VerbosePreference -ne 'SilentlyContinue'){ Write-Host $FormattedMessage -ForegroundColor Yellow } } break } default { Write-Host $FormattedMessage ; break } } } $log } } |