Created
April 22, 2024 02:24
-
-
Save jfhbrook/839d7a55c57bee97629883178b1a259a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Transform } from 'stream'; | |
import { spawn } from 'child_process'; | |
import build from 'pino-abstract-transport'; | |
export interface Options { | |
logLevel: string; | |
} | |
export default function transport({ logLevel }: Options) { | |
const logScript = `$Input | ForEach-Object { | |
$data = ($_ | ConvertFrom-Json) | |
$message = @{ | |
Data = $data; | |
LogLevel = '${logLevel}'; | |
Message = $data.msg | |
} | |
$dataTime = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0 | |
$dataTime = [DateTime]::SpecifyKind($dataTime, [System.DateTimeKind]::Utc) | |
$dataTime = $dataTime.AddMilliseconds($data.time) | |
$message.DataTime = $dataTime.ToString('u') | |
$msg = ($message | ConvertTo-Json -Compress) | |
if ($data.level -lt 30) { | |
$message.Severity = "Normal" | |
Write-Verbose $msg | |
} elseif ($data.level -lt 40) { | |
$message.Severity = "Warning" | |
Write-Warning $msg | |
} else { | |
$message.Severity = "Error" | |
Write-Error $msg | |
} | |
} | |
`; | |
const proc = spawn('pwsh', [ | |
'-NoProfile', | |
'-NonInteractive', | |
'-c', | |
logScript, | |
]); | |
proc.stdout.pipe(process.stdout); | |
proc.stderr.pipe(process.stderr); | |
return build(async (source) => { | |
source | |
.pipe( | |
new Transform({ | |
objectMode: true, | |
autoDestroy: true, | |
transform(chunk, _encoding, callback) { | |
callback(null, JSON.stringify(chunk) + '\n'); | |
}, | |
}), | |
) | |
.pipe(proc.stdin); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment