Helpers/Write-ScriptLoggerLog.ps1
|
<# .SYNOPSIS Log a message with the specified log level. .DESCRIPTION If the specified log level is higher as the configured log level, the message will be logged to the enabled destinations. These are the specified log file, the PowerShell event log and the current PowerShell console. .INPUTS None. .OUTPUTS None. .EXAMPLE PS C:\> Write-ScriptLoggerLog -Name 'Default' -Message 'My Warning Message' -Level Warning Log the warning message. .LINK https://github.com/claudiospizzi/ScriptLogger #> function Write-ScriptLoggerLog { [CmdletBinding(SupportsShouldProcess = $true)] param ( # The logger name. [Parameter(Mandatory = $true)] [System.String] $Name, # The message to log. [Parameter(Mandatory = $true)] [System.String] $Message, # The log level to use. [Parameter(Mandatory = $true)] [ValidateSet('Verbose', 'Information', 'Warning', 'Error')] [System.String] $Level ) if ($Script:Loggers.ContainsKey($Name)) { $logger = $Script:Loggers[$Name] # Check if the logging enabled if ($logger.Enabled) { $levelMap = @{ 'Verbose' = 0 'Information' = 1 'Warning' = 2 'Error' = 3 } # Check the logging level: The requested level needs to be equals or higher than the configured level if ($levelMap[$Level] -ge $levelMap[$logger.Level]) { # Use the call stack to get the caller info. Skip the first two # entries (this function and the Write-* wrapper function). $callerContext = Get-PSCallStack | Select-Object -First 1 -Skip 2 if (-not [System.String]::IsNullOrEmpty($callerContext.ScriptName)) { $callerInfo = [System.IO.Path]::GetFileName($callerContext.ScriptName) if ($callerContext.ScriptLineNumber -gt 0) { $callerInfo += ':' + $callerContext.ScriptLineNumber } } else { $callerInfo = [System.String] $callerContext.Command } if ($logger.LogFile -and $PSCmdlet.ShouldProcess('LogFile', 'Write Log')) { try { # Output to log file $line = $logger.Format -f (Get-Date), $env:ComputerName, $Env:Username, $Level, $Message, $callerInfo $line | Out-File -FilePath $logger.Path -Encoding $logger.Encoding -Append -ErrorAction Stop } catch { Microsoft.PowerShell.Utility\Write-Warning "ScriptLogger '$Name' module error during write log file: $_" } } if ($logger.EventLog -and $PSCmdlet.ShouldProcess('EventLog', 'Write Log')) { try { # Write to platform specific log. Write-ScriptLoggerPlatformLog -Level $Level -Message "[$callerInfo] $Message" -ErrorAction 'Stop' } catch { Microsoft.PowerShell.Utility\Write-Warning "ScriptLogger '$Name' module error during write event log: $_" } } if ($logger.ConsoleOutput -and $PSCmdlet.ShouldProcess('ConsoleOutput', 'Write Log')) { switch ($Level) { 'Verbose' { Microsoft.PowerShell.Utility\Write-Verbose -Message $Message -Verbose:$true } 'Information' { Microsoft.PowerShell.Utility\Write-Information -MessageData $Message -InformationAction 'Continue' } 'Warning' { Microsoft.PowerShell.Utility\Write-Warning -Message $Message -WarningAction 'Continue' } 'Error' { Microsoft.PowerShell.Utility\Write-Error -Message $Message -ErrorAction 'Continue' } } } } } } else { Microsoft.PowerShell.Utility\Write-Warning "ScriptLogger '$Name' not found. No logs written." } } |