Skip to content

Commit 48315b0

Browse files
committed
Merge pull request #1 from claudiospizzi/dev
Release 1.0.0
2 parents 27d1c9a + a18d1f3 commit 48315b0

27 files changed

+1503
-1
lines changed

Examples/LoggerDemo.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# Initialize the logger with default values
3+
Start-ScriptLogger
4+
5+
# 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
7+
8+
# Get the current script logger configuration object
9+
Get-ScriptLogger
10+
11+
# Update the script logger configuration
12+
Set-ScriptLogger -Level Verbose
13+
14+
# Log an error message
15+
Write-ErrorLog -Message 'My Error Message'
16+
17+
# Log a warning massage
18+
Write-WarningLog -Message 'My Warning Message'
19+
20+
# Log an information message
21+
Write-InformationLog -Message 'My Information Message'
22+
23+
# Log a verbose message
24+
Write-VerboseLog -Message 'My Verbose Message'
25+
26+
# Disable the logger
27+
Stop-ScriptLogger

Examples/LoggerDemo.ps1.log

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2000-12-31 01:02:03 COMPUTERNAME username Error My Error Message
2+
2000-12-31 02:03:04 COMPUTERNAME username Warning My Warning Message
3+
2000-12-31 03:04:05 COMPUTERNAME username Information My Information Message
4+
2000-12-31 04:05:06 COMPUTERNAME username Verbose My Verbose Message

Functions/Get-ScriptLogger.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<#
2+
.SYNOPSIS
3+
Get the current script logger.
4+
5+
.DESCRIPTION
6+
Returns an object with the current configuration of the script logger
7+
inside this PowerShell session.
8+
9+
.EXAMPLE
10+
C:\> Get-ScriptLogger
11+
Get the current script logger object.
12+
#>
13+
14+
function Get-ScriptLogger
15+
{
16+
[CmdletBinding()]
17+
param
18+
(
19+
)
20+
21+
if ($Global:ScriptLogger -ne $null)
22+
{
23+
return $Global:ScriptLogger
24+
}
25+
else
26+
{
27+
throw 'Script logger not found!'
28+
}
29+
}

Functions/Set-ScriptLogger.ps1

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<#
2+
.SYNOPSIS
3+
Update the script logger log configuration.
4+
5+
.DESCRIPTION
6+
The script logger inside the current PowerShell session can be updated with
7+
all parameters inside this cmdlet.
8+
9+
.PARAMETER Path
10+
Update the path to the log file.
11+
12+
.PARAMETER Format
13+
Update the format for the log file.
14+
15+
.PARAMETER Level
16+
Update the logger level.
17+
18+
.PARAMETER LogFile
19+
Enable or disable the log file output.
20+
21+
.PARAMETER EventLog
22+
Enable or disable the event log output.
23+
24+
.PARAMETER ConsoleOutput
25+
Enable or disable the console output.
26+
27+
.EXAMPLE
28+
C:\> Set-ScriptLogger -Level 'Warning' -EventLog $true
29+
Set the script logger level to warning and enable the event log output.
30+
31+
.EXAMPLE Set-ScriptLogger -Path 'C:\Temp\test.log' -Format '{3}: {4}'
32+
C:\> Update the log file path and its format.
33+
#>
34+
35+
function Set-ScriptLogger
36+
{
37+
[CmdletBinding()]
38+
param
39+
(
40+
[Parameter(Position=0,
41+
Mandatory=$false)]
42+
[ValidateScript({Test-Path -Path (Split-Path -Path $_ -Parent)})]
43+
[String] $Path,
44+
45+
[Parameter(Position=1,
46+
Mandatory=$false)]
47+
[ValidateScript({$_ -f (Get-Date), $Env:ComputerName, $Env:Username, 'Verbose', 'My Message'})]
48+
[String] $Format,
49+
50+
[Parameter(Position=2,
51+
Mandatory=$false)]
52+
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
53+
[String] $Level,
54+
55+
[Parameter(Position=3,
56+
Mandatory=$false)]
57+
[Boolean] $LogFile,
58+
59+
[Parameter(Position=4,
60+
Mandatory=$false)]
61+
[Boolean] $EventLog,
62+
63+
[Parameter(Position=5,
64+
Mandatory=$false)]
65+
[Boolean] $ConsoleOutput
66+
)
67+
68+
if ($Global:ScriptLogger -ne $null)
69+
{
70+
if ($PSBoundParameters.ContainsKey('Path'))
71+
{
72+
# Create an empty log file, if it does not exist
73+
if (-not (Test-Path -Path $Path))
74+
{
75+
New-Item -Path $Path -ItemType File | Out-Null
76+
}
77+
78+
# Only work with absolute path, makes error handling easier
79+
$Path = (Resolve-Path -Path $Path).Path
80+
81+
$Global:ScriptLogger.Path = $Path
82+
}
83+
84+
if ($PSBoundParameters.ContainsKey('Format'))
85+
{
86+
$Global:ScriptLogger.Format = $Format
87+
}
88+
89+
if ($PSBoundParameters.ContainsKey('Level'))
90+
{
91+
$Global:ScriptLogger.Level = $Level
92+
}
93+
94+
if ($PSBoundParameters.ContainsKey('LogFile'))
95+
{
96+
$Global:ScriptLogger.LogFile = $LogFile
97+
}
98+
99+
if ($PSBoundParameters.ContainsKey('EventLog'))
100+
{
101+
$Global:ScriptLogger.EventLog = $EventLog
102+
}
103+
104+
if ($PSBoundParameters.ContainsKey('ConsoleOutput'))
105+
{
106+
$Global:ScriptLogger.ConsoleOutput = $ConsoleOutput
107+
}
108+
}
109+
}

