Skip to content

persist logs updateenvironmentpath.psm1 #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions containers-toolkit/Private/CommonToolUtilities.psm1
Original file line number Diff line number Diff line change
@@ -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*
7 changes: 5 additions & 2 deletions containers-toolkit/Private/UpdateEnvironmentPath.psm1
Original file line number Diff line number Diff line change
@@ -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
}

100 changes: 100 additions & 0 deletions containers-toolkit/Private/logger.psm1
Original file line number Diff line number Diff line change
@@ -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*