diff --git a/containers-toolkit/Private/CommonToolUtilities.psm1 b/containers-toolkit/Private/CommonToolUtilities.psm1 index 1131b28..c23d3b7 100644 --- a/containers-toolkit/Private/CommonToolUtilities.psm1 +++ b/containers-toolkit/Private/CommonToolUtilities.psm1 @@ -9,6 +9,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot Import-Module -Name "$ModuleParentPath\Private\UpdateEnvironmentPath.psm1" -Force +Import-Module -Name "$ModuleParentPath\Private\logger.psm1" -Force class ContainerTool { [ValidateNotNullOrEmpty()][string]$Feature @@ -925,3 +926,4 @@ Export-ModuleMember -Function Invoke-ServiceAction Export-ModuleMember -Function Test-ConfFileEmpty Export-ModuleMember -Function Uninstall-ProgramFiles Export-ModuleMember -Function Test-CheckSum +Export-ModuleMember -Function Write-CTK* \ No newline at end of file diff --git a/containers-toolkit/Private/UpdateEnvironmentPath.psm1 b/containers-toolkit/Private/UpdateEnvironmentPath.psm1 index 4e0353d..919dac0 100644 --- a/containers-toolkit/Private/UpdateEnvironmentPath.psm1 +++ b/containers-toolkit/Private/UpdateEnvironmentPath.psm1 @@ -6,6 +6,9 @@ # # ########################################################################### +$ModuleParentPath = Split-Path -Parent $PSScriptRoot +Import-Module -Name "$ModuleParentPath\Private\logger.psm1" -Force + function Update-EnvironmentPath { [CmdletBinding( SupportsShouldProcess = $true @@ -57,7 +60,7 @@ function Update-EnvironmentPath { } if ($pathChanged) { - Write-Information -InformationAction Continue -MessageData "$ActionVerb $toAction in $PathType Environment Path" + Write-CTKInfo "$ActionVerb $toAction in $PathType Environment Path" # Get the updated path $updatedPath = switch ($Action) { @@ -68,7 +71,7 @@ function Update-EnvironmentPath { # For tests, we do not want to update the environment path if ($env:pester) { - Write-Debug "Skipping environment path update for tests" + Write-CTKDebug "Skipping environment path update for tests" return $updatedPath } diff --git a/containers-toolkit/Private/logger.psm1 b/containers-toolkit/Private/logger.psm1 new file mode 100644 index 0000000..d6691f8 --- /dev/null +++ b/containers-toolkit/Private/logger.psm1 @@ -0,0 +1,100 @@ + + +function Get-LogMessage { + param ( + [string]$Message, + [ValidateSet("DEBUG", "ERROR", "INFO", "WARNING")] + [string]$LogLevel = "INFO" + ) + $CallStack = Get-PSCallStack + # [0]: Get-LogMessage, [1]: New-LogMessage, [2]: Write-CTK* + $CallerFunction = $CallStack[3] + + $timestamp = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffK") + $CommandName = $CallerFunction.InvocationInfo.MyCommand + $ScriptLineNumber = $CallerFunction.ScriptLineNumber + return "[$timestamp] [${CommandName}:${ScriptLineNumber}] $($LogLevel.ToUpper()): $Message" +} + +function New-LogMessage { + param ( + [string]$Message, + [ValidateSet("DEBUG", "ERROR", "INFO", "WARNING")] + [string]$LogLevel = "INFO" + ) + Write-Host "LogLevel: $LogLevel" + Write-Host "Message: $Message" + $parsedMessage = Get-LogMessage -Message $Message -LogLevel $LogLevel + switch ($LogLevel) { + "DEBUG" { + Write-Debug $parsedMessage + } + "ERROR" { + $EventID = 5000 + $eventType = [System.Diagnostics.EventLogEntryType]::Error + Write-Error $parsedMessage + } + "INFO" { + $EventID = 1000 + $eventType = [System.Diagnostics.EventLogEntryType]::Information + Write-Information $parsedMessage -InformationAction Continue + } + "WARNING" { + $EventID = 4000 + $eventType = [System.Diagnostics.EventLogEntryType]::Warning + Write-Warning $parsedMessage + } + } + + # User-defined environment variable to skip logging + if ($env:SKIP_CTK_LOGGING) { + return + } + + # Log to file if CTK_LOG_FILE is set + if ($env:CTK_LOG_FILE) { + $parsedMessage | Out-File -FilePath $env:CTK_LOG_FILE -Append + return + } + + # Default: Log to event log (non-DEBUG messages) + if ($LogLevel -ne "DEBUG") { + $eventLog = "Application" + $Source = "Containers-Toolkit" + # Create the event log source if it doesn't exist + if (-not [System.Diagnostics.EventLog]::SourceExists($Source)) { + New-EventLog -LogName $eventLog -Source $Source + } + Write-EventLog -LogName $eventLog -Source $Source -EventId $EventID -EntryType $eventType -Message $parsedMessage + } +} + +function Write-CTKDebug { + param ( + [string]$Message + ) + New-LogMessage -Message $Message -LogLevel "DEBUG" +} + +function Write-CTKError { + param ( + [string]$Message + ) + New-LogMessage -Message $Message -LogLevel "ERROR" +} + +function Write-CTKInfo { + param ( + [string]$Message + ) + New-LogMessage -Message $Message -LogLevel "INFO" +} +function Write-CTKWarning { + param ( + [string]$Message + ) + New-LogMessage -Message $Message -LogLevel "WARNING" +} + + +Export-ModuleMember -Function Write-CTK*