Modules/businessdev.ALbuild.Containers/Private/Get-BcContainerLogTail.ps1
|
function Get-BcContainerLogTail { <# .SYNOPSIS Returns the tail of a container's Docker logs, formatted for appending to an error message. .DESCRIPTION Internal helper used when a container fails to become ready. Captures the last lines of 'docker logs' (the BC generic image's start script output, where the real failure reason is reported) so the thrown error is actionable instead of just stating the container exited. Never throws: if the logs cannot be read it returns an empty string. #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Name, [ValidateRange(1, [int]::MaxValue)] [int] $Tail = 40, [string] $DockerExecutable = 'docker' ) try { $logs = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru ` -Arguments @('logs', '--tail', "$Tail", $Name) $text = (@($logs.StdOut, $logs.StdErr) | Where-Object { $_ } | ForEach-Object { $_.TrimEnd() }) -join [Environment]::NewLine if ([string]::IsNullOrWhiteSpace($text)) { return '' } return "$([Environment]::NewLine)Last $Tail log line(s) from '$Name':$([Environment]::NewLine)$($text.Trim())" } catch { return '' } } |