diff --git a/.github/workflows/new-logentry.yml b/.github/workflows/new-logentry.yml new file mode 100644 index 0000000..19b37c1 --- /dev/null +++ b/.github/workflows/new-logentry.yml @@ -0,0 +1,78 @@ +name: New-LogEntry + +on: + push: + branches: [master] + paths: + - 'New-LogEntry/**' + - '.github/workflows/new-logentry.yml' + pull_request: + paths: + - 'New-LogEntry/**' + - '.github/workflows/new-logentry.yml' + workflow_dispatch: + +permissions: + contents: read + +jobs: + test: + name: Pester (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + runs-on: ${{ matrix.os }} + timeout-minutes: 15 + defaults: + run: + shell: pwsh + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + + - name: Check runtime and script syntax + run: | + $ErrorActionPreference = 'Stop' + $PSVersionTable + if ($PSVersionTable.PSVersion -lt [version]'7.1') { + throw 'PowerShell 7.1 or later is required.' + } + foreach ($file in Get-ChildItem ./New-LogEntry -Filter *.ps1 -Recurse) { + $tokens = $null + $parseErrors = $null + $null = [System.Management.Automation.Language.Parser]::ParseFile( + $file.FullName, [ref]$tokens, [ref]$parseErrors + ) + if ($parseErrors.Count -gt 0) { + throw ($parseErrors | Out-String) + } + } + + - name: Install Pester + run: | + $ErrorActionPreference = 'Stop' + Install-Module Pester -RequiredVersion 5.7.1 -Scope CurrentUser -Force -SkipPublisherCheck -Repository PSGallery + + - name: Smoke test loader + run: | + $ErrorActionPreference = 'Stop' + . ./New-LogEntry/New-LogEntry.ps1 + $null = Get-Command New-LogEntry -CommandType Function -ErrorAction Stop + New-LogEntry -LogMessage 'CI smoke test' -BufferOnly + $entries = @(New-LogEntry -GetBuffer) + if ($entries.Count -ne 1 -or $entries[0] -notmatch '\[INFO\]: CI smoke test$') { + throw 'Loader smoke test failed.' + } + New-LogEntry -ClearBuffer + + - name: Run Pester tests + run: | + $ErrorActionPreference = 'Stop' + Import-Module Pester -RequiredVersion 5.7.1 -Force + $configuration = New-PesterConfiguration + $configuration.Run.Path = './New-LogEntry/Tests' + $configuration.Run.Exit = $true + $configuration.Output.Verbosity = 'Detailed' + Invoke-Pester -Configuration $configuration diff --git a/New-LogEntry/New-LogEntry.ps1 b/New-LogEntry/New-LogEntry.ps1 index 8c25353..f7240af 100644 --- a/New-LogEntry/New-LogEntry.ps1 +++ b/New-LogEntry/New-LogEntry.ps1 @@ -1,3 +1,5 @@ +#Requires -Version 7.1 + $script:NewLogEntryRoot = $PSScriptRoot if ([string]::IsNullOrWhiteSpace($script:NewLogEntryRoot)) diff --git a/New-LogEntry/README.md b/New-LogEntry/README.md index ef2cce3..fb7a061 100644 --- a/New-LogEntry/README.md +++ b/New-LogEntry/README.md @@ -1,15 +1,15 @@ # New-LogEntry -`New-LogEntry` is a PowerShell 7+ logging function designed for production automation and troubleshooting. It writes timestamped log entries to files and the console, supports in-memory buffering, handles concurrent writers safely, accepts pipeline input, and can redact common secrets before log data leaves the process. +`New-LogEntry` is a PowerShell 7.1+ logging function designed for production automation and troubleshooting. It writes timestamped log entries to files and the console, supports in-memory buffering, handles concurrent writers safely, accepts pipeline input, and can redact common secrets before log data leaves the process. The function began as a small logging helper and has since been rewritten around a clearer public API, private helper functions, cross-platform file handling, and automated Pester tests. ## Requirements -- PowerShell 7 or later +- PowerShell 7.1 or later - Pester 5 or later to run the test suite -The current implementation targets PowerShell 7+ on Windows, Linux, and macOS. Windows PowerShell 5.1 and earlier versions are not a compatibility target for this release. +The current implementation targets PowerShell 7.1+ on Windows, Linux, and macOS. Windows PowerShell 5.1 and earlier versions are not a compatibility target for this release. ## Repository layout @@ -75,10 +75,10 @@ Normal informational console output uses `Write-Host` so log messages do not une When caller code needs the formatted entry as output, use `-PassThru`: ```powershell -$entry = New-LogEntry \ - -LogMessage 'Return this line' \ - -LogFilePath './application.log' \ - -NoConsole \ +$entry = New-LogEntry ` + -LogMessage 'Return this line' ` + -LogFilePath './application.log' ` + -NoConsole ` -PassThru ``` @@ -134,9 +134,9 @@ File writes are protected by a named system mutex derived from the resolved log- The default lock timeout is 30 seconds and can be changed with `-LockTimeoutSeconds`: ```powershell -New-LogEntry \ - -LogMessage 'Concurrent-safe write' \ - -LogFilePath './application.log' \ +New-LogEntry ` + -LogMessage 'Concurrent-safe write' ` + -LogFilePath './application.log' ` -LockTimeoutSeconds 10 ``` @@ -149,28 +149,28 @@ Secret redaction is opt-in. Use `-RedactSecrets` to remove common credential patterns before entries are buffered, written, displayed, or returned: ```powershell -New-LogEntry \ - -LogMessage 'Authorization: Bearer abc123 password=SuperSecret' \ - -LogFilePath './application.log' \ +New-LogEntry ` + -LogMessage 'Authorization: Bearer abc123 password=SuperSecret' ` + -LogFilePath './application.log' ` -RedactSecrets ``` Custom regular expressions can be supplied with `-RedactPattern`: ```powershell -New-LogEntry \ - -LogMessage 'sessionId=abc123 visible=yes' \ - -LogFilePath './application.log' \ +New-LogEntry ` + -LogMessage 'sessionId=abc123 visible=yes' ` + -LogFilePath './application.log' ` -RedactPattern 'sessionId=\S+' ``` Use `-RedactionText` to change the replacement text: ```powershell -New-LogEntry \ - -LogMessage 'token=abc123' \ - -LogFilePath './application.log' \ - -RedactSecrets \ +New-LogEntry ` + -LogMessage 'token=abc123' ` + -LogFilePath './application.log' ` + -RedactSecrets ` -RedactionText '' ``` @@ -219,3 +219,9 @@ The full development history is documented in [CHANGELOG.md](./CHANGELOG.md). ## License Released under the [MIT License](./LICENSE). + +## Continuous integration + +The New-LogEntry workflow runs the Pester suite on Windows, Linux, and macOS using each GitHub-hosted runner's installed PowerShell. It also checks PowerShell syntax and performs a separate loader smoke test. Pester is pinned to version 5.7.1. + +PowerShell 7.1 is the API minimum because the mutex helper uses .NET 5 APIs. CI tests the runner versions, not every historical PowerShell release.