Last active
January 27, 2025 15:18
-
-
Save quonic/1246312b7396aaaee608bf95fd877a16 to your computer and use it in GitHub Desktop.
A way to standardize logging.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Write-Log { | |
<# | |
.SYNOPSIS | |
Standard Event Log entry writer | |
.DESCRIPTION | |
Writes an entry to the local system's Event Log in a predictable and dependable way. | |
.INPUTS | |
None | |
.OUTPUTS | |
None | |
.NOTES | |
Version: 1.0 | |
Author: Spyingwind | |
License: MIT or use how you like | |
Creation Date: 3/16/2017 | |
Updated Date: 5/27/2003 | |
Changes: | |
Initial Event Log Writer | |
Implimented file logging | |
.EXAMPLE | |
Write-Log -Method Console -Level Info -Message "Did task" -EventID 0 | |
Logs to console | |
.EXAMPLE | |
Write-Log -Level Info -Message "Did task" -EventID 0 | |
Logs to Event Log | |
.EXAMPLE | |
Write-Log -Level Warn -Message "Did bad task" -EventID 1 | |
Logs to Event Log with warning | |
.EXAMPLE | |
Write-Log -Method LogFile -File "C:\Temp\MyLog.log" -Level Info -Message "Did task" -EventID 0 | |
Logs to a log file with event id | |
.EXAMPLE | |
Write-Log -Method LogFile -File "C:\Temp\MyLog.log" -Level Info -Message "Did task" | |
Logs to a log file | |
.EXAMPLE | |
Write-Log -Method LogFile -File "C:\Temp\MyLog.log" -Level Info -Message "Did task" -UseUtc | |
Logs to a log file with dates in UTC time | |
#> | |
Param( | |
[Parameter(Mandatory = $true, ParameterSetName = "EventLog")] | |
[Parameter(Mandatory = $true, ParameterSetName = "Console")] | |
[Parameter(Mandatory = $true, ParameterSetName = "LogFile")] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
# The information that you wish to convey to the log | |
$Message, | |
[Parameter(Mandatory = $true, ParameterSetName = "EventLog")] | |
[Parameter(Mandatory = $true, ParameterSetName = "Console")] | |
[Parameter(Mandatory = $false, ParameterSetName = "LogFile")] | |
[ValidateNotNullOrEmpty()] | |
[ValidateCount(0, 65535)] | |
[int] | |
# The Event ID of this log entry | |
$EventID, | |
[Parameter(Mandatory = $false, ParameterSetName = "EventLog")] | |
[Parameter(Mandatory = $false, ParameterSetName = "Console")] | |
[Parameter(Mandatory = $false, ParameterSetName = "LogFile")] | |
[ValidateSet("Error", "Warn", "Warning", "Info", "Information", "Fatal", "Debug", "Verbose")] | |
[Alias("EntryType")] | |
[string] | |
# Sets the what type of entry this is | |
$Level = "Info", | |
[Parameter(Mandatory = $false, ParameterSetName = "EventLog")] | |
[Parameter(Mandatory = $false, ParameterSetName = "Console")] | |
[Parameter(Mandatory = $false, ParameterSetName = "LogFile")] | |
[ValidateSet("EventLog", "Console", "LogFile")] | |
[string] | |
# Method of logging | |
$Method = "EventLog", | |
[Parameter(Mandatory = $true, ParameterSetName = "LogFile")] | |
[ValidateScript( | |
{ | |
if ($(Split-Path -Path $_ -Parent | Test-Path)) { $true } | |
else { throw "Folder ($(Split-Path -Path $_ -Parent)) does not exist." } | |
} | |
)] | |
[string] | |
# Log file to write to | |
$File, | |
[Parameter(Mandatory = $true, ParameterSetName = "LogFile")] | |
[switch] | |
# Uses UTC time | |
$UseUtc | |
) | |
switch ($Method) { | |
'EventLog' { | |
$Message = "{0}: {1}" -f $Level, $Message | |
switch ($Level) { | |
'Error' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType FailureAudit -EventId $EventID -Message $Message } | |
'Warn' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType Warning -EventId $EventID -Message $Message } | |
'Warning' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType Warning -EventId $EventID -Message $Message } | |
'Info' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType Information -EventId $EventID -Message $Message } | |
'Information' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType Information -EventId $EventID -Message $Message } | |
'Fatal' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType Error -EventId $EventID -Message $Message } | |
'Debug' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType Information -EventId $EventID -Message $Message } | |
'Verbose' { Write-EventLog -LogName "Application" -Source $ScriptName -EntryType SuccessAudit -EventId $EventID -Message $Message } | |
} | |
} | |
'Console' { | |
switch ($Level) { | |
'Error' { Write-Error -Message "$Message" -ErrorId $EventID } | |
'Warn' { Write-Warning "Warning $EventID : $Message" } | |
'Warning' { Write-Warning "Warning $EventID : $Message" } | |
'Info' { Write-Information "$EventID : $Message" -ForegroundColor White } | |
'Information' { Write-Information "$EventID : $Message" -ForegroundColor White } | |
'Fatal' { Write-Error -Message "$Message" -ErrorId $EventID } | |
'Debug' { Write-Debug -Message "$EventID : $Message" } | |
'Verbose' { Write-Verbose "$EventID : $Message" } | |
} | |
} | |
'LogFile' { | |
if (-not $(Test-Path -Path $File -ErrorAction SilentlyContinue)) { | |
try { | |
New-Item $File -ErrorAction Stop | |
} | |
catch { | |
Write-Error -Message "Could not create file." -TargetObject $File | |
} | |
} | |
$LogLevel = switch ($Level) { | |
'Error' { "[Error]" } | |
'Warn' { "[Warn]" } | |
'Warning' { "[Warn]" } | |
'Info' { "[Info]" } | |
'Information' { "[Info]" } | |
'Fatal' { "[Fatal]" } | |
'Debug' { "[Debug]" } | |
'Verbose' { "[Verbose]" } | |
} | |
$Date = if ($UseUtc) { | |
$(Get-Date).ToUniversalTime().ToString("o") | |
} | |
else { | |
$(Get-Date).ToString("o") | |
} | |
if ($EventID) { | |
"{0} {1}: {2,9} {3:n} {4}" -f $Date, $ScriptName, $LogLevel, $EventID, $Message | Add-Content -Path $File | |
} | |
else { | |
"{0} {1}: {2,9} {3}" -f $Date, $ScriptName, $LogLevel, $Message | Add-Content -Path $File | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment