Skip to content

Commit 80f90c8

Browse files
committed
Merge pull request #7 from claudiospizzi/dev
Release 1.2.0
2 parents 823aa48 + b509eb9 commit 80f90c8

29 files changed

+398
-100
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ bld/
2626
# Uncomment if you have tasks that create the project's static files in wwwroot
2727
#wwwroot/
2828

29+
# Visual Studio Code
30+
.vscode/
31+
2932
# MSTest test Results
3033
[Tt]est[Rr]esult*/
3134
[Bb]uild[Ll]og.*

Examples/LoggerDemo.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Start-ScriptLogger
44

55
# Second options, specify multiple custom settings for the logger
6-
Start-ScriptLogger -Path 'C:\Temp\test.log' -Format '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' -Level Warning -SkipEventLog -HideConsoleOutput
6+
Start-ScriptLogger -Path 'C:\Temp\test.log' -Format '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' -Level Warning -Encoding 'UTF7' -SkipEventLog -HideConsoleOutput
77

88
# Get the current script logger configuration object
99
Get-ScriptLogger

Functions/Get-ScriptLogger.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
.EXAMPLE
1010
C:\> Get-ScriptLogger
1111
Get the current script logger object.
12+
13+
.NOTES
14+
Author : Claudio Spizzi
15+
License : MIT License
16+
17+
.LINK
18+
https://github.com/claudiospizzi/ScriptLogger
1219
#>
1320

1421
function Get-ScriptLogger

Functions/Set-ScriptLogger.ps1

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
.PARAMETER Level
1616
Update the logger level.
1717
18+
.PARAMETER Encoding
19+
Update the used log file encoding.
20+
1821
.PARAMETER LogFile
1922
Enable or disable the log file output.
2023
@@ -30,6 +33,13 @@
3033
3134
.EXAMPLE Set-ScriptLogger -Path 'C:\Temp\test.log' -Format '{3}: {4}'
3235
C:\> Update the log file path and its format.
36+
37+
.NOTES
38+
Author : Claudio Spizzi
39+
License : MIT License
40+
41+
.LINK
42+
https://github.com/claudiospizzi/ScriptLogger
3343
#>
3444

3545
function Set-ScriptLogger
@@ -51,17 +61,22 @@ function Set-ScriptLogger
5161
Mandatory=$false)]
5262
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
5363
[String] $Level,
54-
64+
5565
[Parameter(Position=3,
5666
Mandatory=$false)]
57-
[Boolean] $LogFile,
67+
[ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')]
68+
[String] $Encoding,
5869

5970
[Parameter(Position=4,
6071
Mandatory=$false)]
61-
[Boolean] $EventLog,
72+
[Boolean] $LogFile,
6273

6374
[Parameter(Position=5,
6475
Mandatory=$false)]
76+
[Boolean] $EventLog,
77+
78+
[Parameter(Position=6,
79+
Mandatory=$false)]
6580
[Boolean] $ConsoleOutput
6681
)
6782

@@ -91,6 +106,11 @@ function Set-ScriptLogger
91106
$Global:ScriptLogger.Level = $Level
92107
}
93108

