Public/Core/Write-Log.ps1
|
<#
.SYNOPSIS Writes a message to the specified log .DESCRIPTION Writes a message to the specified log. Ensure that Set-LogType has been called before calling this function. .PARAMETER Message The message to write to the log .PARAMETER DebugMessage Specifies that the message is a debug message .PARAMETER Info Specifies that the message is an informational message .PARAMETER Warn Specifies that the message is a warning message .PARAMETER Error Specifies that the message is an error message .PARAMETER Critical Specifies that the message is a critical error message .PARAMETER ForegroundColor The foreground color to use when writing to the screen. Overrides the color set in the module preferences. .PARAMETER BackgroundColor The background color to use when writing to the screen. Overrides the color set in the module preferences. .EXAMPLE Write-Log -Message "This is an informational message" Write-Log -Message "This is a warning message" -Warn .EXAMPLE Write-Log -Message "This is an error message" -Error -ForegroundColor White -BackgroundColor Red .NOTES If no message type is specified, the message will be written as an informational message. #> function Write-Log() { [CmdletBinding(DefaultParameterSetName="Info")] Param( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true,HelpMessage="The message to write to the log")] [string]$Message, [Parameter(ParameterSetName="Debug", Mandatory=$false, HelpMessage="Specifies that the message is a debug message")] [switch]$DebugMessage, [Parameter(ParameterSetName="Info", Mandatory=$false, HelpMessage="Specifies that the message is an informational message")] [switch]$Info, [Parameter(ParameterSetName="Warn", Mandatory=$false, HelpMessage="Specifies that the message is a warning message")] [switch]$Warn, [Parameter(ParameterSetName="Error", Mandatory=$false, HelpMessage="Specifies that the message is an error message")] [switch]$Error, [Parameter(ParameterSetName="Critical", Mandatory=$false, HelpMessage="Specifies that the message is a critical error message")] [switch]$Critical, [Parameter(Mandatory=$false, HelpMessage="The foreground color to use when writing to the screen. Overrides the color set in the module preferences.")] [System.ConsoleColor]$ForegroundColor, [Parameter(Mandatory=$false, HelpMessage="The background color to use when writing to the screen. Overrides the color set in the module preferences.")] [System.ConsoleColor]$BackgroundColor ) Begin { if ($LogConfig.ValidConfig -eq $false) { Write-LogError -Message "Log configuration has not been set. Please call Set-Log before calling this function." return } $colorParams = @{} if ($PSBoundParameters.ContainsKey('ForegroundColor')) { $colorParams['ForegroundColor'] = $ForegroundColor } if ($PSBoundParameters.ContainsKey('BackgroundColor')) { $colorParams['BackgroundColor'] = $BackgroundColor } if ($DebugMessage) { Write-LogDebug -Message $Message @colorParams } elseif ($Info) { Write-LogInfo -Message $Message @colorParams } elseif ($Warn) { Write-LogWarn -Message $Message @colorParams } elseif ($Error) { Write-LogError -Message $Message @colorParams } elseif ($Critical) { Write-LogCritical -Message $Message @colorParams } else { Write-LogInfo -Message $Message @colorParams } } } |