Skip to content

Instantly share code, notes, and snippets.

@vitaliitylyk
Last active October 15, 2019 08:31
Show Gist options
  • Save vitaliitylyk/ce10fdce3dcd3aec903a89ee11909599 to your computer and use it in GitHub Desktop.
Save vitaliitylyk/ce10fdce3dcd3aec903a89ee11909599 to your computer and use it in GitHub Desktop.
The script is tailing Sitecore log files to the attached console. It is meant to be used within an entry point in Docker container.
<#
.SYNOPSIS
Tails Sitecore logs to Docker console (STDOUT).
.PARAMETER LogsFolder
Path to a folder where Sitecore logs are located.
.PARAMETER Filter
Log file name filter. Can contain wildcards "*".
.PARAMETER FileEndMarker
Marker of the end of the file.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$LogsFolder = "$env:SITEPATH\App_Data\logs"
,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$Filter = "log.*.txt"
,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$FileEndMarker = "------END------"
)
$now = Get-Date;
If(!(Test-Path $LogsFolder))
{
# This is the first time Sitecore starts up
Write-Host ("### Logs folder not found => creating an empty folder $LogsFolder...");
New-Item -ItemType Directory -Force -Path $LogsFolder
}
while($true)
{
Start-Sleep -Milliseconds 1000;
$latestLog = Get-ChildItem -Path $LogsFolder | Where-Object {$_.Name -like $Filter} | Sort-Object Name -Descending | Select-Object -First 1;
# Wait while new log file created, ignore old log files
if (-not $latestLog -or $latestLog.LastWriteTime -lt $now)
{
continue;
}
$lastLine = Get-Content -Tail 1 $latestLog.FullName;
if ($lastLine -Match $FileEndMarker)
{
continue;
}
Write-Host ("### Tailing $latestLog...");
Get-Content $latestLog.FullName -Wait | ForEach-Object {$_ ; if($_ -Match $FileEndMarker){continue} };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment