Skip to content

Commit

Permalink
Converting to Environment Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
VertigoRay committed Jan 12, 2023
1 parent 0a3fd4c commit 3d9bac7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion PSWriteLog/Private/Write-InvocationHeader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ function Write-InvocationHeader {
$tmp.FullName | Remove-Item -ErrorAction 'SilentlyContinue' -Force

Write-Log -Message $header
$env:PSWriteLogIncludedInvocationHeader = $true
$env:PSWriteLogIncludeInvocationHeader = $null
}
20 changes: 10 additions & 10 deletions PSWriteLog/Private/Write-Log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,34 @@ function global:Write-Log {
[Parameter(Mandatory = $false, Position = 4)]
[ValidateSet('CMTrace', 'Legacy')]
[string]
$LogType = 'CMTrace',
$LogType = $(if ($env:PSWriteLogType) { $env:PSWriteLogType } else { 'CMTrace' }),

[Parameter(Mandatory = $false, Position = 5)]
[ValidateNotNullorEmpty()]
[IO.FileInfo]
$FilePath = [IO.Path]::Combine($env:Temp, ('PowerShell {0} {1} {2}.log' -f $PSVersionTable.PSEdition, $PSVersionTable.PSVersion, $MyInvocation.CommandOrigin)),
$FilePath = $(if ($env:PSWriteLogFilePath) { $env:PSWriteLogFilePath } else { [IO.Path]::Combine($env:Temp, ('PowerShell {0} {1} {2}.log' -f $PSVersionTable.PSEdition, $PSVersionTable.PSVersion, $MyInvocation.CommandOrigin)) }),

[Parameter(Mandatory = $false, Position = 6)]
[ValidateNotNullorEmpty()]
[decimal]
$MaxLogFileSizeMB = 10,
$MaxLogFileSizeMB = $(if ($env:PSWriteLogMaxLogFileSizeMB) { $env:PSWriteLogMaxLogFileSizeMB -as [decimal] } else { 10 }),

[Parameter(Mandatory = $false)]
[switch]
$ContinueOnError,
[bool]
$ContinueOnError = $(if ($env:PSWriteLogContinueOnError) { $env:PSWriteLogContinueOnError -as [bool] } else { $false }),

[Parameter(Mandatory = $false)]
[switch]
$DisableLogging,
[bool]
$DisableLogging = $(if ($env:PSWriteLogDisableLogging) { $env:PSWriteLogDisableLogging -as [bool] } else { $false }),

[Parameter(Mandatory = $false)]
[switch]
$IncludeInvocationHeader
[bool]
$IncludeInvocationHeader = $(if ($env:PSWriteLogIncludeInvocationHeader) { $env:PSWriteLogIncludeInvocationHeader -as [bool] } else { $false })
)

begin {
# Microsoft.PowerShell.Utility\Write-Information "[Write-Log] BoundParameters: $($MyInvocation.BoundParameters | Out-String)" -Tags 'VertigoRay\PSWriteLog','Write-Log'
if ($IncludeInvocationHeader.IsPresent -and -Not $env:PSWriteLogIncludedInvocationHeader) {
if ($IncludeInvocationHeader) {
Write-InvocationHeader
}

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ Instead, we've created [proxy functions](https://learn.microsoft.com/en-us/dotne
These proxy functions keep the original functionality of the function intact, while also sending the outputted message to the `Write-Log` function.
By default, `Write-Log` will write messages to a log file in [CMTrace](https://learn.microsoft.com/en-us/mem/configmgr/core/support/cmtrace) compatible format, but Legacy (plain-text) file format is also available.
To configure *PSWriteLog*, what you need to do is change the default actions of the `Write-Log` parameters.
You can specify default parameters using the built-in [`$PSDefaultParameterValues`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters_default_values?view=powershell-5.1) variable.
You can specify default parameters using environment variables.
Here's an example of specifying the Log file path and log type globally:

```powershell
$PSDefaultParameterValues.Add('Write-Log:FilePath', "${env:SystemRoot}\Logs\MyApp.log")
$PSDefaultParameterValues.Add('Write-Log:LogType', 'Legacy')
$env:PSWriteLogFilePath = "${env:SystemRoot}\Logs\MyApp.log"
$env:PSWriteLogType = 'Legacy'
```

> ℹ: For more details, [check out the wiki](/VertigoRay/PSWriteLog/wiki)!
Expand Down Expand Up @@ -119,7 +119,7 @@ For me, when I'm working on *PSWriteLog* and I want to see just those messages,
$InformationPreference = 'Continue'
$PSDefaultParameterValues.Set_Item('Write-Log:InformationVariable', 'foo')
Write-Host 'Testing a thing'
Write-Host 'Testing a thing' -InformationVariable 'foo'
$foo | ?{ $_.Tags -eq 'Write-Log' }
```

Expand Down

0 comments on commit 3d9bac7

Please sign in to comment.