Despite out best efforts as coders, automated processes sometimes fail. One of the principle ways to troubleshoot such processes is to log data to a file so you can follow what happened after-the-fact. I have a TON of scripts that have to do this, so it made sense to cobble together some functions that make doing this easier.
To this end, I’ve written a script module called bsti.logging. It features the following functions:
Once you import the module, you call New-LogFile to setup a new file to write to. You can specify options to append the weekday to the file or a more specific timestamp (e.g. MyLog_mmddyyyyhhmmss.log) to the log file. For timestamped log files, you can also setup retention so old log files get automatically deleted after a period of time or after so many accumulate.
I have three basic types of ways to handle log file naming that you need to be clear on to get good use out of the module:
1) Standard – The log file path you pass is will be unchanged by the function. The purging parameters are ignored, you must use -Append or it will be overwritten if it exists already.
2) Circular – The log file will have _weekday appended to the file name before the extension. If you pass in C:\temp\log\MyLogFile.log for example, you get:
When you call New-LogFile with circular naming and the *same* log file path again that same day, it will automatically be appended to. When the next Monday rolls by, it *automatically* overwrites it.
This scheme is good if you call the script that writes to the log file frequently, don’t want to manage a large number of log files, and don’t need more than 7 days of log file history.
3) DateStamped – This appends a detailed datestamp to the log file name before the extension. In the example above, you get:
MyLogFile_03292015200500.log (Assuming 3/29/2015 10:05 PM)
This means every time you call New-LogFile (Assuming you wait at least 1 second!), you get a unique log file. Append is essentially ignored.
The PurgeAfter and KeepNumberOfFIles, if specified, will cause the New-LogFile function to call Remove-LogFiles automatically and keep your log files trimmed as you specify. If you specify both PurgeAfter AND KeepNumberOfFiles, both thresholds are observed (meaning the file needs to be older than what you specified with PurgeAfter AND you have to have KeepNumberOfFiles remaining).
This scheme is good if you need a specified history of log files and want individual log files for each run of your process. The automatic cleanup is a bonus.
Once you’ve setup your new log file, you call the following functions to write to it. These functions also echo to the console, so you can replace any calls to Write-Host with these functions and get messages to your console and to a log file:
Makes things pretty simple.
As with all of my modules and module functions, I heavily document them using Powershell comments-based help. Just try:
Get-Help New-LogFile -Full
This module does depend on my bsti.conversion module, so if you use this module as a whole you need both modules. I posted about that module here.