handlers/console.psm1


function New-uLogConsole
{
param(
    [string]   $Name      = 'Console',
    [string]   $Source    = $MyInvocation.ScriptName,
    [string]   $Formatter = [LogFormatter]::ConsoleDefault,
    [LogLevel] $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

        if ($Record.Level -ge $this.Level){
            switch ($Record.Level)
            {
                ([LogLevel]::INFO)    { 
                        Write-Host $FormattedMessage
                        break 
                    }
                ([LogLevel]::INFORMATION)    { 
                        Write-Host $FormattedMessage
                        break 
                    }
                ([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 Yellow 
                        }
                        break 
                    }
                default      { Write-Host $FormattedMessage ; break }
            }    
        }
    }

    $log
}
}