Skip to content

Commit

Permalink
Fix for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
PrzemyslawKlys committed Sep 2, 2024
1 parent f47a2ba commit 66793ca
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions Private/Set-LoggingCapabilities.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[CmdletBinding()]
param(
[parameter()][string] $LogPath,
[parameter(Mandatory)][string] $ScriptPath,
[parameter()][string] $ScriptPath,
[parameter(Mandatory)][int] $LogMaximum,
[switch] $ShowTime,
[string] $TimeFormat
Expand All @@ -20,22 +20,26 @@
$null = New-Item -Path $FolderPath -ItemType Directory -Force -WhatIf:$false
}
if ($LogMaximum -gt 0) {
$ScriptPathFolder = [io.path]::GetDirectoryName($ScriptPath)
if ($ScriptPathFolder -eq $FolderPath) {
Write-Color -Text '[i] ', "LogMaximum is set to ", $LogMaximum, " but log files are in the same folder as the script. Cleanup disabled." -Color Yellow, White, DarkCyan, White
return
}
$CurrentLogs = Get-ChildItem -LiteralPath $FolderPath | Sort-Object -Property CreationTime -Descending | Select-Object -Skip $LogMaximum
if ($CurrentLogs) {
Write-Color -Text '[i] ', "Logs directory has more than ", $LogMaximum, " log files. Cleanup required..." -Color Yellow, DarkCyan, Red, DarkCyan
foreach ($Log in $CurrentLogs) {
try {
Remove-Item -LiteralPath $Log.FullName -Confirm:$false -WhatIf:$false
Write-Color -Text '[+] ', "Deleted ", "$($Log.FullName)" -Color Yellow, White, Green
} catch {
Write-Color -Text '[-] ', "Couldn't delete log file $($Log.FullName). Error: ', "$($_.Exception.Message) -Color Yellow, White, Red
if ($ScriptPath) {
$ScriptPathFolder = [io.path]::GetDirectoryName($ScriptPath)
if ($ScriptPathFolder -eq $FolderPath) {
Write-Color -Text '[i] ', "LogMaximum is set to ", $LogMaximum, " but log files are in the same folder as the script. Cleanup disabled." -Color Yellow, White, DarkCyan, White
return
}
$CurrentLogs = Get-ChildItem -LiteralPath $FolderPath | Sort-Object -Property CreationTime -Descending | Select-Object -Skip $LogMaximum
if ($CurrentLogs) {
Write-Color -Text '[i] ', "Logs directory has more than ", $LogMaximum, " log files. Cleanup required..." -Color Yellow, DarkCyan, Red, DarkCyan
foreach ($Log in $CurrentLogs) {
try {
Remove-Item -LiteralPath $Log.FullName -Confirm:$false -WhatIf:$false
Write-Color -Text '[+] ', "Deleted ", "$($Log.FullName)" -Color Yellow, White, Green
} catch {
Write-Color -Text '[-] ', "Couldn't delete log file $($Log.FullName). Error: ', "$($_.Exception.Message) -Color Yellow, White, Red
}
}
}
} else {
Write-Color -Text '[i] ', "LogMaximum is set to ", $LogMaximum, " but no script path detected. Most likely running interactively. Cleanup disabled." -Color Yellow, White, DarkCyan, White
}
} else {
Write-Color -Text '[i] ', "LogMaximum is set to 0 (Unlimited). No log files will be deleted." -Color Yellow, DarkCyan
Expand Down

0 comments on commit 66793ca

Please sign in to comment.