-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWrite-Log.ps1
203 lines (159 loc) · 7.89 KB
/
Write-Log.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function Write-Log {
<#
.SYNOPSIS
Single function to enable logging to file
.DESCRIPTION
The Log file can be output to any directory. A single log entry looks like this:
2018-01-30 14:40:35 INFO: 'My log text'
Log entries can be Info, Warning, Error or Debug
The function takes pipeline input and you can pipe exceptions straight to the function for automatic error logging.
It's not part of this function, but it can be useful to use the $PSDefaultParameterValues built-in Variable can be used to conveniently set the path and/or JSONformat switch at the top of the script:
$PSDefaultParameterValues = @{"Write-Log:Path" = 'C:\YourPathHere.log'}
$PSDefaultParameterValues = @{"Write-Log:JSONformat" = $true}
.PARAMETER Message
This is the body of the log line and should contain the information you wish to log.
.PARAMETER Level
One of four logging levels: INFO, WARNING, ERROR or DEBUG. This is an optional parameter and defaults to INFO
.PARAMETER Path
The path where you want the log file to be created. This is an optional parameter and defaults to "$env:temp\PowershellScript.log"
.PARAMETER StartNew
This will blank any current log in the path, it should be used at the start of your code if you don't want to append to an existing log.
.PARAMETER Exception
Used to pass a powershell exception to the logging function for automatic logging, this will log the excption message as an error.
.PARAMETER JSONFormat
Used to change the logging format from human readable to machine readable format, this will be a single line like the example format below:
In this format the timestamp will include a much more granular time which will also include timezone information. The format is optimised for Splunk input, but should work for any other platform.
{"TimeStamp":"2018-02-01T12:01:24.8908638+00:00","Level":"Warning","Message":"My message"}
.EXAMPLE
Write-Log -StartNew
Starts a new logfile in the default location
.EXAMPLE
Write-Log -StartNew -Path c:\logs\new.log
Starts a new logfile in the specified location
.EXAMPLE
Write-Log 'This is some information'
Appends a new information line to the log.
.EXAMPLE
Write-Log -level Warning 'This is a warning'
Appends a new warning line to the log.
.EXAMPLE
Write-Log -level Error 'This is an Error'
Appends a new Error line to the log.
.EXAMPLE
Write-Log -Exception $error[0]
Appends a new Error line to the log with the message being the contents of the exception message.
.EXAMPLE
$error[0] | Write-Log
Appends a new Error line to the log with the message being the contents of the exception message.
.EXAMPLE
'My log message' | Write-Log
Appends a new Info line to the log with the message being the contents of the string.
.EXAMPLE
Write-Log 'My log message' -JSONFormat
Appends a new Info line to the log with the message. The line will be in JSONFormat.
#>
[CmdletBinding(DefaultParametersetName = "LOG")]
Param (
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = 'LOG',
Position = 0)]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = 'LOG',
Position = 1 )]
[ValidateSet('Error', 'Warning', 'Info', 'Debug')]
[string]$Level = "Info",
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
Position = 2)]
[Alias('PSPath')]
[string]$Path,
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true)]
[switch]$JSONFormat,
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = 'STARTNEW')]
[switch]$StartNew,
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = 'EXCEPTION')]
[System.Management.Automation.ErrorRecord]$Exception
)
BEGIN {
Set-StrictMode -version Latest #Enforces most strict best practice.
}
PROCESS {
#Switch on parameter set
switch ($PSCmdlet.ParameterSetName) {
LOG {
#Get human readable date
$formattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
switch ( $Level ) {
'Info' { $levelText = "INFO: "; break }
'Error' { $levelText = "ERROR: "; break }
'Warning' { $levelText = "WARNING:"; break }
'Debug' { $levelText = "DEBUG: "; break }
}
#Build an object so we can later convert it
$logObject = @{
#TimeStamp = Get-Date -Format o #Get machine readable date
Level = $levelText
Message = $Message
}
if ($JSONFormat) {
$logobject = [PSCustomObject][ordered]@{
TimeStamp = Get-Date -Format o
Level = $levelText
Message = $Message
}
#Convert to a single line of JSON and add it to the file
$logMessage = $logObject | ConvertTo-Json -Compress
$logMessage | Add-Content -Path $Path
}
else {
$logobject = [PSCustomObject][ordered]@{
TimeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Level = $levelText
Message = $Message
}
$logMessage = "$formattedDate`t$levelText`t$Message" #Build human readable line
$logObject | Export-Csv -Path $Path -Delimiter "`t" -NoTypeInformation -Append
}
Write-Verbose $logMessage #Only verbose line in the function
} #LOG
EXCEPTION {
#Splat parameters
$writeLogParams = @{
Level = 'Error'
Message = $Exception.Exception.Message
Path = $Path
JSONFormat = $JSONFormat
}
Write-Log @writeLogParams #Call itself to keep code clean
break
} #EXCEPTION
STARTNEW {
if (Test-Path $Path) {
Remove-Item $Path -Force
}
#Splat parameters
$writeLogParams = @{
Level = 'Info'
Message = 'Starting Logfile'
Path = $Path
JSONFormat = $JSONFormat
}
Write-Log @writeLogParams
break
} #STARTNEW
} #switch Parameter Set
}
END {
}
} #function Write-Log