Modules/businessdev.ALbuild.Core/Public/Write-ALbuildLog.ps1
|
function Write-ALbuildLog { <# .SYNOPSIS Writes a structured ALbuild log message. .DESCRIPTION Emits a consistently formatted log line. When running inside Azure DevOps (TF_BUILD set), warnings and errors additionally emit the corresponding "##vso[task.logissue]" logging commands so they surface in the pipeline summary. Verbose and Debug levels honour the caller's $VerbosePreference / $DebugPreference instead of writing to the host. .PARAMETER Message The text to log. Accepts pipeline input and may be empty (blank line). .PARAMETER Level Severity: Verbose, Debug, Information (default), Success, Warning or Error. .EXAMPLE Write-ALbuildLog 'Creating container...' .EXAMPLE Write-ALbuildLog -Level Warning 'Artifact cache is stale.' #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Write-ALbuildLog is the module''s logging surface and must always render in pipeline logs.')] [CmdletBinding()] param( [Parameter(Position = 0, ValueFromPipeline)] [AllowEmptyString()] [AllowNull()] [string] $Message = '', [Parameter(Position = 1)] [ValidateSet('Verbose', 'Debug', 'Information', 'Success', 'Warning', 'Error')] [string] $Level = 'Information' ) process { $text = if ($null -eq $Message) { '' } else { $Message } $inAdo = -not [string]::IsNullOrEmpty($env:TF_BUILD) switch ($Level) { 'Verbose' { Write-Verbose $text; return } 'Debug' { Write-Debug $text; return } 'Warning' { if ($inAdo) { Write-Host "##vso[task.logissue type=warning]$text" } Write-Host $text -ForegroundColor Yellow return } 'Error' { if ($inAdo) { Write-Host "##vso[task.logissue type=error]$text" } Write-Host $text -ForegroundColor Red return } 'Success' { Write-Host $text -ForegroundColor Green return } default { Write-Host $text } } } } |