Logging made simple

Logging is perhaps not the first thing you think about when making small scripts that is only intended to be used by yourself, but the moment other people are involved, the probability of something going wrong is going up. And making scripts intended for others usually means the size and complexity goes up as well. At some point, you will get tired of feedback from others that are hard to debug, and wish you wrote logging into your script! What if logging was as simple as just adding a bunch of “Write-Log”‘s in your code? I’ll show you how.

I have created three logging functions that makes logging as easy as writing to the verbose stream. The first function (New-Log) you add one time, in the beginning of your script, and defines what kind of logging you want to do (it currently supports logging to file and to the Windows Event log), and some parameters.

Whenever you want to log something throughout your script, just use the second function (Write-Log). It works the same as all the other Write-* commands, except it has some different parameters relevant for logging. When writing to a file, it will automatically call the third function (Invoke-LogRotation) after each write, so you can keep some kind of control of your log(s).

Look at the help for New-Log and Write-Log for examples of use, and descriptions of the different parameters.

Even though I try to test out my functions thoroughly before releasing them, there are probably some bugs or stuff I haven’t thought about. So if you find anything wrong, or have any ideas for future improvement, let me know, ok?

Oh, and this is the first time I’m using a mutex (I found some excellent information about it from Boe Prox), so if I’m using it completely wrong, I’d appreciate you’d let me know!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s