122 lines
3.4 KiB
PowerShell
122 lines
3.4 KiB
PowerShell
# 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-Verbose -Message "Log level set to $Level ($($script:CurrentLogLevel))"
|
|
}
|
|
|
|
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 Start-Logging {
|
|
param(
|
|
[Parameter(Mandatory = $false)]
|
|
[string]$LogPath = "$env:TEMP\pslog_$(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 = "=== Logging Started at $(Get-Date) ==="
|
|
Add-Content -Path $LogPath -Value $LogHeader -ErrorAction Stop
|
|
Write-LogMessage -Message "Logging initialized" -Level Status
|
|
}
|
|
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
|
|
try {
|
|
$LogHeader = "=== Logging Stopped at $(Get-Date) ==="
|
|
Add-Content -Path $LogPath -Value $LogHeader -ErrorAction Stop
|
|
Write-LogMessage -Message "Logging stopped" -Level Status
|
|
}
|
|
catch {
|
|
Write-Warning "Failed to stop logging: $_"
|
|
}
|
|
}
|
|
}
|
|
|
|
Export-ModuleMember -Function Set-LogLevel, Write-LogMessage, Start-Logging, Stop-Logging |