Skip to content

Commit f4769ce

Browse files
author
Antony Bragg
committed
Release 1.0
Here weee gooooo
1 parent bddba58 commit f4769ce

File tree

3 files changed

+187
-8
lines changed

3 files changed

+187
-8
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class WriteLog {
2+
[string]$LogLocation = "$PSScriptRoot\log.log"
3+
4+
AddError([string]$Message) {
5+
$this.AddEntry($message,"Error")
6+
}
7+
8+
AddWarning([string]$Message) {
9+
$this.AddEntry($message,"Warning")
10+
}
11+
12+
AddInfo([string]$Message) {
13+
$this.AddEntry($message,"Info")
14+
}
15+
16+
WriteLog([string]$LogLocation) {
17+
$this.LogLocation = $LogLocation
18+
}
19+
20+
hidden AddEntry([string]$Message,[string]$severity) {
21+
if(!(test-path $this.LogLocation)) {
22+
new-item $this.LogLocation -Force
23+
}
24+
25+
$timeStamp = Get-date -Format "dd/MM/yyyy HH:mm:ss"
26+
$Output = "$timeStamp - [$($severity)] $($Message)"
27+
Add-Content $this.logLocation -value $Output
28+
Write-Host "$Output"
29+
}
30+
}

Write-Log.psm1 renamed to Module/Write-Log/Write-Log.psm1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<#
22
.SYNOPSIS
3-
Basic log file creator.
3+
Creates entries to a log file
44
55
.DESCRIPTION
66
Creates an entry of a specified severity into the log file.
@@ -21,14 +21,14 @@ The directory of the log file.
2121
The name of the log file. Should be a *.log extension. The default location is the area the script was executed.
2222
2323
.EXAMPLE
24-
write-log -Message "Oh no its all gone wrong"
24+
write-log -Message "Oh no Jurgen was here"
2525
.EXAMPLE
26-
write-log -Message "Oh no its an error!" -severity "Error"
26+
write-log -Message "Oh no Jurgen was here" -severity "Error"
2727
.EXAMPLE
28-
write-log -Message "Oh no its all gone wrong" -severity "Error" -LogLocation "C:\temp\Log.log"
28+
write-log -Message "Oh no Jurgen was here" -severity "Error" -LogLocation "C:\temp\ffsJurgen.log"
2929
3030
.NOTES
31-
This original module can be found in the following GitHub Repo: https://github.com/captainqwerty/Write-Log
31+
This module can be found in the following GitHub Repo: https://github.com/captainqwerty/Write-Log
3232
#>
3333

