A logging module for PowerShell
Import-Module C:\Path\To\PSLogger.psm1
One or more log targets must be added into the logger before any logging will be possible.
The module has several pre-defined log targets, each sharing the following common parameters:
Name
: (Required) A unique name for the targetMinLevel
: Minimum log level (Default: INFO)MessageFormat
: Format of log message (Default: {{date}} - {{level}} - [{{stack}}] --> {{message}})Passive
: Specifies that target should be passive - target must be explicitly targeted
This target will log to a single log file
Add-LoggerFileTarget [-Name] [-MinLevel] [-MessageFormat] [-Passive] [-LogPath]
LogPath
: Path of log file (Default: $Env:TEMP\{Name}.log)
This target will output to host
Add-LoggerHostTarget [-Name] [-MinLevel] [-MessageFormat] [-Passive]
More log targets can be added via Add-LoggerTarget
:
Add-LoggerTarget [-Name] [-MinLevel] [-MessageFormat] [-Passive] [-Invoke] [-Parameters]
Invoke
: (Required) A scriptblock with a parameterMessage
, which will be invoked by the logger.Level
andParameters
parameters will be passed also, if availableParameters
A hashtable of parameters which will be passed into Invoke
Write-LoggerTrace [-Message] [-Target]
Write-LoggerDebug [-Message] [-Target]
Write-LoggerInfo [-Message] [-Target]
Write-LoggerWarn [-Message] [-Target]
Write-LoggerError [-Message] [-Target]
Write-LoggerFatal [-Message] [-Target] [-ExitCode]
Aliases are also defined for writing logs:
Logger.Trace
Logger.Debug
Logger.Info
Logger.Warn
Logger.Error
Logger.Fatal
> Write-LoggerInfo "Testing log"
> "an error occurred","oops" | Write-LoggerError
> Write-LoggerWarn "Testing log file" -Target LogFile