Skip to content

Commit

Permalink
Merge pull request #7 from claudiospizzi/dev
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
claudiospizzi committed Mar 9, 2016
2 parents 823aa48 + b509eb9 commit 80f90c8
Show file tree
Hide file tree
Showing 29 changed files with 398 additions and 100 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ bld/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

# Visual Studio Code
.vscode/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
Expand Down
2 changes: 1 addition & 1 deletion Examples/LoggerDemo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Start-ScriptLogger

# Second options, specify multiple custom settings for the logger
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
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

# Get the current script logger configuration object
Get-ScriptLogger
Expand Down
7 changes: 7 additions & 0 deletions Functions/Get-ScriptLogger.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
.EXAMPLE
C:\> Get-ScriptLogger
Get the current script logger object.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Get-ScriptLogger
Expand Down
26 changes: 23 additions & 3 deletions Functions/Set-ScriptLogger.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
.PARAMETER Level
Update the logger level.
.PARAMETER Encoding
Update the used log file encoding.
.PARAMETER LogFile
Enable or disable the log file output.
Expand All @@ -30,6 +33,13 @@
.EXAMPLE Set-ScriptLogger -Path 'C:\Temp\test.log' -Format '{3}: {4}'
C:\> Update the log file path and its format.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Set-ScriptLogger
Expand All @@ -51,17 +61,22 @@ function Set-ScriptLogger
Mandatory=$false)]
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
[String] $Level,

[Parameter(Position=3,
Mandatory=$false)]
[Boolean] $LogFile,
[ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')]
[String] $Encoding,

[Parameter(Position=4,
Mandatory=$false)]
[Boolean] $EventLog,
[Boolean] $LogFile,

[Parameter(Position=5,
Mandatory=$false)]
[Boolean] $EventLog,

[Parameter(Position=6,
Mandatory=$false)]
[Boolean] $ConsoleOutput
)

Expand Down Expand Up @@ -91,6 +106,11 @@ function Set-ScriptLogger
$Global:ScriptLogger.Level = $Level
}

if ($PSBoundParameters.ContainsKey('Encoding'))
{
$Global:ScriptLogger.Encoding = $Encoding
}

if ($PSBoundParameters.ContainsKey('LogFile'))
{
$Global:ScriptLogger.LogFile = $LogFile
Expand Down
33 changes: 33 additions & 0 deletions Functions/Show-ErrorMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<#
.SYNOPSIS
Shows an error message on the PowerShell host.
.DESCRIPTION
Uses the internal .NET method WriteErrorLine() of the host UI class to show
the error message on the console.
.PARAMETER Message
The error message.
.EXAMPLE
C:\> Show-ErrorMessage -Message 'My Error Message'
Show the error message.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Show-ErrorMessage
{
param
(
[Parameter(Mandatory=$true)]
[String] $Message
)

$Host.UI.WriteErrorLine("ERROR: $Message")
}
33 changes: 33 additions & 0 deletions Functions/Show-InformationMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<#
.SYNOPSIS
Shows an information message on the PowerShell host.
.DESCRIPTION
Uses the internal .NET method () of the host UI class to show
the information message on the console.
.PARAMETER Message
The information message.
.EXAMPLE
C:\> Show-InformationMessage -Message 'My Information Message'
Show the information message.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Show-InformationMessage
{
param
(
[Parameter(Mandatory=$true)]
[String] $Message
)

$Host.UI.WriteLine("INFORMATION: $Message")
}
33 changes: 33 additions & 0 deletions Functions/Show-VerboseMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<#
.SYNOPSIS
Shows a verbose message on the PowerShell host.
.DESCRIPTION
Uses the internal .NET method WriteVerboseLine() of the host UI class to
show the verbose message on the console.
.PARAMETER Message
The verbose message.
.EXAMPLE
C:\> Show-VerboseMessage -Message 'My Verbose Message'
Show the verbose message.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Show-VerboseMessage
{
param
(
[Parameter(Mandatory=$true)]
[String] $Message
)

$Host.UI.WriteVerboseLine($Message)
}
33 changes: 33 additions & 0 deletions Functions/Show-WarningMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<#
.SYNOPSIS
Shows a warning message on the PowerShell host.
.DESCRIPTION
Uses the internal .NET method WriteWarningLine() of the host UI class to
show the warning message on the console.
.PARAMETER Message
The warning message.
.EXAMPLE
C:\> Show-WarningMessage -Message 'My Warning Message'
Show the warning message.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Show-WarningMessage
{
param
(
[Parameter(Mandatory=$true)]
[String] $Message
)

$Host.UI.WriteWarningLine($Message)
}
23 changes: 20 additions & 3 deletions Functions/Start-ScriptLogger.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
3. Warning
4. Error
.PARAMETER Encoding
Define the encoding which is used to write the log file. The possible
options are the same as on the used Out-File cmdlet.
.PARAMETER NoLogFile
Do not write the log messages into the log file. By default, all messages
are written to the specified or default log file.
Expand All @@ -50,6 +54,13 @@
Log all message with verbose level or higher to the log file but skip the
event log and the consule output. In addition, use a custom format for the
log file content.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Start-ScriptLogger
Expand All @@ -71,17 +82,22 @@ function Start-ScriptLogger
Mandatory=$false)]
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
[String] $Level = 'Verbose',

