# 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