Skip to content

Commit 472f2d8

Browse files
committed
Added: Optional log rotation (hourly, daily, monthly, yearly)
1 parent cb2619b commit 472f2d8

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is mainly based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## 3.1.0 - 2019-02-18
10+
11+
* Added: Optional log rotation (hourly, daily, monthly, yearly)
12+
13+
914
## 3.0.1 - 2019-02-14
1015

1116
* Changed: Format of the script logger object to show the logger name

Modules/ScriptLogger/Functions/Start-ScriptLogger.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ function Start-ScriptLogger
7979
[System.String]
8080
$Encoding = 'UTF8',
8181

82+
# Allow the auto log rotation. The period is attached to the log file.
83+
[Parameter(Mandatory = $false)]
84+
[ValidateSet('None', 'Hourly', 'Daily', 'Monthly', 'Yearly')]
85+
[System.String]
86+
$Rotation = 'None',
87+
8288
# Do not write the log messages into the log file. By default, all
8389
# messages are written to the specified or default log file.
8490
[Parameter(Mandatory = $false)]
@@ -119,6 +125,27 @@ function Start-ScriptLogger
119125
}
120126
}
121127

128+
# If log rotation is enable, add the current period to the path
129+
switch ($Rotation)
130+
{
131+
'Hourly'
132+
{
133+
$Path = $Path.Insert($Path.LastIndexOf('.'), [System.String]::Format('.{0:yyyyMMddHH}', (Get-Date)))
134+
}
135+
'Daily'
136+
{
137+
$Path = $Path.Insert($Path.LastIndexOf('.'), [System.String]::Format('.{0:yyyyMMdd}', (Get-Date)))
138+
}
139+
'Monthly'
140+
{
141+
$Path = $Path.Insert($Path.LastIndexOf('.'), [System.String]::Format('.{0:yyyyMM}', (Get-Date)))
142+
}
143+
'Yearly'
144+
{
145+
$Path = $Path.Insert($Path.LastIndexOf('.'), [System.String]::Format('.{0:yyyy}', (Get-Date)))
146+
}
147+
}
148+
122149
# Create an empty log file, if it does not exist
123150
if (-not (Test-Path -Path $Path))
124151
{
@@ -147,6 +174,7 @@ function Start-ScriptLogger
147174
Format = $Format
148175
Level = $Level
149176
Encoding = $Encoding
177+
Rotation = $Rotation
150178
LogFile = -not $NoLogFile.IsPresent
151179
EventLog = -not $NoEventLog.IsPresent
152180
ConsoleOutput = -not $NoConsoleOutput.IsPresent

Modules/ScriptLogger/ScriptLogger.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RootModule = 'ScriptLogger.psm1'
44

55
# Version number of this module.
6-
ModuleVersion = '3.0.1'
6+
ModuleVersion = '3.1.0'
77

88
# Supported PSEditions
99
# CompatiblePSEditions = @()

Modules/ScriptLogger/Tests/Unit/Start-ScriptLogger.Tests.ps1

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ Import-Module -Name "$modulePath\$moduleName" -Force
77

88
Describe 'Start-ScriptLogger' {
99

10+
Mock Get-Date { return [DateTime]::new(2010, 12, 06, 18, 20, 22) } -ModuleName $moduleName
11+
1012
$defaultEnabled = $true
1113
$defaultPath = "$PSScriptRoot\Start-ScriptLogger.Tests.ps1.log"
1214
$defaultFormat = '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}'
1315
$defaultLevel = 'Verbose'
1416
$defaultEncoding = 'UTF8'
17+
$defaultRotation = 'None'
1518
$defaultLogFile = $true
1619
$defaultEventLog = $true
1720
$defaultConsole = $true
@@ -28,6 +31,7 @@ Describe 'Start-ScriptLogger' {
2831
$scriptLogger.Format | Should -Be $defaultFormat
2932
$scriptLogger.Level | Should -Be $defaultLevel
3033
$scriptLogger.Encoding | Should -Be $defaultEncoding
34+
$scriptLogger.Rotation | Should -Be $defaultRotation
3135
$scriptLogger.LogFile | Should -Be $defaultLogFile
3236
$scriptLogger.EventLog | Should -Be $defaultEventLog
3337
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
@@ -48,13 +52,15 @@ Describe 'Start-ScriptLogger' {
4852
$scriptLogger.Format | Should -Be $defaultFormat
4953
$scriptLogger.Level | Should -Be $defaultLevel
5054
$scriptLogger.Encoding | Should -Be $defaultEncoding
55+
$scriptLogger.Rotation | Should -Be $defaultRotation
5156
$scriptLogger.LogFile | Should -Be $defaultLogFile
5257
$scriptLogger.EventLog | Should -Be $defaultEventLog
5358
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
5459
}
5560

5661
It 'should return a valid value for the parameter format' {
5762

63+
# Arrange
5864
$expectedFormat = '{4} {3} {2} {1} {0}'
5965

6066
# Act
@@ -67,13 +73,15 @@ Describe 'Start-ScriptLogger' {
6773
$scriptLogger.Format | Should -Be $expectedFormat
6874
$scriptLogger.Level | Should -Be $defaultLevel
6975
$scriptLogger.Encoding | Should -Be $defaultEncoding
76+
$scriptLogger.Rotation | Should -Be $defaultRotation
7077
$scriptLogger.LogFile | Should -Be $defaultLogFile
7178
$scriptLogger.EventLog | Should -Be $defaultEventLog
7279
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
7380
}
7481

7582
It 'should return a valid value for the parameter log level' {
7683

84+
# Arrange
7785
$expectedLevel = 'Error'
7886

7987
# Act
@@ -86,13 +94,15 @@ Describe 'Start-ScriptLogger' {
8694
$scriptLogger.Format | Should -Be $defaultFormat
8795
$scriptLogger.Level | Should -Be $expectedLevel
8896
$scriptLogger.Encoding | Should -Be $defaultEncoding
97+
$scriptLogger.Rotation | Should -Be $defaultRotation
8998
$scriptLogger.LogFile | Should -Be $defaultLogFile
9099
$scriptLogger.EventLog | Should -Be $defaultEventLog
91100
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
92101
}
93102

94103
It 'should return a valid value for the parameter encoding' {
95104

105+
# Arrange
96106
$expectedEncoding = 'UTF8'
97107

98108
# Act
@@ -105,6 +115,96 @@ Describe 'Start-ScriptLogger' {
105115
$scriptLogger.Format | Should -Be $defaultFormat
106116
$scriptLogger.Level | Should -Be $defaultLevel
107117
$scriptLogger.Encoding | Should -Be $expectedEncoding
118+
$scriptLogger.Rotation | Should -Be $defaultRotation
119+
$scriptLogger.LogFile | Should -Be $defaultLogFile
120+
$scriptLogger.EventLog | Should -Be $defaultEventLog
121+
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
122+
}
123+
124+
It 'should return a valid value for the parameter rotation hourly' {
125+
126+
# Arrange
127+
$expectedRotation = 'Hourly'
128+
$expectedPath = "$PSScriptRoot\Start-ScriptLogger.Tests.ps1.2010120618.log"
129+
130+
# Act
131+
$scriptLogger = Start-ScriptLogger -Rotation $expectedRotation -PassThru
132+
133+
# Assert
134+
$scriptLogger | Should -Not -BeNullOrEmpty
135+
$scriptLogger.Enabled | Should -Be $defaultEnabled
136+
$scriptLogger.Path | Should -Be $expectedPath
137+
$scriptLogger.Format | Should -Be $defaultFormat
138+
$scriptLogger.Level | Should -Be $defaultLevel
139+
$scriptLogger.Encoding | Should -Be $defaultEncoding
140+
$scriptLogger.Rotation | Should -Be $expectedRotation
141+
$scriptLogger.LogFile | Should -Be $defaultLogFile
142+
$scriptLogger.EventLog | Should -Be $defaultEventLog
143+
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
144+
}
145+
146+
147+
It 'should return a valid value for the parameter rotation daily' {
148+
149+
# Arrange
150+
$expectedRotation = 'Daily'
151+
$expectedPath = "$PSScriptRoot\Start-ScriptLogger.Tests.ps1.20101206.log"
152+
153+
# Act
154+
$scriptLogger = Start-ScriptLogger -Rotation $expectedRotation -PassThru
155+
156+
# Assert
157+
$scriptLogger | Should -Not -BeNullOrEmpty
158+
$scriptLogger.Enabled | Should -Be $defaultEnabled
159+
$scriptLogger.Path | Should -Be $expectedPath
160+
$scriptLogger.Format | Should -Be $defaultFormat
161+
$scriptLogger.Level | Should -Be $defaultLevel
162+
$scriptLogger.Encoding | Should -Be $defaultEncoding
163+
$scriptLogger.Rotation | Should -Be $expectedRotation
164+
$scriptLogger.LogFile | Should -Be $defaultLogFile
165+
$scriptLogger.EventLog | Should -Be $defaultEventLog
166+
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
167+
}
168+
169+
It 'should return a valid value for the parameter rotation monthly' {
170+
171+
# Arrange
172+
$expectedRotation = 'Monthly'
173+
$expectedPath = "$PSScriptRoot\Start-ScriptLogger.Tests.ps1.201012.log"
174+
175+
# Act
176+
$scriptLogger = Start-ScriptLogger -Rotation $expectedRotation -PassThru
177+
178+
# Assert
179+
$scriptLogger | Should -Not -BeNullOrEmpty
180+
$scriptLogger.Enabled | Should -Be $defaultEnabled
181+
$scriptLogger.Path | Should -Be $expectedPath
182+
$scriptLogger.Format | Should -Be $defaultFormat
183+
$scriptLogger.Level | Should -Be $defaultLevel
184+
$scriptLogger.Encoding | Should -Be $defaultEncoding
185+
$scriptLogger.Rotation | Should -Be $expectedRotation
186+
$scriptLogger.LogFile | Should -Be $defaultLogFile
187+
$scriptLogger.EventLog | Should -Be $defaultEventLog
188+
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
189+
}
190+
191+
It 'should return a valid value for the parameter rotation yearly' {
192+
193+
# Arrange
194+
$expectedRotation = 'Yearly'
195+
$expectedPath = "$PSScriptRoot\Start-ScriptLogger.Tests.ps1.2010.log"
196+
197+
# Act
198+
$scriptLogger = Start-ScriptLogger -Rotation $expectedRotation -PassThru
199+
200+
# Assert
201+
$scriptLogger | Should -Not -BeNullOrEmpty
202+
$scriptLogger.Enabled | Should -Be $defaultEnabled
203+
$scriptLogger.Path | Should -Be $expectedPath
204+
$scriptLogger.Format | Should -Be $defaultFormat
205+
$scriptLogger.Level | Should -Be $defaultLevel
206+
$scriptLogger.Encoding | Should -Be $defaultEncoding
207+
$scriptLogger.Rotation | Should -Be $expectedRotation
108208
$scriptLogger.LogFile | Should -Be $defaultLogFile
109209
$scriptLogger.EventLog | Should -Be $defaultEventLog
110210
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
@@ -122,6 +222,7 @@ Describe 'Start-ScriptLogger' {
122222
$scriptLogger.Format | Should -Be $defaultFormat
123223
$scriptLogger.Level | Should -Be $defaultLevel
124224
$scriptLogger.Encoding | Should -Be $defaultEncoding
225+
$scriptLogger.Rotation | Should -Be $defaultRotation
125226
$scriptLogger.LogFile | Should -Be $false
126227
$scriptLogger.EventLog | Should -Be $defaultEventLog
127228
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
@@ -139,6 +240,7 @@ Describe 'Start-ScriptLogger' {
139240
$scriptLogger.Format | Should -Be $defaultFormat
140241
$scriptLogger.Level | Should -Be $defaultLevel
141242
$scriptLogger.Encoding | Should -Be $defaultEncoding
243+
$scriptLogger.Rotation | Should -Be $defaultRotation
142244
$scriptLogger.LogFile | Should -Be $defaultLogFile
143245
$scriptLogger.EventLog | Should -Be $false
144246
$scriptLogger.ConsoleOutput | Should -Be $defaultConsole
@@ -156,6 +258,7 @@ Describe 'Start-ScriptLogger' {
156258
$scriptLogger.Format | Should -Be $defaultFormat
157259
$scriptLogger.Level | Should -Be $defaultLevel
158260
$scriptLogger.Encoding | Should -Be $defaultEncoding
261+
$scriptLogger.Rotation | Should -Be $defaultRotation
159262
$scriptLogger.LogFile | Should -Be $defaultLogFile
160263
$scriptLogger.EventLog | Should -Be $defaultEventLog
161264
$scriptLogger.ConsoleOutput | Should -Be $false

0 commit comments

Comments
 (0)