From 579899cc620679f87083a76124255bd5e526a2b2 Mon Sep 17 00:00:00 2001 From: misdept Date: Mon, 6 Jan 2025 14:44:16 -0500 Subject: [PATCH] Add logging module --- PS-Logger.psm1 | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 PS-Logger.psm1 diff --git a/PS-Logger.psm1 b/PS-Logger.psm1 new file mode 100644 index 0000000..37e74ab --- /dev/null +++ b/PS-Logger.psm1 @@ -0,0 +1,115 @@ +# Define log levels +$script:LogLevels = @{ + Debug = 0 + Info = 1 + Status = 2 + Warning = 3 + Success = 4 + Error = 5 +} + +$script:CurrentLogLevel = $LogLevels.Info # Default level + +function Set-LogLevel { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [ValidateSet('Debug', 'Info', 'Status', 'Warning', 'Success', 'Error')] + [string]$Level + ) + + $script:CurrentLogLevel = $script:LogLevels[$Level] + Write-LogMessage -Message "Log level set to: $Level" -Level Info +} + +function Write-LogMessage { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Message, + + [Parameter(Mandatory = $false)] + [ValidateSet('Info', 'Warning', 'Error', 'Success', 'Status', 'Debug')] + [string]$Level = 'Info', + + [Parameter(Mandatory = $false)] + [string]$LogPath = $script:logPath, + + [Parameter(Mandatory = $false)] + [switch]$NoConsole + ) + + # Only process if message level is >= current log level + if ($script:LogLevels[$Level] -ge $script:CurrentLogLevel) { + $ColorMap = @{ + 'Info' = 'Cyan' + 'Warning' = 'Yellow' + 'Error' = 'Red' + 'Success' = 'Green' + 'Status' = 'Magenta' + 'Debug' = 'DarkMagenta' + } + + $TimeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $LogEntry = "[$TimeStamp] [$Level] $Message" + + # Write to log file if logging is enabled + if ($global:loggingEnabled) { + try { + Add-Content -Path $LogPath -Value $LogEntry -ErrorAction Stop + } + catch { + Write-Warning "Failed to write to log file: $_" + } + } + + # Write to console unless suppressed + if (-not $NoConsole) { + $Color = $ColorMap[$Level] + Write-Host -ForegroundColor $Color $LogEntry + } + } +} + +function Initialize-Logging { + [CmdletBinding()] + param( + [Parameter(Mandatory = $false)] + [string]$LogPath = "$env:TEMP\PremierOne_Install_$(Get-Date -Format 'yyyyMMdd_HHmmss').log", + + [Parameter(Mandatory = $false)] + [switch]$Force, + + [Parameter(Mandatory = $false)] + [ValidateSet('Debug', 'Info', 'Status', 'Warning', 'Success', 'Error')] + [string]$Level = 'Info' + ) + + $global:loggingEnabled = $true + Set-LogLevel -Level $Level + + $script:logPath = $LogPath + + if ($Force -and (Test-Path $LogPath)) { + Remove-Item -Path $LogPath -Force + } + + try { + $LogHeader = "=== PremierOne Installation Log Started at $(Get-Date) ===" + Add-Content -Path $LogPath -Value $LogHeader -ErrorAction Stop + Write-LogMessage -Message "Logging initialized" -Level Info + } + catch { + Write-Warning "Failed to initialize logging: $_" + $global:loggingEnabled = $false + } +} + +function Stop-Logging { + if ($global:loggingEnabled) { + Write-LogMessage -Message "=== Logging stopped at $(Get-Date) ===" -Level Info + $global:loggingEnabled = $false + } +} + +Export-ModuleMember -Function Set-LogLevel, Write-LogMessage, Initialize-Logging, Stop-Logging \ No newline at end of file