Skip to content

Instantly share code, notes, and snippets.

@jfhbrook
Created April 22, 2024 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfhbrook/839d7a55c57bee97629883178b1a259a to your computer and use it in GitHub Desktop.
Save jfhbrook/839d7a55c57bee97629883178b1a259a to your computer and use it in GitHub Desktop.
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