Skip to content

Commit 224b9ee

Browse files
author
Antony Bragg
committed
Update.
Date update and other user suggestions
1 parent a82c56b commit 224b9ee

File tree

3 files changed

+100
-23
lines changed

3 files changed

+100
-23
lines changed

Class/Write-Log/Write-Log-Class.psm1

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class WriteLog {
2-
[string]$LogLocation = "$PSScriptRoot\log.log"
2+
hidden [string]$LogLocation
3+
[string]$DateFormat
34

45
AddError([string]$Message) {
56
$this.AddEntry($message,"Error")
@@ -15,14 +16,30 @@ class WriteLog {
1516

1617
WriteLog([string]$LogLocation) {
1718
$this.LogLocation = $LogLocation
19+
if(!($this.ValidateLogFile($this.LogLocation))) { throw "Invalid log file extension. Please use .log or .txt" }
20+
}
21+
22+
hidden [bool]ValidateLogFile([string]$logFIle) {
23+
$extension = [System.IO.Path]::GetExtension($logFIle)
24+
if(($extension -eq ".log") -or ($extension -eq ".txt")) {
25+
return $true
26+
} else {
27+
return $false
28+
}
1829
}
1930

2031
hidden AddEntry([string]$Message,[string]$severity) {
2132
if(!(test-path $this.LogLocation)) {
2233
new-item $this.LogLocation -Force
2334
}
2435

25-
$timeStamp = Get-date -Format "dd/MM/yyyy HH:mm:ss"
36+
if($null -eq $this.DateFormat) {
37+
[DateTime]$Date = Get-Date
38+
$timeStamp = $date.ToShortDateString() + " " + $date.ToShortTimeString()
39+
} else {
40+
$timeStamp = Get-date -Format $this.DateFormat
41+
}
42+
2643
$Output = "$timeStamp - [$($severity)] $($Message)"
2744
Add-Content $this.logLocation -value $Output
2845
Write-Host "$Output"

Module/Write-Log/Write-Log.psm1

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,77 @@ write-log -Message "Oh no Jurgen was here"
2626
write-log -Message "Oh no Jurgen was here" -severity "Error"
2727
.EXAMPLE
2828
write-log -Message "Oh no Jurgen was here" -severity "Error" -LogLocation "C:\temp\ffsJurgen.log"
29+
<#
30+
.SYNOPSIS
31+
Creates entries to a log file.
32+
33+
.DESCRIPTION
34+
Creates an entry of a specified severity into the log file.
35+
36+
.OUTPUTS
37+
A *.log or *.txt file with specific name or location.
38+
39+
.PARAMETER Message
40+
The log message entered into the log file.
41+
42+
.PARAMETER severity
43+
The severity set for the log message within the log file. (Info, Warning, Error).
44+
45+
.PARAMETER logLocation
46+
The directory of the log file. If left blank the log will be located in the Write-Log module folder.
47+
48+
.PARAMETER DateFormat
49+
String format if a specific date format is desired.
50+
51+
.EXAMPLE
52+
write-log -Message "Info worthy log"
53+
.EXAMPLE
54+
write-log -Message "Error message!! OH NO!" -severity "Error"
55+
.EXAMPLE
56+
write-log -Message "Errorrrr!!!!" -severity "Error" -LogLocation "C:\temp\Events.log"
57+
58+
.NOTES
59+
This module can be found in the following GitHub Repo: https://github.com/PowershellGroup/Write-Log-Module
60+
#>
61+
62+
Function Write-Log {
63+
Param(
64+
[Parameter(Position=0, Mandatory=$true, HelpMessage="Enter your log message.")]
65+
[string]$Message,
66+
67+
[Parameter(Position=1, HelpMessage="Enter the log level severity between.")]
68+
[ArgumentCompleter({'Info','Warning','Error'})]
69+
[string]$severity = "Info",
70+
71+
[Parameter(Position=2, HelpMessage="Directory for the log file. Extension must be .log or .txt")]
72+
[string]$logLocation = "$PSScriptRoot\Events.log",
73+
74+
[Parameter(Position=3, HelpMessage="String format of the date. E.g. dd/MM/yyyy")]
75+
[string]$DateFormat
76+
)
77+
78+
$extension = [System.IO.Path]::GetExtension($logLocation)
79+
if(!($extension -eq ".log") -and !($extension -eq ".txt")) {
80+
throw "Invalid log file extension. Please use .log or .txt"
81+
}
82+
83+
if(!(test-path $logLocation)) {
84+
new-item $logLocation -Force | out-null
85+
}
86+
87+
if($null -eq $DateFormat) {
88+
[DateTime]$Date = Get-Date
89+
$timeStamp = $date.ToShortDateString() + " " + $date.ToShortTimeString()
90+
} else {
91+
$timeStamp = Get-date -Format $DateFormat
92+
}
93+
94+
$Output = "$timeStamp - [$severity] $Message"
95+
Add-Content $logLocation -value $Output
96+
Write-Host "$Output"
97+
}
2998

99+
Export-ModuleMember -Function Write-Log
30100
.NOTES
31101
This module can be found in the following GitHub Repo: https://github.com/captainqwerty/Write-Log
32102
#>

README.md

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ This project was to just offer people a easy way to quickly add the ability to o
2626

2727
### Class Version
2828

29-
The class Version is the preferred version however this version is limited to PowerShell Version 5.0 and greater and utilizes the *Using* statement.
29+
The class Version is the preferred version however this version is limited to PowerShell Version 5.0 and greater and utilises the *Using* statement.
3030

3131
### Module Version
3232

33-
The Module Version is available for those preferring 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.
33+
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.
3434

3535
<p align="right">(<a href="#top">back to top</a>)</p>
3636

@@ -43,7 +43,6 @@ To get a local copy up and running follow these simple example steps.
4343
### Prerequisites
4444

4545
This is an example of how to list things you need to use the software and how to install them.
46-
4746
* PowerShell version 2.0 or greater - Module Version.
4847
* PowerShell version 5.0 or greater - Either version.
4948

@@ -52,21 +51,18 @@ This is an example of how to list things you need to use the software and how to
5251
1. Download the <a href="https://github.com/PowershellGroup/Write-Log-Module/release">latest release</a>.
5352
2. Extract the zipped folder.
5453
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.
55-
4. Add the Using statement 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.
56-
54+
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.
5755
```ps1
5856
using module ".\Write-Log\Write-Log-Class.psm1"
5957
```
60-
61-
1. See <a href="#usage-of-the-class-version">Class Version Usage</a> section for examples on how to configure the log location and add entries.
58+
5. 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.
6259

6360
### Installation of Module Version
6461

6562
1. Download the <a href="https://github.com/PowershellGroup/Write-Log-Module/releases">latest release</a>.
6663
2. Extra the zipped folder.
6764
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.
68-
4. Import the Module. In this example the Write-Log folder is in the root of the project folder.
69-
65+
4. Import the Module. In this example the Write-Log folder is in the root of the project folder.
7066
```ps1
7167
$module = "$PSScriptRoot\Write-Log"
7268
if(!(test-path $module)){
@@ -75,10 +71,8 @@ if(!(test-path $module)){
7571
}
7672
Import-Module $module
7773
```
78-
79-
1. See <a href="#usage-of-the-module-version">Module Version Usage</a> section for examples on how to configure the log location and add entries.
80-
2. Add the Remove-Module line to the bottom of your script.
81-
74+
5. 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.
75+
6. Add the Remove-Module line to the bottom of your script.
8276
```ps1
8377
Remove-Module Write-Log
8478
```
@@ -87,7 +81,6 @@ Remove-Module Write-Log
8781

8882
<!-- USAGE EXAMPLES -->
8983
## Usage of the Class Version
90-
9184
```ps1
9285
using module ".\Class\Write-Log\Write-Log-Class.psm1"
9386
@@ -98,8 +91,7 @@ $Log.AddError("There was a huge error!")
9891
$Log.AddWarning("Oh dear, I should really warn you about this!")
9992
$Log.AddEntry("Testing","Test Severity") #This method is hidden but can be used for custom severities
10093
```
101-
102-
The below example shows having multiple Write-Log objects to store different types or log entries in different logs.
94+
The below example shows having mutliple Write-Log objects to store different types or log enteries in different logs.
10395

10496
```ps1
10597
using module ".\Class\Write-Log\Write-Log-Class.psm1"
@@ -114,7 +106,6 @@ $WarningLog.AddWarning("Oh dear, I should really warn you about this!")
114106
```
115107

116108
## Usage of the Module version
117-
118109
```ps1
119110
$module = "$PSScriptRoot\Module\Write-Log"
120111
if(!(test-path $module)){
@@ -131,9 +122,7 @@ write-log "This is an example Warning" -severity "Warning" -logLocation $logLoca
131122
132123
Remove-Module Write-Log
133124
```
134-
135-
Below is an example of having separate logs for Info, Error and Warning entries.
136-
125+
Below is an example of having seperate logs for Info, Error and Warning enteries.
137126
```ps1
138127
$module = "$PSScriptRoot\Module\Write-Log"
139128
if(!(test-path $module)){
@@ -152,7 +141,6 @@ write-log "This is an example Warning" -severity "Warning" -logLocation $Warning
152141
153142
Remove-Module Write-Log
154143
```
155-
156144
<p align="right">(<a href="#top">back to top</a>)</p>
157145

158146
<!-- CONTRIBUTING -->
@@ -175,6 +163,8 @@ Don't forget to give the project a star! Thanks again!
175163
## Acknowledgments
176164

177165
* [Othneildrew's Best-README-Template](https://github.com/othneildrew/Best-README-Template)
166+
* [matthewjdegarmo](https://github.com/matthewjdegarmo) - Excellent suggestion on moving from ValidationSet in the module version and improvements / corrections to the ReadMe
167+
* [SeidChr](https://github.com/SeidChr) - For prompting the addition of .txt
178168

179169
<p align="right">(<a href="#top">back to top</a>)</p>
180170

0 commit comments

Comments
 (0)