Skip to content

Latest commit

 

History

History
178 lines (136 loc) · 7.35 KB

README.md

File metadata and controls

178 lines (136 loc) · 7.35 KB

Contributors Forks Stargazers Issues LinkedIn

Write-Log

A simple project to easily add a log to your projects
Releases - Report Bug

About The Project

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.

Class 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.

Module Version

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.

(back to top)

Getting Started

Prerequisites

  • PowerShell version 2.0 or greater - Module Version.
  • PowerShell version 5.0 or greater - Either version.

Installation of Class Version

  1. Download the latest release.
  2. Extract the zipped folder.
  3. 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.
  4. 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"
  1. See Class Version Usage section for examples on how to configure the log location and add enteries.

Installation of Module Version

  1. Download the latest release.
  2. Extra the zipped folder.
  3. 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.
  4. 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
  1. See Module Version Usage section for examples on how to configure the log location and add enteries.
  2. Add the Remove-Module line to the bottom of your script.
Remove-Module Write-Log

(back to top)

Usage of the Class Version

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!")

Usage of the Module version

$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

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

Acknowledgments

(back to top)