Skip to content

Commit

Permalink
Merge pull request #15 from ProVal-Tech/write-log-advanced-function
Browse files Browse the repository at this point in the history
Update Write-Log with Proper Array Handling
  • Loading branch information
nixuno authored Oct 23, 2024
2 parents 8ab2dab + 3c47810 commit 5f097d1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 71 deletions.
145 changes: 75 additions & 70 deletions Strapper/Public/Write-Log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function Write-Log {
[CmdletBinding(DefaultParameterSetName = 'Level')]
param (
[Parameter(ValueFromPipeline, Mandatory, Position = 0)][Alias('Message', 'Text')]
[object]$InputObject,
[object[]]$InputObject,
[Parameter(Mandatory, DontShow, ParameterSetName = 'Type')]
[string]$Type,
[Parameter(ParameterSetName = 'Level')]
Expand All @@ -31,80 +31,85 @@ function Write-Log {
[Parameter()]
[System.Management.Automation.ErrorCategory]$ErrorCategory = [System.Management.Automation.ErrorCategory]::NotSpecified
)
if (!($StrapperSession.LogPath -and $StrapperSession.ErrorPath)) {
$location = (Get-Location).Path
$StrapperSession.LogPath = Join-Path -Path $location -ChildPath "$((Get-Date).ToString('yyyyMMdd'))-log.txt"
$StrapperSession.ErrorPath = Join-Path -Path $location -ChildPath "$((Get-Date).ToString('yyyyMMdd'))-error.txt"
}
$Text = $InputObject.ToString()

# Accounting for -Type to allow for backwards compatibility.
if ($Type) {
switch ($Type) {
'LOG' { $Level = [StrapperLogLevel]::Information }
'WARN' { $Level = [StrapperLogLevel]::Warning }
'ERROR' { $Level = [StrapperLogLevel]::Error }
'SUCCESS' { $Level = [StrapperLogLevel]::Information }
'DATA' { $Level = [StrapperLogLevel]::Information }
'INIT' { $Level = [StrapperLogLevel]::Debug }
Default { $Level = [StrapperLogLevel]::Information }
}
} else {
[StrapperLogLevel]$Level = $Level
}

switch ([StrapperLogLevel]$Level) {
([StrapperLogLevel]::Verbose) {
$levelShortName = 'VER'
Write-Verbose -Message $Text
break
begin {
if (!($StrapperSession.LogPath -and $StrapperSession.ErrorPath)) {
$location = (Get-Location).Path
$StrapperSession.LogPath = Join-Path -Path $location -ChildPath "$((Get-Date).ToString('yyyyMMdd'))-log.txt"
$StrapperSession.ErrorPath = Join-Path -Path $location -ChildPath "$((Get-Date).ToString('yyyyMMdd'))-error.txt"
}
([StrapperLogLevel]::Debug) {
$levelShortName = 'DBG'
Write-Debug -Message $Text
break
}
([StrapperLogLevel]::Information) {
$levelShortName = 'INF'
Write-Information -MessageData $Text
break
}
([StrapperLogLevel]::Warning) {
$levelShortName = 'WRN'
Write-Warning -Message $Text
break
}
([StrapperLogLevel]::Error) {
$levelShortName = 'ERR'
if ($Exception) {
Write-Error -Message $Text -Exception $Exception -Category $ErrorCategory
break
# Accounting for -Type to allow for backwards compatibility.
if ($Type) {
switch ($Type) {
'LOG' { $Level = [StrapperLogLevel]::Information }
'WARN' { $Level = [StrapperLogLevel]::Warning }
'ERROR' { $Level = [StrapperLogLevel]::Error }
'SUCCESS' { $Level = [StrapperLogLevel]::Information }
'DATA' { $Level = [StrapperLogLevel]::Information }
'INIT' { $Level = [StrapperLogLevel]::Debug }
Default { $Level = [StrapperLogLevel]::Information }
}
Write-Error -Message $Text -Category $ErrorCategory
break
}
([StrapperLogLevel]::Fatal) {
$levelShortName = 'FTL'
if ($Exception) {
Write-Error -Message $Text -Category $ErrorCategory -Exception $Exception
break
}
Write-Error -Message $Text -Category $ErrorCategory
break
}
Default {
$levelShortName = 'UNK'
Write-Information -MessageData $Text
} else {
[StrapperLogLevel]$Level = $Level
}
}
$formattedLog = "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss.fff zzz')) [$levelShortName] $Text"
Add-Content -Path $StrapperSession.logPath -Value $formattedLog
if ([StrapperLogLevel]$Level -ge [StrapperLogLevel]::Error) {
Add-Content -Path $StrapperSession.ErrorPath -Value $formattedLog
}
process {
foreach ($item in $InputObject) {
$Text = $item.ToString()
switch ([StrapperLogLevel]$Level) {
([StrapperLogLevel]::Verbose) {
$levelShortName = 'VER'
Write-Verbose -Message $Text
break
}
([StrapperLogLevel]::Debug) {
$levelShortName = 'DBG'
Write-Debug -Message $Text
break
}
([StrapperLogLevel]::Information) {
$levelShortName = 'INF'
Write-Information -MessageData $Text
break
}
([StrapperLogLevel]::Warning) {
$levelShortName = 'WRN'
Write-Warning -Message $Text
break
}
([StrapperLogLevel]::Error) {
$levelShortName = 'ERR'
if ($Exception) {
Write-Error -Message $Text -Exception $Exception -Category $ErrorCategory
break
}
Write-Error -Message $Text -Category $ErrorCategory
break
}
([StrapperLogLevel]::Fatal) {
$levelShortName = 'FTL'
if ($Exception) {
Write-Error -Message $Text -Category $ErrorCategory -Exception $Exception
break
}
Write-Error -Message $Text -Category $ErrorCategory
break
}
Default {
$levelShortName = 'UNK'
Write-Information -MessageData $Text
}
}

$formattedLog = "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss.fff zzz')) [$levelShortName] $Text"
Add-Content -Path $StrapperSession.logPath -Value $formattedLog
if ([StrapperLogLevel]$Level -ge [StrapperLogLevel]::Error) {
Add-Content -Path $StrapperSession.ErrorPath -Value $formattedLog
}

if($StrapperSession.LogsToDB) {
Write-SQLiteLog -Message $Text -Level $Level
if ($StrapperSession.LogsToDB) {
Write-SQLiteLog -Message $Text -Level $Level
}
}
}
}

2 changes: 1 addition & 1 deletion Strapper/Strapper.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
RootModule = 'Strapper.psm1'

# Version number of this module.
ModuleVersion = '1.6.1'
ModuleVersion = '1.6.2'

# ID used to uniquely identify this module
GUID = '6fe5cf06-7b4f-4695-b022-1ca2feb0341f'
Expand Down

0 comments on commit 5f097d1

Please sign in to comment.