K.PSGallery.LoggingModule.psm1

<#
.SYNOPSIS
    Flexibles Logging-Modul für PowerShell mit farbiger Ausgabe.
 
.DESCRIPTION
    Dieses Modul stellt Logging-Funktionen für verschiedene Log-Level bereit und gibt farbige Ausgaben auf die Konsole aus.
    Später kann der Logging-Mechanismus durch andere Ausgabemedien wie Eventlog, Datei oder Azure angepasst werden.
 
.NOTES
    Version: 1.1.0
    PowerShell-Version: 5.1+
#>


# Basis-Logging-Funktion, die je nach Provider überschrieben werden kann
Function Write-LogEntry {
    Param (
        [Parameter(Mandatory = $true)]
        [ValidateSet("DEBUG", "INFO", "WARNING", "ERROR", "FATAL", "TASKSUCCESS", "TASKFAILED")]
        [string]$Level,
        
        [Parameter(Mandatory = $true)]
        [string]$Message,

        [Parameter(Mandatory = $false)]
        [string]$Context = ""

    )
    
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $prefix = "[$timestamp] [$Level] - "


    $logEntry = "$prefix$Message"
    
    # Format the log entry based on the level
    switch ($Level) {
        "DEBUG" { $color = "Gray" }
        "INFO" { $color = "Blue" }
        "WARNING" { $color = "Yellow" }
        "ERROR" { $color = "Red" }
        "FATAL" { $color = "DarkRed" }
        "TASKSUCCESS" { 
            $logEntry = "✅ SUCCESS: $logEntry `n ================================================="
            $color = "Green"
        }
        "TASKFAILED" { 
            $logEntry = "❌ ERROR: $logEntry `n xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            $color = "Red"
        }
        default { $color = "White" }
    }

    [void](Write-Host $logEntry -ForegroundColor $color)

    # Add context information if provided - indented and visually connected to the main message
    if ($Context -and $Context.Trim() -ne "") {
        $indent = " " * $prefix.Length
        foreach ($line in $Context -split "`n") {
            [void](Write-Host "$indent▶ $line" -ForegroundColor $color)
        }
    }
}

Function Write-DebugLog {
    Param (
        [string]$Message,
        [string]$Context = ""
    )

    [void](Write-LogEntry -Level "DEBUG" -Message $Message -Context $Context)
}

Function Write-InfoLog {
    Param (
        [string]$Message,
        [string]$Context = ""
    )   

    [void](Write-LogEntry -Level "INFO" -Message $Message -Context $Context)
}

Function Write-WarningLog {
    Param (
        [string]$Message,
        [string]$Context = ""
    )

    [void](Write-LogEntry -Level "WARNING" -Message $Message -Context $Context)
}

Function Write-ErrorLog {
    Param (
        [string]$Message,
        [string]$Context = ""
    )

   [void](Write-LogEntry -Level "ERROR" -Message $Message -Context $Context)
}

Function Write-FatalLog {
    Param (
        [string]$Message,
        [string]$Context = ""
    )

    [void](Write-LogEntry -Level "FATAL" -Message $Message -Context $Context)
}

Function Write-TaskSuccessLog {
    Param (
        [string]$Message,
        [string]$Context = ""
    )

    [void](Write-LogEntry -Level "TASKSUCCESS" -Message $Message -Context $Context)
}

Function Write-TaskFailLog {
    Param (
        [string]$Message,
        [string]$Context = ""
    )

    [void](Write-LogEntry -Level "TASKFAILED" -Message $Message -Context $Context)
}