ulog.psm1
Add-Type -TypeDefinition @" public enum LogLevel { DEBUG = 600, VERBOSE = 501, TRACE = 500, INFORMATION = 401, INFO = 400, WARNING = 301, WARN = 300, ERROR = 200, FATAL = 101, CRITICAL = 100, SUCCESS = 50 } "@ -ErrorAction SilentlyContinue <# .Synopsis Instantiates a new log manager .DESCRIPTION Description d�taill�e .EXAMPLE $log = New-uLog Log-Info -Message 'Hello' This example creates a new log manager and write an information message on the console .EXAMPLE $log = New-uLog $log.AddLogProvider( (New-uLogEventLog) ) Log-Info -Message 'Hello' This example creates a new log manager, adds a evenlog provider the infoemtion message is #> function New-uLog { param( [switch] $Append = $true, [string] $Source = $MyInvocation.ScriptName ) Begin{ if ($Source -eq ''){$Source = 'Console'} } Process{ $global:LOGAPPEND = $Append if ($Source.Contains('.') -and $Source.Substring( $Source.LastIndexOf('.')) -eq '.psm1'){ $providers = @((New-uLogEventLog -Source $Source)) }else{ $providers = @((New-uLogConsole -Source $Source)) } $log = [PSCustomObject] @{Source = $Source; Providers = $providers } $log | Add-Member -MemberType ScriptMethod -Name AddLogHandler -Value { param($Provider) $Provider.Source = $this.Source $this.providers += $Provider if ($Provider.Name -ne ''){ $this |Add-Member -MemberType NoteProperty -Name $Provider.Name -Value $Provider } } $global:uLOG = $log $global:uLOG } } function New-LogRecord { param( [string] $Message, [string] $Level, [int] $Id = 10000, [int] $Indent = 1 ) [PSCustomObject] @{Message = $Message; Level = $Level; Id = $Id; Indent = $Indent } } function Write-Log { param( [string] $Message, [ValidateSet('INFO', 'INFORMATION', 'WARN', 'WARNING', 'ERROR', 'FATAL', 'CRITICAL', 'DEBUG', 'TRACE', 'SUCCESS', 'VERBOSE')] $Level, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log = $null, [string] $Source = '' ) $record = New-LogRecord -Message $Message -Level $Level -Id $Id -Indent $Indent if ($Log -eq $null){ Get-Variable -Name uLog -Scope Global -ErrorAction SilentlyContinue | Out-Null if($?){ $Log = $Global:uLog }else{ if ($Source -eq ''){ $source = $MyInvocation.ScriptName } $Log = New-uLog -Source $Source } } $log.providers | % { $_.WriteLog($record) } } function Log-Info { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) $Source = $MyInvocation.ScriptName Write-Log -Message $Message -Level INFO -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log -Source $Source } function Log-Warning { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level WARNING -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } function Log-Error { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level ERROR -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } function Log-Verbose { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level VERBOSE -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } function Log-Debug { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level DEBUG -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } function Log-Trace { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level TRACE -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } function Log-Success { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level SUCCESS -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } function Log-Critical { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level CRITICAL -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } function Log-Fatal { param( [string] $Message, [int] $Id = 1, [string] $Path = $null, [switch] $Append = $false, [switch] $NoDisplayOnTerminal = $false, [int] $Indent = 1, $Log=$null ) Write-Log -Message $Message -Level FATAL -Id $Id -Append $Append -NoDisplayOnTerminal $NoDisplayOnTerminal -Indent $Indent -Log $Log } |