sentinel/Modules/Logging/Logging.psm1

39 lines
1023 B
PowerShell
Raw Permalink Normal View History

2025-01-03 02:23:16 +00:00
# Modules/Logging/Logging.psm1
# Function to initialize the log file path
function Initialize-Logging {
param (
[Parameter(Mandatory = $true)]
[string]$Path
)
# Store the log file path in a module-scoped variable
$Script:LogFile = $Path
}
# Function to log messages with timestamps
function Write-Log {
param (
[string]$Message,
[ValidateSet("INFO", "WARNING", "ERROR")]
[string]$Type = "INFO"
)
if (-not $Script:LogFile) {
Throw "Log file path is not initialized. Please run Initialize-Logging first."
}
$logTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "$logTimestamp [$Type] - $Message"
Add-Content -Path $Script:LogFile -Value $logMessage
switch ($Type) {
"INFO" { Write-Verbose $logMessage }
"WARNING" { Write-Warning $logMessage }
"ERROR" { Write-Error $logMessage }
}
}
# Export the functions
Export-ModuleMember -Function Initialize-Logging, Write-Log