3434
Function Write-Log {
@@ -44,10 +44,10 @@ Function Write-Log {
4444
[string]$logLocation = "$PSScriptRoot\log.log"
4545
)
4646

47-
if(!test-path $logLocation) {
48-
New-Item $logLocation -Force
47+
if(!(test-path $logLocation)) {
48+
new-item $logLocation -Force | out-null
4949
}
50-
50+
5151
$timeStamp = Get-date -Format "dd/MM/yyyy HH:mm:ss"
5252
$Output = "$timeStamp - [$severity] $Message"
5353
Add-Content $logLocation -value $Output

README.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<div align="center">
2+
3+
<h1 align="center"><b>Write-Log</b></h1>
4+
5+
<p align="center">
6+
A simple project to easily add a log to your projects
7+
<br />
8+
<a href="https://github.com/captainqwerty/Write-Log/releases">Releases</a>
9+
<a href="https://github.com/captainqwerty/Write-Log/issues">Report Bug</a>
10+
</p>
11+
</div>
12+
13+
<!-- ABOUT THE PROJECT -->
14+
## About The Project
15+
16+
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*.
17+
18+
### Class Version
19+
20+
The class Version is the preffered version however this version is limited to PowerShell Version 5.0 and greater and utilises the *Using* statement.
21+
22+
### Module Version
23+
24+
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.
25+
26+
<p align="right">(<a href="#top">back to top</a>)</p>
27+
28+
<!-- GETTING STARTED -->
29+
## Getting Started
30+
31+
This is an example of how you may give instructions on setting up your project locally.
32+
To get a local copy up and running follow these simple example steps.
33+
34+
### Prerequisites
35+
36+
This is an example of how to list things you need to use the software and how to install them.
37+
* PowerShell version 2.0 or greater - Module Version.
38+
* PowerShell version 5.0 or greater - Either version.
39+
40+
### Installation of Class Version
41+
42+
1. Download the <a href="https://github.com/captainqwerty/Write-Log/release">latest release</a>.
43+
2. 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.
44+
3. 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.
45+
```ps1
46+
using module ".\Write-Log\Write-Log-Class.psm1"
47+
```
48+
4. See <a href="#usage-of-the-class-version">Class Version Usage</a> section for examples on how to configure the log location and add enteries.
49+
50+
### Installation of Module Version
51+
52+
1. Download the <a href="https://github.com/captainqwerty/Write-Log/releases">latest release</a>.
53+
2. 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.
54+
3. Import the Module. In this example the Write-Log folder is in the root of the project folder.
55+
```ps1
56+
$module = "$psscriptroot\Write-Log"
57+
if(!(test-path $module)){
58+
write-host "$module not found" -ForegroundColor Red
59+
exit
60+
}
61+
Import-Module $module
62+
```
63+
4. See <a href="#usage-of-the-module-version">Module Version Usage</a> section for examples on how to configure the log location and add enteries.
64+
5. Add the Remove-Module line to the bottom of your script.
65+
```ps1
66+
Remove-Module Write-Log
67+
```
68+
69+
<p align="right">(<a href="#top">back to top</a>)</p>
70+
71+
<!-- USAGE EXAMPLES -->
72+
## Usage of the Class Version
73+
```ps1
74+
using module ".\Class\Write-Log\Write-Log-Class.psm1"
75+
76+
$Log = [WriteLog]::New("C:\Example\mylog.log")
77+
78+
$Log.AddInfo("Something occured that was wroth making an info log about")
79+
$Log.AddError("There was a huge error!")
80+
$Log.AddWarning("Oh dear I should really warn you about this!")
81+
$Log.AddEntry("Testing","Test Severity") #This method is hidden but can be used for custom severities
82+
```
83+
The below example shows having mutliple Write-Log objects to store different types or log enteries in different logs.
84+
85+
```ps1
86+
using module ".\Class\Write-Log\Write-Log-Class.psm1"
87+
88+
$InfoLog = [WriteLog]::New("C:\Example\Info.log")
89+
$ErrorLog = [WriteLog]::New("C:\Example\Errors.log")
90+
$WarningLog = [WriteLog]::New("C:\Example\Warning.log")
91+
92+
$InfoLog.AddInfo("Something occured that was wroth making an info log about")
93+
$ErrorLog.AddError("There was a huge error!")
94+
$WarningLog.AddWarning("Oh dear I should really warn you about this!")
95+
```
96+
97+
## Usage of the Module version
98+
```ps1
99+
$module = "$psscriptroot\Module\Write-Log"
100+
if(!(test-path $module)){
101+
write-host "$module not found" -ForegroundColor Red
102+
exit
103+
}
104+
Import-Module $module
105+
106+
$logLocation = "C:\Example\Log.log"
107+
108+
write-log "This is an example Info" -logLocation $logLocation
109+
write-log "This is an example Error" -severity "Error" -logLocation $logLocation
110+
write-log "This is an example Warning" -severity "Warning" -logLocation $logLocation
111+
112+
Remove-Module Write-Log
113+
```
114+
Below is an example of having seperate logs for Info, Error and Warning enteries.
115+
```ps1
116+
$module = "$psscriptroot\Module\Write-Log"
117+
if(!(test-path $module)){
118+
write-host "$module not found" -ForegroundColor Red
119+
exit
120+
}
121+
Import-Module $module
122+
123+
$InfoLog = "C:\Example\Log.log"
124+
$ErrorLog = "C:\Example\Errors.log"
125+
$WarningLog = "C:\Example\Warning.log"
126+
127+
write-log "This is an example Info" -logLocation $InfoLog
128+
write-log "This is an example Error" -severity "Error" -logLocation $ErrorLog
129+
write-log "This is an example Warning" -severity "Warning" -logLocation $WarningLog
130+
131+
Remove-Module Write-Log
132+
```
133+
<p align="right">(<a href="#top">back to top</a>)</p>
134+
135+
<!-- CONTRIBUTING -->
136+
## Contributing
137+
138+
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
139+
140+
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".
141+
Don't forget to give the project a star! Thanks again!
142+
143+
1. Fork the Project
144+
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
145+
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
146+
4. Push to the Branch (`git push origin feature/AmazingFeature`)
147+
5. Open a Pull Request
148+
149+
<p align="right">(<a href="#top">back to top</a>)</p>

0 commit comments

Comments
 (0)