Graylog and PowerShell
What is Graylog? Why should I use it?
Logging is a topic that does not get enough attention in the PowerShell community. When I first started writing PowerShell, I did not use any logging. I did not have any record of what my scripts did or if there were any failures during execution. I changed things up for a while, writing adhoc logging functions for each script that I wrote. However, these did not always include all the needed information. The biggest problem I had was that I never would check the logs until I realized something wasn’t working. I had no way of knowing if anything failed.
I took notice that our development team had a standard logging assembly that was used across all of our custom .NET applications. That is what we needed on my infrastructure team as well. I created an internal module that imports the other dependent modules and has a few functions that standardize our logging. This module is used in all of our PowerShell jobs and automation.
We use Graylog as our log management software. Graylog is the closest Open Source product to Splunk. It allows us to store, analyze, and alert on log data. We mainly use Graylog to gather Windows Server event logs, F5 web request logs, and logs from our custom .NET applications. A few years ago I wrote a module to send data to Graylog using the GELF protocol. This originally was used in a script that would query log data from SQL and then send it to Graylog.
Using PSGELF to send log data to Graylog
Here are few examples using the basic PSGELF module to send logs.
Extremely Simple example
Querying logs from SQL to send to Graylog
Example Internal Module
The internal module, uses PSGELF and EsOsO’s Logging module as the base. EsOsO’s module starts a new thread specific for logging to avoid bottlenecking. He also has support for custom targets. I wrote a custom target that uses PSGELF. Most of my scripts use the Graylog target and the File target. I have uploaded an example of the internal module here. The Owner, Environment, and ScriptPath are used in Graylog to alert appropriately.
Example Script using the internal module
Below is what the logs look like inside Graylog.