66 lines
1.7 KiB
PowerShell
66 lines
1.7 KiB
PowerShell
# Logger.psm1 -- Einfaches Logging-Modul fuer den Mail-Organizer
|
|
|
|
$script:LogFile = $null
|
|
$script:LogLevel = "Info"
|
|
|
|
$script:LevelPriority = @{
|
|
"Debug" = 0
|
|
"Info" = 1
|
|
"Warn" = 2
|
|
"Error" = 3
|
|
}
|
|
|
|
function Initialize-Logger {
|
|
param(
|
|
[Parameter(Mandatory)]
|
|
[string]$LogDirectory,
|
|
|
|
[string]$Level = "Info",
|
|
[int]$RetentionDays = 30
|
|
)
|
|
|
|
if (-not (Test-Path $LogDirectory)) {
|
|
New-Item -ItemType Directory -Path $LogDirectory -Force | Out-Null
|
|
}
|
|
|
|
$script:LogFile = Join-Path $LogDirectory "mail-organizer_$(Get-Date -Format 'yyyy-MM-dd').log"
|
|
$script:LogLevel = $Level
|
|
|
|
# Alte Logs aufraeumen
|
|
Get-ChildItem -Path $LogDirectory -Filter "mail-organizer_*.log" |
|
|
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$RetentionDays) } |
|
|
Remove-Item -Force
|
|
|
|
Write-Log "=== Mail-Organizer gestartet ===" -Level Info
|
|
}
|
|
|
|
function Write-Log {
|
|
param(
|
|
[Parameter(Mandatory)]
|
|
[string]$Message,
|
|
|
|
[ValidateSet("Debug", "Info", "Warn", "Error")]
|
|
[string]$Level = "Info"
|
|
)
|
|
|
|
if ($script:LevelPriority[$Level] -lt $script:LevelPriority[$script:LogLevel]) {
|
|
return
|
|
}
|
|
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
$entry = "[$timestamp] [$Level] $Message"
|
|
|
|
if ($script:LogFile) {
|
|
Add-Content -Path $script:LogFile -Value $entry -Encoding UTF8
|
|
}
|
|
|
|
switch ($Level) {
|
|
"Error" { Write-Host $entry -ForegroundColor Red }
|
|
"Warn" { Write-Host $entry -ForegroundColor Yellow }
|
|
"Debug" { Write-Host $entry -ForegroundColor Gray }
|
|
default { Write-Host $entry }
|
|
}
|
|
}
|
|
|
|
Export-ModuleMember -Function Initialize-Logger, Write-Log
|