Skip to content

Commit d0b1e96

Browse files
committed
Refactor console logging level handling
Moves CippConsoleLogLevel enum to script scope and refactors log level handling to use string values instead of enum instances. Improves environment variable parsing and log level comparison logic for Application Insights telemetry integration.
1 parent 3da08cc commit d0b1e96

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

Modules/CIPPCore/Public/Tools/Enable-CippConsoleLogging.ps1

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# Define log level enum at script scope
2+
enum CippConsoleLogLevel {
3+
Debug = 0
4+
Verbose = 1
5+
Information = 2
6+
Warning = 3
7+
Error = 4
8+
}
9+
110
function Enable-CippConsoleLogging {
211
<#
312
.SYNOPSIS
@@ -17,50 +26,38 @@ function Enable-CippConsoleLogging {
1726
[CmdletBinding()]
1827
param()
1928

20-
# Store the original functions
21-
if (-not $global:CippOriginalWriteFunctions) {
22-
$global:CippOriginalWriteFunctions = @{
23-
Information = Get-Command Write-Information -CommandType Cmdlet
24-
Warning = Get-Command Write-Warning -CommandType Cmdlet
25-
Error = Get-Command Write-Error -CommandType Cmdlet
26-
Verbose = Get-Command Write-Verbose -CommandType Cmdlet
27-
Debug = Get-Command Write-Debug -CommandType Cmdlet
28-
Host = Get-Command Write-Host -CommandType Cmdlet
29-
}
30-
}
31-
32-
# Define log level enum
33-
enum CippConsoleLogLevel {
34-
Debug = 0
35-
Verbose = 1
36-
Information = 2
37-
Warning = 3
38-
Error = 4
39-
}
40-
4129
# Set minimum log level from environment variable (default: Information)
30+
$validLevels = @('Debug', 'Verbose', 'Information', 'Warning', 'Error')
4231
$configuredLevel = $env:CIPP_CONSOLE_LOG_LEVEL
43-
$global:CippConsoleLogMinLevel = if ($configuredLevel) {
44-
try {
45-
[CippConsoleLogLevel]$configuredLevel
46-
} catch {
47-
[CippConsoleLogLevel]::Information
48-
}
32+
$global:CippConsoleLogMinLevel = if ($configuredLevel -and $configuredLevel -in $validLevels) {
33+
$configuredLevel
4934
} else {
50-
[CippConsoleLogLevel]::Information
35+
'Information'
5136
}
5237

5338
# Helper function to send log to Application Insights
5439
$global:SendCippConsoleLog = {
55-
param([string]$Message, [CippConsoleLogLevel]$Level)
40+
param([string]$Message, [string]$Level)
5641

5742
if ($global:TelemetryClient) {
5843
try {
44+
# Map level names to numeric values for comparison
45+
$levelMap = @{
46+
'Debug' = 0
47+
'Verbose' = 1
48+
'Information' = 2
49+
'Warning' = 3
50+
'Error' = 4
51+
}
52+
53+
$currentLevelValue = $levelMap[$Level]
54+
$minLevelValue = $levelMap[$global:CippConsoleLogMinLevel]
55+
5956
# Check if this level should be logged
60-
if ($Level -ge $global:CippConsoleLogMinLevel) {
57+
if ($currentLevelValue -ge $minLevelValue) {
6158
$props = New-Object 'System.Collections.Generic.Dictionary[string,string]'
6259
$props['Message'] = $Message
63-
$props['Level'] = $Level.ToString()
60+
$props['Level'] = $Level
6461
$props['Timestamp'] = (Get-Date).ToString('o')
6562

6663
$global:TelemetryClient.TrackEvent('CIPP.ConsoleLog', $props, $null)
@@ -81,7 +78,7 @@ function Enable-CippConsoleLogging {
8178
)
8279

8380
# Send to telemetry
84-
& $global:SendCippConsoleLog -Message ($MessageData | Out-String).Trim() -Level ([CippConsoleLogLevel]::Information)
81+
& $global:SendCippConsoleLog -Message ($MessageData | Out-String).Trim() -Level 'Information'
8582

8683
# Call original function
8784
Microsoft.PowerShell.Utility\Write-Information @PSBoundParameters
@@ -96,7 +93,7 @@ function Enable-CippConsoleLogging {
9693
)
9794

9895
# Send to telemetry
99-
& $global:SendCippConsoleLog -Message $Message -Level ([CippConsoleLogLevel]::Warning)
96+
& $global:SendCippConsoleLog -Message $Message -Level 'Warning'
10097

10198
# Call original function
10299
Microsoft.PowerShell.Utility\Write-Warning @PSBoundParameters
@@ -125,7 +122,7 @@ function Enable-CippConsoleLogging {
125122
elseif ($Exception) { $Exception.Message }
126123
elseif ($ErrorRecord) { $ErrorRecord.Exception.Message }
127124
else { 'Unknown error' }
128-
& $global:SendCippConsoleLog -Message $errorMessage -Level ([CippConsoleLogLevel]::Error)
125+
& $global:SendCippConsoleLog -Message $errorMessage -Level 'Error'
129126

130127
# Call original function
131128
Microsoft.PowerShell.Utility\Write-Error @PSBoundParameters
@@ -140,7 +137,7 @@ function Enable-CippConsoleLogging {
140137
)
141138

142139
# Send to telemetry
143-
& $global:SendCippConsoleLog -Message $Message -Level ([CippConsoleLogLevel]::Verbose)
140+
& $global:SendCippConsoleLog -Message $Message -Level 'Verbose'
144141

145142
# Call original function
146143
Microsoft.PowerShell.Utility\Write-Verbose @PSBoundParameters
@@ -155,7 +152,7 @@ function Enable-CippConsoleLogging {
155152
)
156153

157154
# Send to telemetry
158-
& $global:SendCippConsoleLog -Message $Message -Level ([CippConsoleLogLevel]::Debug)
155+
& $global:SendCippConsoleLog -Message $Message -Level 'Debug'
159156

160157
# Call original function
161158
Microsoft.PowerShell.Utility\Write-Debug @PSBoundParameters
@@ -175,7 +172,7 @@ function Enable-CippConsoleLogging {
175172

176173
# Send to telemetry
177174
$message = if ($Object) { ($Object | Out-String).Trim() } else { '' }
178-
& $global:SendCippConsoleLog -Message $message -Level ([CippConsoleLogLevel]::Information)
175+
& $global:SendCippConsoleLog -Message $message -Level 'Information'
179176

180177
# Call original function
181178
Microsoft.PowerShell.Utility\Write-Host @PSBoundParameters

0 commit comments

Comments
 (0)