PSMultiLog
PSMultiLog copied to clipboard
Multi-target logging module for Windows PowerShell
PSMultiLog
PSMultiLog is a multi-target logging module to simplify and centralize logging in your code. At the beginning of your code, turn on the log targets you want to use, and from the on just call the Write-Log
Cmdlet to simultaneously write to all active targets.
Log targets
The PSMultiLog module supports the following log targets:
- File
- Host (great for debugging, or when you're first building a script)
- PassThru (will write to the verbose, warning, and error streams)
- Event Log
- Email (log events are collected, and then the
Send-EmailLog
Cmdlet is used to send the actual e-mail) - Slack webhook integration
Compatibility
As of version 2.0.0, The PSMultiLog module is compatible with PowerShell v3.0 and up. for compatability with v2.0, please use version 1.1.1 (commit 94e4207).
Installation
Copy the module into a folder in your PowerShell Module Path. You can check $env:PSModulePath
in a PowerShell session if unsure what paths are included, but starting in PowerShell v4.0 the standard is C:\Program Files\WindowsPowerShell\Modules
. Don't put the code directly into the Modules folder, it must be in a subfolder named PSMultiLog
.
Usage
Here is a simple example of using multiple log targets. Note that when you enable a log target, you also specify what severity log entries to write to it. In the example below, we are everything to a log file, but only errors to the event log.
# Import the module.
Import-Module PSMultiLog
# Turn on desired targets.
Start-FileLog -FilePath c:\ps\info.log -LogLevel Information # Log everything.
Start-EventLogLog -Source "SampleScript" # The default value for LogLevel is "Error".
# Write log entries.
Write-Log -EntryType Information -Message "This will only get logged to file."
Write-Log -EntryType Warning -Message "This will only get logged to file."
Write-Log -EntryType Error -Message "This will get logged both to file and to the Event Log."
# Turn off targets (this is sort of optional, since it won't matter once the script exits).
Stop-FileLog
Stop-EventLogLog
Sample Output
Below is some sample output generated by the following code:
Start-HostLog -LogLevel Information
Start-FileLog -LogLevel Information -FilePath c:\ps\Log.txt
Start-EventLogLog -Source "PowerShell Sample" -LogLevel Information
Start-EmailLog
Write-Log -EntryType Information -Message "This is a sample log message."
Write-Log -EntryType Warning -Message "This is a sample warning."
Write-Log -EntryType Error -Message "This is a sample error."
Write-Log -EntryType Error -Message "This is a sample error with an exception." -Exception (New-Object -TypeName Exception)
Send-EmailLog -SmtpServer smtp.host.com -To [email protected] -From [email protected] -Subject "Sample Log" -Message "This is a sample log." -LogLevel Information
Stop-EmailLog
Stop-EventLogLog
Stop-FileLog
Stop-HostLog
Host Log
The following will be output to the host:
File Log
The contents of c:\ps\Log.txt
will be:
[2015-10-12 13:32:10Z] - INFO - This is a sample log message.
[2015-10-12 13:32:10Z] - WARN - This is a sample warning.
[2015-10-12 13:32:10Z] - ERRR - This is a sample error.
[2015-10-12 13:32:10Z] - ERRR - This is a sample error with an exception. - Exception: Exception of type 'System.Exception' was thrown.
Event Log Log
The following Events will be created:
The Source, Stack Trace, and Target Site properties would be populated on a real exception.
E-mail Log
An e-mail will be generated which looks like this:
The Source, Stack Trace, and Target Site properties would be populated on a real exception.
About Slack Integration
Integrating with Slack requires setting up an Incoming Webhook, but don't worry about knowing the details of how to make the HTTP calls. All you really need is the URL they will provide once you enable the integration. For additional information about how to use Slack logging, see Get-Help Start-SlackLog
.