109+
if ($PSBoundParameters.ContainsKey('Encoding'))
110+
{
111+
$Global:ScriptLogger.Encoding = $Encoding
112+
}
113+
94114
if ($PSBoundParameters.ContainsKey('LogFile'))
95115
{
96116
$Global:ScriptLogger.LogFile = $LogFile

Functions/Show-ErrorMessage.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Shows an error message on the PowerShell host.
4+
5+
.DESCRIPTION
6+
Uses the internal .NET method WriteErrorLine() of the host UI class to show
7+
the error message on the console.
8+
9+
.PARAMETER Message
10+
The error message.
11+
12+
.EXAMPLE
13+
C:\> Show-ErrorMessage -Message 'My Error Message'
14+
Show the error message.
15+
16+
.NOTES
17+
Author : Claudio Spizzi
18+
License : MIT License
19+
20+
.LINK
21+
https://github.com/claudiospizzi/ScriptLogger
22+
#>
23+
24+
function Show-ErrorMessage
25+
{
26+
param
27+
(
28+
[Parameter(Mandatory=$true)]
29+
[String] $Message
30+
)
31+
32+
$Host.UI.WriteErrorLine("ERROR: $Message")
33+
}

Functions/Show-InformationMessage.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Shows an information message on the PowerShell host.
4+
5+
.DESCRIPTION
6+
Uses the internal .NET method () of the host UI class to show
7+
the information message on the console.
8+
9+
.PARAMETER Message
10+
The information message.
11+
12+
.EXAMPLE
13+
C:\> Show-InformationMessage -Message 'My Information Message'
14+
Show the information message.
15+
16+
.NOTES
17+
Author : Claudio Spizzi
18+
License : MIT License
19+
20+
.LINK
21+
https://github.com/claudiospizzi/ScriptLogger
22+
#>
23+
24+
function Show-InformationMessage
25+
{
26+
param
27+
(
28+
[Parameter(Mandatory=$true)]
29+
[String] $Message
30+
)
31+
32+
$Host.UI.WriteLine("INFORMATION: $Message")
33+
}

Functions/Show-VerboseMessage.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Shows a verbose message on the PowerShell host.
4+
5+
.DESCRIPTION
6+
Uses the internal .NET method WriteVerboseLine() of the host UI class to
7+
show the verbose message on the console.
8+
9+
.PARAMETER Message
10+
The verbose message.
11+
12+
.EXAMPLE
13+
C:\> Show-VerboseMessage -Message 'My Verbose Message'
14+
Show the verbose message.
15+
16+
.NOTES
17+
Author : Claudio Spizzi
18+
License : MIT License
19+
20+
.LINK
21+
https://github.com/claudiospizzi/ScriptLogger
22+
#>
23+
24+
function Show-VerboseMessage
25+
{
26+
param
27+
(
28+
[Parameter(Mandatory=$true)]
29+
[String] $Message
30+
)
31+
32+
$Host.UI.WriteVerboseLine($Message)
33+
}

Functions/Show-WarningMessage.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Shows a warning message on the PowerShell host.
4+
5+
.DESCRIPTION
6+
Uses the internal .NET method WriteWarningLine() of the host UI class to
7+
show the warning message on the console.
8+
9+
.PARAMETER Message
10+
The warning message.
11+
12+
.EXAMPLE
13+
C:\> Show-WarningMessage -Message 'My Warning Message'
14+
Show the warning message.
15+
16+
.NOTES
17+
Author : Claudio Spizzi
18+
License : MIT License
19+
20+
.LINK
21+
https://github.com/claudiospizzi/ScriptLogger
22+
#>
23+
24+
function Show-WarningMessage
25+
{
26+
param
27+
(
28+
[Parameter(Mandatory=$true)]
29+
[String] $Message
30+
)
31+
32+
$Host.UI.WriteWarningLine($Message)
33+
}

Functions/Start-ScriptLogger.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
3. Warning
3030
4. Error
3131
32+
.PARAMETER Encoding
33+
Define the encoding which is used to write the log file. The possible
34+
options are the same as on the used Out-File cmdlet.
35+
3236
.PARAMETER NoLogFile
3337
Do not write the log messages into the log file. By default, all messages
3438
are written to the specified or default log file.
@@ -50,6 +54,13 @@
5054
Log all message with verbose level or higher to the log file but skip the
5155
event log and the consule output. In addition, use a custom format for the
5256
log file content.
57+
58+
.NOTES
59+
Author : Claudio Spizzi
60+
License : MIT License
61+
62+
.LINK
63+
https://github.com/claudiospizzi/ScriptLogger
5364
#>
5465

5566
function Start-ScriptLogger
@@ -71,17 +82,22 @@ function Start-ScriptLogger
7182
Mandatory=$false)]
7283
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
7384
[String] $Level = 'Verbose',
74-
85+
7586
[Parameter(Position=3,
7687
Mandatory=$false)]
77-
[Switch] $NoLogFile,
88+
[ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')]
89+
[String] $Encoding = 'Default',
7890

7991
[Parameter(Position=4,
8092
Mandatory=$false)]
81-
[Switch] $NoEventLog,
93+
[Switch] $NoLogFile,
8294

8395
[Parameter(Position=5,
8496
Mandatory=$false)]
97+
[Switch] $NoEventLog,
98+
99+
[Parameter(Position=6,
100+
Mandatory=$false)]
85101
[Switch] $NoConsoleOutput
86102
)
87103

@@ -102,6 +118,7 @@ function Start-ScriptLogger
102118
Path = $Path
103119
Format = $Format
104120
Level = $Level
121+
Encoding = $Encoding
105122
LogFile = -not $NoLogFile.IsPresent
106123
EventLog = -not $NoEventLog.IsPresent
107124
ConsoleOutput = -not $NoConsoleOutput.IsPresent

Functions/Stop-ScriptLogger.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
.EXAMPLE
1010
C:\> Stop-ScriptLogger
1111
Stop the current logger.
12+
13+
.NOTES
14+
Author : Claudio Spizzi
15+
License : MIT License
16+
17+
.LINK
18+
https://github.com/claudiospizzi/ScriptLogger
1219
#>
1320

1421
function Stop-ScriptLogger

Functions/Write-ErrorLog.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
.EXAMPLE
1313
C:\> Write-ErrorLog -Message 'My Error Message'
1414
Log the error message.
15+
16+
.NOTES
17+
Author : Claudio Spizzi
18+
License : MIT License
19+
20+
.LINK
21+
https://github.com/claudiospizzi/ScriptLogger
1522
#>
1623

1724
function Write-ErrorLog
@@ -28,14 +35,15 @@ function Write-ErrorLog
2835
[System.Management.Automation.ErrorRecord] $ErrorRecord
2936
)
3037

31-
switch ($PSCmdlet.ParameterSetName)
38+
# Extract error message and invocation info from error record object
39+
if ($PSCmdlet.ParameterSetName -eq 'ErrorRecord')
3240
{
33-
'Message' {
34-
Write-Log -Message $Message -Level 'Error'
35-
}
36-
37-
'ErrorRecord' {
38-
Write-Log -ErrorRecord $ErrorRecord
39-
}
41+
$Message = '{0} ({1}: {2}:{3} char:{4})' -f $ErrorRecord.Exception.Message,
42+
$ErrorRecord.FullyQualifiedErrorId,
43+
$ErrorRecord.InvocationInfo.ScriptName,
44+
$ErrorRecord.InvocationInfo.ScriptLineNumber,
45+
$ErrorRecord.InvocationInfo.OffsetInLine
4046
}
47+
48+
Write-Log -Message $Message -Level 'Error'
4149
}

Functions/Write-InformationLog.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
.EXAMPLE
1414
C:\> Write-InformationLog -Message 'My Information Message'
1515
Log the information message.
16+
17+
.NOTES
18+
Author : Claudio Spizzi
19+
License : MIT License
20+
21+
.LINK
22+
https://github.com/claudiospizzi/ScriptLogger
1623
#>
1724

1825
function Write-InformationLog

0 commit comments

Comments
 (0)