A simple project to easily add a log to your projects
Releases -
Report Bug
This project was to just offer people a easy way to quickly add the ability to output a nice, simple log within their own projects. There are two versions in this project known as the Class Version and the Module Version.
The class Version is the preferred version however this version is limited to PowerShell Version 5.0 and greater and utilises the Using statement.
The Module Version is available for those prefering to use Import-Module and those using older versions of PowerShell as the Class version will not work on PowerShell versions prior to 5.0.
- PowerShell version 2.0 or greater - Module Version.
- PowerShell version 5.0 or greater - Either version.
- Download the latest release.
- Extract the zipped folder.
- Place the "Write-Log" folder in your project's folder or in a location the script can access under the context it will be ran.
- Add the Using satement pointing to the Write-Log-Class.psm1 file, please note using statements must be the very first lines of your script. In this example the Write-Log folder containing the file is in the root folder with the script calling it.
using module ".\Write-Log\Write-Log-Class.psm1"
- See Class Version Usage section for examples on how to configure the log location and add enteries.
- Download the latest release.
- Extra the zipped folder.
- Ensure the Write-log.psm1 remains in a folder called "Write-Log" and place the Write-Log folder in your project's folder or in a location the script can access under the context it will be ran.
- Import the Module. In this example the Write-Log folder is in the root of the project folder.
$module = "$PSScriptRoot\Write-Log"
if(!(test-path $module)){
write-host "$module not found" -ForegroundColor Red
exit
}
Import-Module $module
- See Module Version Usage section for examples on how to configure the log location and add enteries.
- Add the Remove-Module line to the bottom of your script.
Remove-Module Write-Log
using module ".\Class\Write-Log\Write-Log-Class.psm1"
$Log = [WriteLog]::New("C:\Example\mylog.log")
$Log.AddInfo("Something occurred that was worth making an info log about")
$Log.AddError("There was a huge error!")
$Log.AddWarning("Oh dear, I should really warn you about this!")
$Log.AddEntry("Testing","Test Severity") #This method is hidden but can be used for custom severities
The below example shows having mutliple Write-Log objects to store different types or log enteries in different logs.
using module ".\Class\Write-Log\Write-Log-Class.psm1"
$InfoLog = [WriteLog]::New("C:\Example\Info.log")
$ErrorLog = [WriteLog]::New("C:\Example\Errors.log")
$WarningLog = [WriteLog]::New("C:\Example\Warning.log")
$InfoLog.AddInfo("Something occurred that was worth making an info log about")
$ErrorLog.AddError("There was a huge error!")
$WarningLog.AddWarning("Oh dear, I should really warn you about this!")
$module = "$PSScriptRoot\Module\Write-Log"
if(!(test-path $module)){
write-host "$module not found" -ForegroundColor Red
exit
}
Import-Module $module
$logLocation = "C:\Example\Log.log"
write-log "This is an example Info" -logLocation $logLocation
write-log "This is an example Error" -severity "Error" -logLocation $logLocation
write-log "This is an example Warning" -severity "Warning" -logLocation $logLocation
Remove-Module Write-Log
Below is an example of having seperate logs for Info, Error and Warning enteries.
$module = "$PSScriptRoot\Module\Write-Log"
if(!(test-path $module)){
write-host "$module not found" -ForegroundColor Red
exit
}
Import-Module $module
$InfoLog = "C:\Example\Log.log"
$ErrorLog = "C:\Example\Errors.log"
$WarningLog = "C:\Example\Warning.log"
write-log "This is an example Info" -logLocation $InfoLog
write-log "This is an example Error" -severity "Error" -logLocation $ErrorLog
write-log "This is an example Warning" -severity "Warning" -logLocation $WarningLog
Remove-Module Write-Log
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Othneildrew's Best-README-Template
- matthewjdegarmo - Excellent suggestion on moving from ValidationSet in the module version and improvements / corrections to the ReadMe
- SeidChr - For prompting the addition of .txt