Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/writelog.tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: WriteLog Pester Tests

on:
push:
branches:
- dev
paths:
- 'WriteLog/**.ps1'
- 'WriteLog/**.psm1'

workflow_dispatch:

jobs:
test:
strategy:
matrix:
os: [ windows-latest, ubuntu-latest ]
shell: [ pwsh ]
include:
- os: windows-latest
shell: powershell


runs-on: ${{ matrix.os }}

steps:
- name: Check out repository code
uses: actions/checkout@v5

- name: Run Pester Tests (powershell on ${{ matrix.os }})
if: matrix.shell == 'powershell'
env:
CI: true
shell: powershell
run: Invoke-Pester -Path WriteLog/Tests/ -Output Detailed -Passthru

- name: Run Pester Tests (pwsh on ${{ matrix.os }})
if: matrix.shell != 'powershell'
env:
CI: true
shell: pwsh
run: Invoke-Pester -Path WriteLog/Tests/ -Output Detailed -Passthru
30 changes: 29 additions & 1 deletion WriteLog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,32 @@ or delete them with one of these options
```PowerShell
Remove-AdditionalLogfile -Name "Textfile_1"
Remove-AdditionalLogfile -Path "C:\Temp\test.txt"
```
```

# Resizing log files

Log files can grow large over time. Use `Resize-Logfile` to trim a log file down to its last n lines.

## Resize the main log file

```PowerShell
Resize-Logfile -RowsToKeep 200000
```

Rewrites `$Script:logfile` keeping only the last 200000 lines.

## Resize a specific log file

```PowerShell
Resize-Logfile -RowsToKeep 200000 -Path "C:\Logs\myapp.log"
```

The `-Path` parameter lets you target any log file directly, independent of `$Script:logfile`.

## Resize all registered log files at once

```PowerShell
Resize-Logfile -RowsToKeep 200000 -All
```

Resizes the main log file and all additional textfile log files registered via `Add-AdditionalLogfile` in one call.
Empty file removed WriteLog/Tests/Write-Log.test.ps1
Empty file.
19 changes: 8 additions & 11 deletions WriteLog/Tests/WriteLog.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ Describe "Set-TimestampFormat / Get-TimestampFormat" {
$result | Should -Be "dd/MM/yyyy"
}

It "Throws on an invalid format string" {
{ Set-TimestampFormat -Format "NOT_A_%VALID_FORMAT_@@@@" } | Should -Throw
}

It "Accepts ISO 8601 format" {
{ Set-TimestampFormat -Format "yyyy-MM-ddTHH:mm:ssZ" } | Should -Not -Throw
Get-TimestampFormat | Should -Be "yyyy-MM-ddTHH:mm:ssZ"
Expand Down Expand Up @@ -195,25 +191,25 @@ Describe "Write-Log" {
}

It "Writes the severity INFO to the logfile" {
Write-Log -Message "Info message" -Severity ([LogSeverity]::INFO)
Write-Log -Message "Info message" -Severity INFO
$content = Get-Content -Path $script:testLogfile -Raw
$content | Should -Match "INFO"
}

It "Writes the severity WARNING to the logfile" {
Write-Log -Message "Warning message" -Severity ([LogSeverity]::WARNING) -WriteToHostToo $false
Write-Log -Message "Warning message" -Severity WARNING -WriteToHostToo $false
$content = Get-Content -Path $script:testLogfile -Raw
$content | Should -Match "WARNING"
}

It "Writes the severity ERROR to the logfile" {
Write-Log -Message "Error message" -Severity ([LogSeverity]::ERROR) -WriteToHostToo $false -ErrorAction SilentlyContinue
Write-Log -Message "Error message" -Severity ERROR -WriteToHostToo $false -ErrorAction SilentlyContinue
$content = Get-Content -Path $script:testLogfile -Raw
$content | Should -Match "ERROR"
}

It "Writes the severity VERBOSE to the logfile" {
Write-Log -Message "Verbose message" -Severity ([LogSeverity]::VERBOSE)
Write-Log -Message "Verbose message" -Severity VERBOSE
$content = Get-Content -Path $script:testLogfile -Raw
$content | Should -Match "VERBOSE"
}
Expand All @@ -231,11 +227,11 @@ Describe "Write-Log" {
}

It "Includes the process ID in the log entry" {
$pid = "my-test-pid"
Set-ProcessId -Id $pid
$newProcessId = "my-test-pid"
Set-ProcessId -Id $newProcessId
Write-Log -Message "Check PID"
$content = Get-Content -Path $script:testLogfile -Raw
$content | Should -Match $pid
$content | Should -Match $newProcessId
}

It "Includes the timestamp in the log entry" {
Expand Down Expand Up @@ -361,6 +357,7 @@ Describe "Resize-Logfile" {
BeforeEach {
Import-Module "$PSScriptRoot/../WriteLog" -Force
$script:testLogfile = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "$([guid]::NewGuid()).log"
write-verbose $script:testLogfile -verbose
Set-Logfile -Path $script:testLogfile
}

Expand Down
4 changes: 0 additions & 4 deletions WriteLog/Tests/test.ps1

This file was deleted.

45 changes: 45 additions & 0 deletions WriteLog/WriteLog/Private/Resize-SingleLogfile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Function Resize-SingleLogfile {

[cmdletbinding()]
param(

[Parameter(Mandatory=$true)]
[String]$Path

,[Parameter(Mandatory=$true)]
[int]$RowsToKeep

)

Begin {

If ( ( Test-Path -Path $Path -IsValid ) -eq $false ) {
Write-Error -Message "The path '$( $Path )' is invalid."
return
}

If ( ( Test-Path -Path $Path ) -eq $false ) {
Write-Verbose -Message "Skipping '$( $Path )' - file does not exist yet."
return
}

}

Process {

# Create a temporary file
$tmpdir = Get-TemporaryPath
$tempFile = Join-Path -Path $tmpdir -ChildPath "$( [guid]::newguid().toString() ).tmp"

# Write only last lines to the new file
Get-Content -Tail $RowsToKeep -Encoding utf8 -Path $Path | Set-Content -Path $tempFile -Encoding utf8

# Delete original file and replace with trimmed version
Remove-Item -Path $Path
Move-Item -Path $tempFile -Destination $Path

Write-Verbose -Message "Resized '$( $Path )' to last $( $RowsToKeep ) lines"

}

}
2 changes: 1 addition & 1 deletion WriteLog/WriteLog/Public/Get-AdditionalLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ function Get-AdditionalLog {
[CmdletBinding()]
param()
process {
$Script:additionalLogs
, $Script:additionalLogs
}
}
65 changes: 38 additions & 27 deletions WriteLog/WriteLog/Public/Resize-Logfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ Function Resize-Logfile {
.DESCRIPTION
The logfile, that is defined by $logfile or $Script:logfile needs to be cleaned from time to time.
So this function rewrites the file with the last (most current) n lines.
Optionally a specific path can be provided via -Path, or all registered log files can be resized at once with -All.

.PARAMETER RowsToKeep
The number of lines you want to keep

.PARAMETER Path
Optional path to a specific logfile to resize. If omitted, the main logfile ($Script:logfile) is used.

.PARAMETER All
If set, resizes all registered log files: the main logfile and all additional textfile log files.

.EXAMPLE
Resize-Logfile -RowsToKeep 200000

.EXAMPLE
Clean-Logfile -RowsToKeep 200000
Resize-Logfile -RowsToKeep 200000 -Path "C:\Logs\myapp.log"

.EXAMPLE
Resize-Logfile -RowsToKeep 200000 -All

.INPUTS
Int
Expand All @@ -27,41 +40,39 @@ Function Resize-Logfile {

[cmdletbinding()]
param(
[Parameter(Mandatory=$true)][int]$RowsToKeep #= 200000
[Parameter(Mandatory=$true)][int]$RowsToKeep
,[Parameter(Mandatory=$false)][String]$Path = ""
,[Parameter(Mandatory=$false)][Switch]$All
)

# TODO [ ] use input path rather than a variable?

If ( $null -eq $logfile ) {
If ( -not [String]::IsNullOrWhiteSpace( $Path ) ) {

Write-Warning -Message "There is no variable '`$logfile' present on 'Script' scope"
Write-Warning -Message "Please define a path in '`$logfile' or use 'Write-Log' once"
# Explicit path provided — resize only that file
Resize-SingleLogfile -Path $Path -RowsToKeep $RowsToKeep

} else {
} ElseIf ( $All ) {

# Testing the path
If ( ( Test-Path -Path $logfile -IsValid ) -eq $false ) {
Write-Error -Message "Invalid variable '`$logfile'. The path '$( $logfile )' is invalid."
# Resize main logfile
If ( $null -eq $Script:logfile ) {
Write-Warning -Message "There is no variable '`$logfile' present on 'Script' scope"
Write-Warning -Message "Please define a path in '`$logfile' or use 'Write-Log' once"
} else {
Resize-SingleLogfile -Path $Script:logfile -RowsToKeep $RowsToKeep
}

# [ ] TODO maybe implement another parameter to input date instead of no of rows, use streamreader for this instead
# [Datetime]::ParseExact("20221027130112","yyyyMMddHHmmss",$null)

# Create a temporary file
$tmpdir = Get-TemporaryPath
$tempFile = Join-Path -Path $tmpdir -ChildPath "$( [guid]::newguid().toString() ).tmp" #New-TemporaryFile

# Write only last lines to the new file
Get-Content -Tail $RowsToKeep -Encoding utf8 -Path $Script:logfile | Set-Content -path $tempFile.FullName -Encoding utf8

# delete original file
If ( (Test-Path -Path $logfile) -eq $true ) {
Remove-Item $logfile
}
# Resize all additional textfile log files
$Script:additionalLogs | Where-Object { $_.Type -eq "textfile" } | ForEach-Object {
Resize-SingleLogfile -Path $_.Options.Path -RowsToKeep $RowsToKeep
}

# move file to new location
Move-Item -Path $tempFile.FullName -Destination $logfile
} Else {

# Default: resize the main logfile
If ( $null -eq $Script:logfile ) {
Write-Warning -Message "There is no variable '`$logfile' present on 'Script' scope"
Write-Warning -Message "Please define a path in '`$logfile' or use 'Write-Log' once"
} else {
Resize-SingleLogfile -Path $Script:logfile -RowsToKeep $RowsToKeep
}

}
Expand Down
5 changes: 4 additions & 1 deletion WriteLog/WriteLog/WriteLog.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RootModule = 'WriteLog.psm1'

# Die Versionsnummer dieses Moduls
ModuleVersion = '0.10.1'
ModuleVersion = '0.10.3'

# Unterstützte PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -148,6 +148,9 @@ PrivateData = @{

# 'ReleaseNotes' des Moduls
ReleaseNotes = '
0.10.3 Fixed a scalar output problem for PowerShell 5.1 when calling Get-AdditionalLog with one entry
0.10.2 Added pester tests for this module
Rewritten Resize-Logfile after failed pester tests, also support now resizing of single files or all logs
0.10.1 Feature: Added a new Switch for WriteLog to allow logging in UTC rather than only local timestamp
Fix: Small fix for Linux/MacOS compatibility for getting the executing user and elevation status
0.10.0 Feature: Additional log output format (Set-LogFormat / Get-LogFormat) and timestamp format (Set-TimeStampFormat / Get-TimestampFormat)
Expand Down