-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogging.psm1
More file actions
143 lines (118 loc) · 4.52 KB
/
Copy pathLogging.psm1
File metadata and controls
143 lines (118 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Globale Variablen
$Global:LogFilePath = $null
$Global:LogLevel = "INFO"
$Global:EnableConsoleOutput = $true
$Global:MaxLogSizeMB = 5
$Global:DefaultLogFolder = "C:\Users\Public\10020115_WinScripts\Logs"
$Global:LastLogScriptName = $null
function Initialize-Logger {
param(
[ValidateSet("DEBUG","INFO","WARN","ERROR")]
[string]$Level = "INFO",
[bool]$ConsoleOutput = $true,
[int]$MaxSizeMB = 5,
# Optionaler expliziter Dateiname (z.B. für Update-Skript)
[string]$FileName = ""
)
if (-not (Test-Path $Global:DefaultLogFolder)) {
New-Item -Path $Global:DefaultLogFolder -ItemType Directory -Force | Out-Null
}
if ($FileName -ne "") {
$Path = Join-Path $Global:DefaultLogFolder $FileName
} else {
$date = Get-Date -Format "yyyy-MM-dd"
$Path = Join-Path $Global:DefaultLogFolder "log_$date.log"
}
if (-not (Test-Path $Path)) {
New-Item -Path $Path -ItemType File -Force | Out-Null
}
$Global:LogFilePath = $Path
$Global:LogLevel = $Level
$Global:EnableConsoleOutput = $ConsoleOutput
$Global:MaxLogSizeMB = $MaxSizeMB
$Global:LastLogScriptName = $null
Write-Log -Level "INFO" -Message "Logger initialisiert. Logfile: $Path"
}
function Rotate-Log {
if (-not $Global:LogFilePath -or -not (Test-Path $Global:LogFilePath)) { return }
$sizeMB = (Get-Item $Global:LogFilePath).Length / 1MB
if ($sizeMB -ge $Global:MaxLogSizeMB) {
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$archivePath = "$Global:LogFilePath.$timestamp.bak"
Move-Item -Path $Global:LogFilePath -Destination $archivePath -Force
New-Item -Path $Global:LogFilePath -ItemType File -Force | Out-Null
}
# Tagesrotation nur bei tagesbasiertem Dateinamen
if ($Global:LogFilePath -match "log_\d{4}-\d{2}-\d{2}\.log$") {
$currentDate = Get-Date -Format "yyyy-MM-dd"
$expectedFile = Join-Path $Global:DefaultLogFolder "log_$currentDate.log"
if ($expectedFile -ne $Global:LogFilePath) {
$Global:LogFilePath = $expectedFile
if (-not (Test-Path $expectedFile)) {
New-Item -Path $expectedFile -ItemType File -Force | Out-Null
}
}
}
}
function Write-Log {
param(
[Parameter(Mandatory)]
[ValidateSet("DEBUG","INFO","WARN","ERROR")]
[string]$Level,
[Parameter(Mandatory)]
[string]$Message
)
if (-not $Global:LogFilePath) {
Initialize-Logger
}
$levels = @{ DEBUG = 1; INFO = 2; WARN = 3; ERROR = 4 }
if ($levels[$Level] -lt $levels[$Global:LogLevel]) { return }
Rotate-Log
$callStack = Get-PSCallStack
$scriptFrame = $callStack | Where-Object { $_.ScriptName -and $_.ScriptName.Trim() -ne "" } | Select-Object -First 1
$scriptName = if ($scriptFrame) {
Split-Path $scriptFrame.ScriptName -Leaf
} elseif ($PSCommandPath) {
Split-Path $PSCommandPath -Leaf
} else {
$Host.Name
}
if ($Global:LastLogScriptName -ne $scriptName) {
$nl = [Environment]::NewLine
$headerText = "$nl$nl$nl+++ $scriptName +++"
Add-Content -Path $Global:LogFilePath -Value $headerText
if ($Global:EnableConsoleOutput) {
Write-Host $headerText -ForegroundColor Magenta
}
$Global:LastLogScriptName = $scriptName
}
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$entry = "[$timestamp] [$Level] $Message"
Add-Content -Path $Global:LogFilePath -Value $entry
if ($Global:EnableConsoleOutput) {
switch ($Level) {
"ERROR" { Write-Host $entry -ForegroundColor Red }
"WARN" { Write-Host $entry -ForegroundColor Yellow }
"INFO" { Write-Host $entry -ForegroundColor Cyan }
"DEBUG" { Write-Host $entry -ForegroundColor DarkGray }
}
}
}
function Get-LogConfig {
[PSCustomObject]@{
LogFilePath = $Global:LogFilePath
LogLevel = $Global:LogLevel
ConsoleOutput = $Global:EnableConsoleOutput
MaxLogSizeMB = $Global:MaxLogSizeMB
DefaultFolder = $Global:DefaultLogFolder
LastScriptName = $Global:LastLogScriptName
}
}
function Close-Logger {
if ($Global:LogFilePath) {
Write-Log -Level "INFO" -Message "Logger wird geschlossen."
}
$Global:LogFilePath = $null
$Global:LastLogScriptName = $null
}
Export-ModuleMember -Function Initialize-Logger, Write-Log, Rotate-Log, Get-LogConfig, Close-Logger