39 lines
1023 B
PowerShell
39 lines
1023 B
PowerShell
# 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 |