Functions/Start-ScriptLogger.ps1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<#
2+
.SYNOPSIS
3+
Start the script logger inside the current PowerShell session.
4+
5+
.DESCRIPTION
6+
Start the script logger inside the current PowerShell session. By starting
7+
the logger, a global log configuration for the current PowerShell session
8+
will be set. This configuration is customizable with the available
9+
paramters.
10+
11+
.PARAMETER Path
12+
The path to the log file.
13+
14+
.PARAMETER Format
15+
This parameter defines, how the log output will be formated. The format
16+
definition will be used to call the System.String.Format() method. The
17+
following values are used as arguments:
18+
{0} Timestamp as datetime value.
19+
{1} NetBIOS computer name.
20+
{2} Current session username.
21+
{3} Log entry level.
22+
{4} Message.
23+
24+
.PARAMETER Level
25+
The event log level. All log messages equal to or higher to the level will
26+
be logged. This is the level order:
27+
1. Verbose
28+
2. Information
29+
3. Warning
30+
4. Error
31+
32+
.PARAMETER NoLogFile
33+
Do not write the log messages into the log file. By default, all messages
34+
are written to the specified or default log file.
35+
36+
.PARAMETER NoEventLog
37+
Skip the event log output. By default, all log messages will be written
38+
into the "Windows PowerShell" event log.
39+
40+
.PARAMETER NoConsoleOutput
41+
Hide the PowerShell console output. By default, all log messages are shown
42+
on the console.
43+
44+
.EXAMPLE
45+
C:\> Start-ScriptLogger
46+
Initialize the logger with default values
47+
48+
.EXAMPLE
49+
C:\>Start-ScriptLogger -Path 'C:\test.log' -Format '{3}: {4}' -Level 'Verbose' -SkipEventLog -HideConsoleOutput
50+
Log all message with verbose level or higher to the log file but skip the
51+
event log and the consule output. In addition, use a custom format for the
52+
log file content.
53+
#>
54+
55+
function Start-ScriptLogger
56+
{
57+
[CmdletBinding()]
58+
param
59+
(
60+
[Parameter(Position=0,
61+
Mandatory=$false)]
62+
[ValidateScript({(Test-Path -Path (Split-Path -Path $_ -Parent))})]
63+
[String] $Path = (Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'PowerShell.log'),
64+
65+
[Parameter(Position=1,
66+
Mandatory=$false)]
67+
[ValidateScript({$_ -f (Get-Date), $Env:ComputerName, $Env:Username, 'Verbose', 'Message'})]
68+
[String] $Format = '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}',
69+
70+
[Parameter(Position=2,
71+
Mandatory=$false)]
72+
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
73+
[String] $Level = 'Verbose',
74+
75+
[Parameter(Position=3,
76+
Mandatory=$false)]
77+
[Switch] $NoLogFile,
78+
79+
[Parameter(Position=4,
80+
Mandatory=$false)]
81+
[Switch] $NoEventLog,
82+
83+
[Parameter(Position=5,
84+
Mandatory=$false)]
85+
[Switch] $NoConsoleOutput
86+
)
87+
88+
# Create an empty log file, if it does not exist
89+
if (-not (Test-Path -Path $Path))
90+
{
91+
New-Item -Path $Path -ItemType File | Out-Null
92+
}
93+
94+
# Only work with absolute path, makes error handling easier
95+
$Path = (Resolve-Path -Path $Path).Path
96+
97+
Write-Verbose "Enable script logger with log file '$Path'"
98+
99+
# Define global variable for the logging
100+
$Global:ScriptLogger = New-Object -TypeName PSObject -Property @{
101+
Enabled = $true
102+
Path = $Path
103+
Format = $Format
104+
Level = $Level
105+
LogFile = -not $NoLogFile.IsPresent
106+
EventLog = -not $NoEventLog.IsPresent
107+
ConsoleOutput = -not $NoConsoleOutput.IsPresent
108+
}
109+
$Global:ScriptLogger.PSTypeNames.Insert(0, 'ScriptLogger.Configuration')
110+
}

