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
78 changes: 78 additions & 0 deletions .github/workflows/new-logentry.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions New-LogEntry/New-LogEntry.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#Requires -Version 7.1

$script:NewLogEntryRoot = $PSScriptRoot

if ([string]::IsNullOrWhiteSpace($script:NewLogEntryRoot))
Expand Down
46 changes: 26 additions & 20 deletions New-LogEntry/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
```

Expand Down Expand Up @@ -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
```

Expand All @@ -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 '<secret>'
```

Expand Down Expand Up @@ -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.
Loading