-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPsLogger.psm1
More file actions
59 lines (51 loc) · 1.76 KB
/
Copy pathPsLogger.psm1
File metadata and controls
59 lines (51 loc) · 1.76 KB
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
<#
.SYNOPSIS
Structured logging PowerShell module with JSONL formatted logfiles for OpenTelemetry.
#>
class Logger {
[datetime]$Timestamp = (Get-Date)
[ValidateRange(1, 24)]
[int]$Level = 9
[string]$Message = $null
[hashtable]$Attributes
hidden static [string] GetSeverity([int]$level) {
switch ($level) {
{$_ -in 1..4} { return 'TRACE' }
{$_ -in 5..8} { return 'DEBUG' }
{$_ -in 13..16} { return 'WARN' }
{$_ -in 17..20} { return 'ERROR' }
{$_ -in 21..24} { return 'FATAL' }
default { return 'INFO' }
}
return 'INFO'
}
[string]FormatLog() {
return "[$(($this.Timestamp).ToString('o'))] [$(($this.Level).ToString())] $($this.Message)"
}
[string]FormatLogRecord() {
$logEntry = @{
timestamp = $this.Timestamp.ToUniversalTime().ToString('o')
severity_number = $this.Level
severity_text = [Logger]::GetSeverity($this.Level)
body = $this.Message
resource = $Script:ResourceAttributes
}
if ($this.Attributes) {
$logEntry.attributes = $this.Attributes
}
return ($logEntry | ConvertTo-Json -Depth 10 -Compress)
}
}
$Script:ResourceAttributes = @{
'service.name' = $env:PSLOGGER_SERVICENAME ?? 'PsLogger'
'service.version' = $env:PSLOGGER_SERVICEVERSION ?? "$($MyInvocation.MyCommand.ScriptBlock.Module.Version)"
'host.name' = $env:PSLOGGER_HOSTNAME ?? [System.Environment]::MachineName
'deployment.environment' = $env:PSLOGGER_ENVIRONMENT ?? 'development'
}
$Script:SpanEndpoint = $env:PSLOGGER_SPAN_ENDPOINT ?? 'http://localhost:4318/v1/traces'
try {
Get-ChildItem "$(Split-Path -Parent $MyInvocation.MyCommand.Path)/Public/*.ps1" | ForEach-Object {. $_}
}
catch {
Write-Error -Message "Importing module PsLogger failed: $($_.Exception.ToString())"
}