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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.1.0] Add -NoColor Switch Parameter

### Added

- `-NoColor` switch parameter to both `Read-ChocoLog` and `Get-ChocoLogEntry` functions
- Module-level variable `$script:ChocoLogNoColor` to control formatter behavior
- Colored output control in `ChocoLog.format.ps1xml` formatter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [alex] reported by reviewdog 🐶
Be careful with Colored, it’s profane in some cases colored retext-profanities

- Documentation and examples for the new parameter

### Fixed

- Undefined `$bg` variable reference in the original formatter

## [1.0.1] Add Missing Fatal

### Fixes
Expand Down
12 changes: 8 additions & 4 deletions ChocoLogParse/ChocoLog.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$style = if ($_.exitCode -eq 0) {
$PSStyle.Foreground.White
if ($script:ChocoLogNoColor) {
[String]::Format("{0}", $_.exitCode)
} else {
$PSStyle.Foreground.Yellow
$style = if ($_.exitCode -eq 0) {
$PSStyle.Foreground.White
} else {
$PSStyle.Foreground.Yellow
}
[String]::Format("{0}{1}{2}", $style, $_.exitCode, $PSStyle.Reset)
}
[String]::Format("{0}{1}{2}", $bg, $_.exitCode, $PSStyle.Reset)
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
Expand Down
2 changes: 1 addition & 1 deletion ChocoLogParse/ChocoLogParse.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'ChocoLogParse.psm1'

# Version number of this module.
ModuleVersion = '1.0.1'
ModuleVersion = '1.1.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
3 changes: 3 additions & 0 deletions ChocoLogParse/ChocoLogParse.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
}
}

# Initialize module variable for color control (default to colored output)
$script:ChocoLogNoColor = $false

# Add our custom formatter that needed classes first
$format = Join-Path -Path $PSScriptRoot -ChildPath 'ChocoLog.format.ps1xml'
Update-FormatData -PrependPath $format
Expand All @@ -23,7 +26,7 @@
[ChocoLog]
)
# Get the internal TypeAccelerators class to use its static methods.
$TypeAcceleratorsClass = [psobject].Assembly.GetType(

Check warning on line 29 in ChocoLogParse/ChocoLogParse.psm1

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (psobject) Suggestions: (isobject, isObject, project, object, subject)
'System.Management.Automation.TypeAccelerators'
)
# Ensure none of the types would clobber an existing type accelerator.
Expand Down
12 changes: 10 additions & 2 deletions ChocoLogParse/Public/Get-ChocoLogEntry.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<#
.SYNOPSIS
Grab a single session from the latest log file. Defaults to last exection.

Check warning on line 3 in ChocoLogParse/Public/Get-ChocoLogEntry.ps1

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Misspelled word (exection) Suggestions: (execution*, ejection, election, erection, evection)
.DESCRIPTION
Reads the latest log file and grabs the last session
.NOTES
Expand All @@ -8,7 +8,11 @@
.EXAMPLE
Get-ChocoLogEntry

Grabs the laste entry from the latest log

Check warning on line 11 in ChocoLogParse/Public/Get-ChocoLogEntry.ps1

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (laste) Suggestions: (lase, last, late, lasts, latte)
.EXAMPLE
Get-ChocoLogEntry -NoColor

Grabs the latest entry from the latest log without colored output
.PARAMETER Report
This changes the output to be more friendly for reporting
.PARAMETER Path
Expand All @@ -20,6 +24,9 @@
The log4net pattern layout used to parse the log. It is very unlikely that you
need to supply this. The code expects pattern names: time, session, level, and
message.
.PARAMETER NoColor
Disables colored output in the formatter. When specified, the output will be
displayed without ANSI color codes.
#>
function Get-ChocoLogEntry {
[CmdletBinding()]
Expand All @@ -36,20 +43,21 @@
$Filter = 'chocolatey*.log',
[string]
$PatternLayout = '%date %thread [%-5level] - %message',
[switch]$Report
[switch]$Report,
[switch]$NoColor
)
# ToDo:
# - Support searching for a cli entry
# - sub command type (e.g. search, list, upgrade)
# - Exit code
# - Filter to recent time to make sure we get the right one

