|
| 1 | +class Logger { |
| 2 | + static [string] $EventSource = "Containers-Toolkit" |
| 3 | + static [string] $EventLogName = "Application" |
| 4 | + static [string] $LogFile |
| 5 | + static [bool] $Quiet |
| 6 | + static [string] $MinLevel |
| 7 | + |
| 8 | + static [hashtable] $LogLevelRank = @{ |
| 9 | + "DEBUG" = 1 |
| 10 | + "INFO" = 2 |
| 11 | + "WARNING" = 3 |
| 12 | + "ERROR" = 4 |
| 13 | + "FATAL" = 5 |
| 14 | + } |
| 15 | + |
| 16 | + # Set minimum log level from environment variable: CTK_LOG_LEVEL or DebugPreference |
| 17 | + static [string] GetMinLevel() { |
| 18 | + try { |
| 19 | + $DebugPref = Get-Variable -Name DebugPreference -Scope Global -ValueOnly |
| 20 | + if ($DebugPref -ne "SilentlyContinue") { |
| 21 | + return "DEBUG" |
| 22 | + } |
| 23 | + elseif ($env:CTK_LOG_LEVEL) { |
| 24 | + return $env:CTK_LOG_LEVEL.ToUpper() |
| 25 | + } |
| 26 | + else { |
| 27 | + return "INFO" |
| 28 | + } |
| 29 | + } |
| 30 | + catch { |
| 31 | + return "INFO" |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + # Set log file path from environment variable: CTK_LOG_FILE |
| 36 | + static [string] GetLogFile() { |
| 37 | + try { |
| 38 | + $fileName = $env:CTK_LOG_FILE |
| 39 | + } |
| 40 | + catch { |
| 41 | + $fileName = $null |
| 42 | + } |
| 43 | + return $fileName |
| 44 | + } |
| 45 | + |
| 46 | + # Set quiet mode from environment variable: SKIP_CTK_LOGGING |
| 47 | + # User-defined environment variable to skip logging. Equivalent to --quiet. |
| 48 | + # If set, only DEBUG messages are logged to the terminal. |
| 49 | + # If not set, all messages are logged to the terminal and to the event log. |
| 50 | + static [bool] GetQuiet() { |
| 51 | + try { |
| 52 | + $quietValue = $env:SKIP_CTK_LOGGING |
| 53 | + return if ($quietValue) { [bool]::Parse($quietValue) } else { $false } |
| 54 | + } |
| 55 | + catch { |
| 56 | + return $false |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + # Check if the log level is greater than or equal to the minimum log level |
| 61 | + static [bool] ShouldLog([string] $Level) { |
| 62 | + return [Logger]::LogLevelRank[$Level.ToUpper()] -ge [Logger]::LogLevelRank[[Logger]::MinLevel] |
| 63 | + } |
| 64 | + |
| 65 | + # Format the message for logging |
| 66 | + static [string] FormatMessage([object] $message) { |
| 67 | + if ($null -eq $message) { |
| 68 | + return "[null]" |
| 69 | + } |
| 70 | + |
| 71 | + if ($message -is [string]) { |
| 72 | + return $message |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + return $message | ConvertTo-Json -Depth 5 -Compress |
| 77 | + } |
| 78 | + catch { |
| 79 | + return $message.ToString() |
| 80 | + # $Message = $Message | Out-String |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + # Retrieve the function in the call stack that triggered the log |
| 85 | + static [pscustomobject] GetCallerFunction($CallStack) { |
| 86 | + $i = 3 |
| 87 | + $CallerFunction = $CallStack[$i] |
| 88 | + |
| 89 | + while ((-not $CallerFunction.Command) -and ($i -lt $CallStack.Count - 1)) { |
| 90 | + $i++ |
| 91 | + $CallerFunction = $CallStack[$i] |
| 92 | + } |
| 93 | + |
| 94 | + return $CallerFunction |
| 95 | + } |
| 96 | + |
| 97 | + # Generate a parsed log message from the log level and message |
| 98 | + static [string] GetLogMessage([string] $Level, [string] $Message) { |
| 99 | + $CallStack = Get-PSCallStack |
| 100 | + $Caller = [Logger]::GetCallerFunction($CallStack) |
| 101 | + |
| 102 | + $timestamp = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffK") |
| 103 | + $cmd = $Caller.InvocationInfo.MyCommand |
| 104 | + $line = $Caller.ScriptLineNumber |
| 105 | + |
| 106 | + # Add padding to the message based on the difference between the longest level name and the current level |
| 107 | + $padding = ([Logger]::LogLevelRank.Keys | Measure-Object -Maximum Length).Maximum - $Level.Length |
| 108 | + |
| 109 | + return "$($Level.ToUpper()):$(" " * $padding)[$timestamp] [${cmd}:${line}]: $Message" |
| 110 | + } |
| 111 | + |
| 112 | + # Write log messages to the Windows Event Log |
| 113 | + static [void] WriteToEventLog([string] $Level, [string] $Message) { |
| 114 | + # Create the event log source if it doesn't exist |
| 115 | + if (-not [System.Diagnostics.EventLog]::SourceExists($([Logger]::EventLogName))) { |
| 116 | + Write-Debug "Creating event log source: { LogName: $([Logger]::EventLogName), Source: $([Logger]::EventSource) }" |
| 117 | + New-EventLog -LogName $([Logger]::EventLogName) -Source $([Logger]::EventSource) |
| 118 | + } |
| 119 | + |
| 120 | + $entryType = switch ($Level) { |
| 121 | + "INFO" { [System.Diagnostics.EventLogEntryType]::Information } |
| 122 | + "WARNING" { [System.Diagnostics.EventLogEntryType]::Warning } |
| 123 | + "ERROR" { [System.Diagnostics.EventLogEntryType]::Error } |
| 124 | + "FATAL" { [System.Diagnostics.EventLogEntryType]::Error } |
| 125 | + default { throw [System.NotImplementedException]("Invalid log level: $Level") } |
| 126 | + } |
| 127 | + |
| 128 | + $eventId = switch ($Level) { |
| 129 | + "INFO" { 1000 } |
| 130 | + "WARNING" { 4000 } |
| 131 | + "ERROR" { 5000 } |
| 132 | + "FATAL" { 6000 } |
| 133 | + default { throw [System.NotImplementedException]("Invalid log level: $Level") } |
| 134 | + } |
| 135 | + |
| 136 | + try { |
| 137 | + Write-EventLog -LogName $([Logger]::EventLogName) ` |
| 138 | + -Source $([Logger]::EventSource) ` |
| 139 | + -EntryType $entryType ` |
| 140 | + -EventId $eventId ` |
| 141 | + -Message $Message |
| 142 | + } |
| 143 | + catch { |
| 144 | + # Fallback: write warning but continue |
| 145 | + Write-Warning "Failed to write to event log: $_" |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + # Write log messages to the console and/or event log (or file) |
| 150 | + # This is the main logging function that handles all log levels |
| 151 | + static [void] Write([string] $Level, [object] $Message) { |
| 152 | + # Set values |
| 153 | + [Logger]::MinLevel = [Logger]::GetMinLevel() |
| 154 | + [Logger]::LogFile = [Logger]::GetLogFile() |
| 155 | + [Logger]::Quiet = [Logger]::GetQuiet() |
| 156 | + |
| 157 | + $Level = $Level.ToUpper() |
| 158 | + |
| 159 | + # Minimum log level filtering: Only log messages that are at least as severe as the minimum level |
| 160 | + if (-not [Logger]::ShouldLog($Level)) { |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + # Convert the message to a string |
| 165 | + $formatedMessage = [Logger]::FormatMessage($message) |
| 166 | + |
| 167 | + # Generate the log message |
| 168 | + $parsedMessage = [Logger]::GetLogMessage($Level, $formatedMessage) |
| 169 | + |
| 170 | + # Default: Log to event log (non-DEBUG messages) |
| 171 | + if ($Level -ne "DEBUG") { |
| 172 | + [Logger]::WriteToEventLog($Level, $parsedMessage) |
| 173 | + } |
| 174 | + |
| 175 | + # Log to file if CTK_LOG_FILE is set |
| 176 | + if ([Logger]::LogFile) { |
| 177 | + Add-Content -Path [Logger]::LogFile -Value $parsedMessage |
| 178 | + } |
| 179 | + |
| 180 | + # If true, only DEBUG messages are logged to the terminal |
| 181 | + # else, all messages are logged to the terminal and to the event log. |
| 182 | + if ([Logger]::Quiet -and $Level -ne "DEBUG") { |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + # Console output |
| 187 | + switch ($Level) { |
| 188 | + "FATAL" { [Console]::Error.WriteLine($parsedMessage); throw $Message } |
| 189 | + "ERROR" { [Console]::Error.WriteLine($parsedMessage) } |
| 190 | + default { [Console]::WriteLine($parsedMessage) } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + static [void] Fatal([object] $Message) { [Logger]::Write("FATAL", $Message) } |
| 195 | + static [void] Error([object] $Message) { [Logger]::Write("ERROR", $Message) } |
| 196 | + static [void] Warning([object] $Message) { [Logger]::Write("WARNING", $Message) } |
| 197 | + static [void] Info([object] $Message) { [Logger]::Write("INFO", $Message) } |
| 198 | + static [void] Debug([object] $Message) { [Logger]::Write("DEBUG", $Message) } |
| 199 | + static [void] Log([string] $Level = "INFO", [string] $Message) { [Logger]::Write($Level, $Message) } |
| 200 | +} |
0 commit comments