[Parameter(Position=3,
Mandatory=$false)]
[Switch] $NoLogFile,
[ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')]
[String] $Encoding = 'Default',

[Parameter(Position=4,
Mandatory=$false)]
[Switch] $NoEventLog,
[Switch] $NoLogFile,

[Parameter(Position=5,
Mandatory=$false)]
[Switch] $NoEventLog,

[Parameter(Position=6,
Mandatory=$false)]
[Switch] $NoConsoleOutput
)

Expand All @@ -102,6 +118,7 @@ function Start-ScriptLogger
Path = $Path
Format = $Format
Level = $Level
Encoding = $Encoding
LogFile = -not $NoLogFile.IsPresent
EventLog = -not $NoEventLog.IsPresent
ConsoleOutput = -not $NoConsoleOutput.IsPresent
Expand Down
7 changes: 7 additions & 0 deletions Functions/Stop-ScriptLogger.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
.EXAMPLE
C:\> Stop-ScriptLogger
Stop the current logger.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Stop-ScriptLogger
Expand Down
24 changes: 16 additions & 8 deletions Functions/Write-ErrorLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
.EXAMPLE
C:\> Write-ErrorLog -Message 'My Error Message'
Log the error message.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Write-ErrorLog
Expand All @@ -28,14 +35,15 @@ function Write-ErrorLog
[System.Management.Automation.ErrorRecord] $ErrorRecord
)

switch ($PSCmdlet.ParameterSetName)
# Extract error message and invocation info from error record object
if ($PSCmdlet.ParameterSetName -eq 'ErrorRecord')
{
'Message' {
Write-Log -Message $Message -Level 'Error'
}

'ErrorRecord' {
Write-Log -ErrorRecord $ErrorRecord
}
$Message = '{0} ({1}: {2}:{3} char:{4})' -f $ErrorRecord.Exception.Message,
$ErrorRecord.FullyQualifiedErrorId,
$ErrorRecord.InvocationInfo.ScriptName,
$ErrorRecord.InvocationInfo.ScriptLineNumber,
$ErrorRecord.InvocationInfo.OffsetInLine
}

Write-Log -Message $Message -Level 'Error'
}
7 changes: 7 additions & 0 deletions Functions/Write-InformationLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
.EXAMPLE
C:\> Write-InformationLog -Message 'My Information Message'
Log the information message.
.NOTES
Author : Claudio Spizzi
License : MIT License
.LINK
https://github.com/claudiospizzi/ScriptLogger
#>

function Write-InformationLog
Expand Down
Loading

0 comments on commit 80f90c8

Please sign in to comment.