$entry = Read-ChocoLog -FileLimit 1 -Path $Path -Filter $Filter -PatternLayout $PatternLayout | Select-Object -Last 1
$entry = Read-ChocoLog -FileLimit 1 -Path $Path -Filter $Filter -PatternLayout $PatternLayout -NoColor:$NoColor | Select-Object -Last 1
if ($report) {
# Print out in a format that's useful for Chef logging
Write-Host ('Command: {0}' -F $entry.cli)
Write-Host ('Exit Code: {0}' -F $entry.exitCode )
# This will print out the configuration in a more readble way

Check warning on line 60 in ChocoLogParse/Public/Get-ChocoLogEntry.ps1

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (readble) Suggestions: (readable, ridable, reade, rabble, raddle)
Write-Host "Configuration: `n - "
$entry.Configuration

Expand Down
22 changes: 18 additions & 4 deletions ChocoLogParse/Public/Read-ChocoLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
Read-ChocoLog

This will read the latest Chocolatey.log on the machine.
.EXAMPLE
Read-ChocoLog -NoColor

This will read the latest Chocolatey.log on the machine without colored output.
.PARAMETER Path
The log path you want to parse. This will default to the latest local log.
This can be a directory of logs.
Expand All @@ -23,13 +27,16 @@
The log4net pattern layout used to parse the log. It is very unlikely that you
need to supply this. The code expects pattern names: time, session, level, and
message.
.PARAMETER NoColor
Disables colored output in the formatter. When specified, the output will be
displayed without ANSI color codes.
#>
function Read-ChocoLog {
# This makes PlatyPS sad.
[OutputType([System.Collections.Generic.List[ChocoLog]])]
param (
[ValidateScript({
if (-Not ($_ | Test-Path) ) {
if (-not ($_ | Test-Path) ) {
throw "File or folder does not exist"
}
return $true
Expand All @@ -41,8 +48,14 @@ function Read-ChocoLog {
[String]
$Filter = 'chocolatey*.log',
[string]
$PatternLayout = '%date %thread [%-5level] - %message'
$PatternLayout = '%date %thread [%-5level] - %message',
[switch]
$NoColor
)

# Set module-level variable to control coloring in formatter
$script:ChocoLogNoColor = $NoColor.IsPresent

$files = Get-Item -Path $Path
if ($files.PSIsContainer) {
$files = Get-ChildItem -Path $Path -Filter $Filter |
Expand Down Expand Up @@ -110,6 +123,7 @@ function Read-ChocoLog {
}

# Return the whole parsed object
Write-Verbose "Returning results in desceding order. Count: $($parsed.Count)"
$parsed.Values | Sort-Object -Descending Time
Write-Verbose "Returning results in descending order. Count: $($parsed.Count)"
$return = $parsed.Values | Sort-Object -Descending Time
return $return
}
18 changes: 17 additions & 1 deletion docs/en-US/Get-ChocoLogEntry.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

## SYNOPSIS
Grab a single session from the latest log file.
Defaults to last exection.

Check warning on line 12 in docs/en-US/Get-ChocoLogEntry.md

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Misspelled word (exection) Suggestions: (execution*, ejection, election, erection, evection)

## SYNTAX

```
Get-ChocoLogEntry [[-Path] <String[]>] [[-Filter] <String>] [[-PatternLayout] <String>] [-Report]
Get-ChocoLogEntry [[-Path] <String[]>] [[-Filter] <String>] [[-PatternLayout] <String>] [-Report] [-NoColor]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

Expand All @@ -28,7 +28,7 @@
Get-ChocoLogEntry
```

Grabs the laste entry from the latest log

Check warning on line 31 in docs/en-US/Get-ChocoLogEntry.md

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (laste) Suggestions: (lase, last, late, lasts, latte)

## PARAMETERS

Expand Down Expand Up @@ -99,13 +99,29 @@
Accept wildcard characters: False
```

### -NoColor
Disables colored output in the formatter. When specified, the output will be
displayed without ANSI color codes.

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```

### -ProgressAction
{{ Fill ProgressAction Description }}

```yaml
Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Check warning on line 124 in docs/en-US/Get-ChocoLogEntry.md

View workflow job for this annotation

GitHub Actions / ci / Run Linters

Unknown word (proga) Suggestions: (proa, prog, progs, Prog, prgo)

Required: False
Position: Named
Expand Down
18 changes: 17 additions & 1 deletion docs/en-US/Read-ChocoLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Parses a Chocolatey log into an object that is easier to search and filter.

```
Read-ChocoLog [[-Path] <String[]>] [[-FileLimit] <Int32>] [[-Filter] <String>] [[-PatternLayout] <String>]
[<CommonParameters>]
[-NoColor] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -100,6 +100,22 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -NoColor
Disables colored output in the formatter. When specified, the output will be
displayed without ANSI color codes.

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).

Expand Down
4 changes: 2 additions & 2 deletions requirements.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
}
}
'psake' = @{
Version = '4.9.0'
Version = '4.9.1'
}
'BuildHelpers' = @{
Version = '2.0.16'
}
'PowerShellBuild' = @{
Version = '0.6.1'
Version = '0.7.3'
}
'PSScriptAnalyzer' = @{
Version = '1.19.1'
Expand Down
33 changes: 8 additions & 25 deletions tests/ChocoLogParse.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cSpell:ignore BHPS Ffoo Subkeys
BeforeDiscovery {
if ($null -eq $env:BHPSModuleManifest) {
.\build.ps1 -Task Init
}
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
Expand All @@ -14,8 +17,7 @@ BeforeDiscovery {

BeforeAll {
# Setup dummy data including things across multiple lines
$folder = "TestDrive:\folder"
New-Item -Path "TestDrive:\" -Name 'folder' -Type Directory -Force
$folder = Join-Path -Path $PSScriptRoot -ChildPath 'fixtures\folder'
$singleFile = "TestDrive:\test.log"
# Due to "files.trimTrailingWhitespace" vscode setting, I added some '.'s
# to the multiline examples
Expand Down Expand Up @@ -74,27 +76,6 @@ Chocolatey upgraded 0/1 packages.
2023-06-14 14:22:10,115 54321 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 14:22:10,117 54321 [DEBUG] - Exiting with 900
'@
# Create 10 files with 2 random sessions
0..10 | ForEach-Object {
$randID = Get-Random -Minimum 1000 -Maximum 99999
$randID2 = $randID - 100
Set-Content "TestDrive:\folder\chocolatey.$($_).log" -Value @"
2023-06-14 14:22:09,411 $randId [DEBUG] - Ffoo
2023-06-14 14:22:09,418 $randId [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
2023-06-14 14:22:09,422 $randId [INFO ] - Upgrading the following packages:
2023-06-14 14:22:09,423 $randId [INFO ] - zoom
2023-06-14 14:22:09,423 $randId [INFO ] - By upgrading, you accept licenses for the packages.
2023-06-14 14:22:10,107 $randId [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
You must be smarter than the average bear...
2023-06-14 14:22:10,113 $randId [WARN ] - .
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
2023-06-14 14:22:10,115 $randId [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 14:22:10,117 $randId [DEBUG] - Exiting with 0
2023-06-14 15:22:09,411 $randId2 [DEBUG] - Ffoo
2023-06-14 15:22:10,117 $randId2 [DEBUG] - Exiting with 0
"@
}
}

Describe 'Read-ChocoLog' {
Expand All @@ -106,7 +87,7 @@ Describe 'Read-ChocoLog' {

Context 'For Single File' {
It 'Parses the correct number of sessions' {
($script:parsed).Count | Should -Be 3
($script:parsed).Count | Should -Be 3
}

It 'Parses the correct number of lines per session' {
Expand Down Expand Up @@ -140,7 +121,9 @@ Describe 'Read-ChocoLog' {
}

It 'Parses the correct number of lines per session' {
$script:multiple.Count | Should -Be 2
$script:multiple[0].logs.Count | Should -Be 9
$script:multiple[1].logs.Count | Should -Be 2
}
}
}
Expand All @@ -153,7 +136,7 @@ Describe 'Get-ChocoLogEntry' {

Context 'For Single File' {
It 'Parses the correct number of sessions' {
($script:parsedEntry).Count | Should -Be 1
($script:parsedEntry).Count | Should -Be 1
}

It 'Parses the correct number of lines per session' {
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/folder/chocolatey.00.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2023-06-14 13:22:09,411 20171 [DEBUG] - Ffoo
2023-06-14 13:22:09,418 20171 [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
2023-06-14 13:22:09,422 20171 [INFO ] - Upgrading the following packages:
2023-06-14 13:22:09,423 20171 [INFO ] - zoom
2023-06-14 13:22:09,423 20171 [INFO ] - By upgrading, you accept licenses for the packages.
2023-06-14 13:22:10,107 20171 [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
You must be smarter than the average bear...
2023-06-14 13:22:10,113 20171 [WARN ] - .
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
2023-06-14 13:22:10,115 20171 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 13:22:10,117 20171 [DEBUG] - Exiting with 0
2023-06-14 14:10:09,411 20071 [DEBUG] - Ffoo
2023-06-14 14:10:10,117 20071 [DEBUG] - Exiting with 0
14 changes: 14 additions & 0 deletions tests/fixtures/folder/chocolatey.01.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2023-06-14 12:22:09,411 82710 [DEBUG] - Ffoo
2023-06-14 12:22:09,418 82710 [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
2023-06-14 12:22:09,422 82710 [INFO ] - Upgrading the following packages:
2023-06-14 12:22:09,423 82710 [INFO ] - zoom
2023-06-14 12:22:09,423 82710 [INFO ] - By upgrading, you accept licenses for the packages.
2023-06-14 12:22:10,107 82710 [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
You must be smarter than the average bear...
2023-06-14 12:22:10,113 82710 [WARN ] - .
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
2023-06-14 12:22:10,115 82710 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 12:22:10,117 82710 [DEBUG] - Exiting with 0
2023-06-14 13:10:09,411 82610 [DEBUG] - Ffoo
2023-06-14 13:10:10,117 82610 [DEBUG] - Exiting with 0
14 changes: 14 additions & 0 deletions tests/fixtures/folder/chocolatey.02.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2023-06-14 11:22:09,411 27936 [DEBUG] - Ffoo
2023-06-14 11:22:09,418 27936 [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
2023-06-14 11:22:09,422 27936 [INFO ] - Upgrading the following packages:
2023-06-14 11:22:09,423 27936 [INFO ] - zoom
2023-06-14 11:22:09,423 27936 [INFO ] - By upgrading, you accept licenses for the packages.
2023-06-14 11:22:10,107 27936 [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
You must be smarter than the average bear...
2023-06-14 11:22:10,113 27936 [WARN ] - .
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
2023-06-14 11:22:10,115 27936 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 11:22:10,117 27936 [DEBUG] - Exiting with 0
2023-06-14 12:10:09,411 27836 [DEBUG] - Ffoo
2023-06-14 12:10:10,117 27836 [DEBUG] - Exiting with 0
14 changes: 14 additions & 0 deletions tests/fixtures/folder/chocolatey.03.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2023-06-14 10:22:09,411 67323 [DEBUG] - Ffoo
2023-06-14 10:22:09,418 67323 [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
2023-06-14 10:22:09,422 67323 [INFO ] - Upgrading the following packages:
2023-06-14 10:22:09,423 67323 [INFO ] - zoom
2023-06-14 10:22:09,423 67323 [INFO ] - By upgrading, you accept licenses for the packages.
2023-06-14 10:22:10,107 67323 [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
You must be smarter than the average bear...
2023-06-14 10:22:10,113 67323 [WARN ] - .
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
2023-06-14 10:22:10,115 67323 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 10:22:10,117 67323 [DEBUG] - Exiting with 0
2023-06-14 11:10:09,411 67223 [DEBUG] - Ffoo
2023-06-14 11:10:10,117 67223 [DEBUG] - Exiting with 0
14 changes: 14 additions & 0 deletions tests/fixtures/folder/chocolatey.04.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2023-06-14 09:22:09,411 21017 [DEBUG] - Ffoo
2023-06-14 09:22:09,418 21017 [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
2023-06-14 09:22:09,422 21017 [INFO ] - Upgrading the following packages:
2023-06-14 09:22:09,423 21017 [INFO ] - zoom
2023-06-14 09:22:09,423 21017 [INFO ] - By upgrading, you accept licenses for the packages.
2023-06-14 09:22:10,107 21017 [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
You must be smarter than the average bear...
2023-06-14 09:22:10,113 21017 [WARN ] - .
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
2023-06-14 09:22:10,115 21017 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 09:22:10,117 21017 [DEBUG] - Exiting with 0
2023-06-14 10:10:09,411 20917 [DEBUG] - Ffoo
2023-06-14 10:10:10,117 20917 [DEBUG] - Exiting with 0
14 changes: 14 additions & 0 deletions tests/fixtures/folder/chocolatey.05.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2023-06-14 08:22:09,411 52383 [DEBUG] - Ffoo
2023-06-14 08:22:09,418 52383 [DEBUG] - _ Chocolatey:ChocolateyUpgradeCommand - Normal Run Mode _
2023-06-14 08:22:09,422 52383 [INFO ] - Upgrading the following packages:
2023-06-14 08:22:09,423 52383 [INFO ] - zoom
2023-06-14 08:22:09,423 52383 [INFO ] - By upgrading, you accept licenses for the packages.
2023-06-14 08:22:10,107 52383 [INFO ] - zoom v5.14.11.17466 is newer than the most recent.
You must be smarter than the average bear...
2023-06-14 08:22:10,113 52383 [WARN ] - .
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
2023-06-14 08:22:10,115 52383 [DEBUG] - Sending message 'PostRunMessage' out if there are subscribers...
2023-06-14 08:22:10,117 52383 [DEBUG] - Exiting with 0
2023-06-14 09:10:09,411 52283 [DEBUG] - Ffoo
2023-06-14 09:10:10,117 52283 [DEBUG] - Exiting with 0
Loading
Loading