Functions/Stop-ScriptLogger.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<#
2+
.SYNOPSIS
3+
Stop the script logger inside the current PowerShell session.
4+
5+
.DESCRIPTION
6+
Stop the script logger inside the current PowerShell session and clear all
7+
log configurations.
8+
9+
.EXAMPLE
10+
C:\> Stop-ScriptLogger
11+
Stop the current logger.
12+
#>
13+
14+
function Stop-ScriptLogger
15+
{
16+
[CmdletBinding()]
17+
param
18+
(
19+
)
20+
21+
if ($Global:ScriptLogger -ne $null)
22+
{
23+
Remove-Variable -Scope Global -Name ScriptLogger -Force
24+
}
25+
}

Functions/Write-ErrorLog.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<#
2+
.SYNOPSIS
3+
Log an error message.
4+
5+
.DESCRIPTION
6+
Log an error message to the log file, the event log and show it on the
7+
current console.
8+
9+
.PARAMETER Message
10+
The error message
11+
12+
.EXAMPLE
13+
C:\> Write-ErrorLog -Message 'My Error Message'
14+
Log the error message.
15+
#>
16+
17+
function Write-ErrorLog
18+
{
19+
[CmdletBinding()]
20+
param
21+
(
22+
[Parameter(Mandatory=$true)]
23+
[String] $Message
24+
)
25+
26+
Write-Log -Message $Message -Level 'Error'
27+
}

Functions/Write-InformationLog.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<#
2+
.SYNOPSIS
3+
Log an information message.
4+
5+
.DESCRIPTION
6+
Log an information message to the log file, the event log and show it on
7+
the current console. If the global log level is set to 'warning', no
8+
information message will be logged.
9+
10+
.PARAMETER Message
11+
The information message
12+
13+
.EXAMPLE
14+
C:\> Write-InformationLog -Message 'My Information Message'
15+
Log the information message.
16+
#>
17+
18+
function Write-InformationLog
19+
{
20+
[CmdletBinding()]
21+
param
22+
(
23+
[Parameter(Mandatory=$true)]
24+
[String] $Message
25+
)
26+
27+
Write-Log -Message $Message -Level 'Information'
28+
}

0 commit comments

Comments
 (0)