Skip to content

Instantly share code, notes, and snippets.

@juliangruber
Last active October 18, 2023 12:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliangruber/7cd90f71b82eb5376ec4a9ea74b9e882 to your computer and use it in GitHub Desktop.
Save juliangruber/7cd90f71b82eb5376ec4a9ea74b9e882 to your computer and use it in GitHub Desktop.
const dgram = require('node:dgram')
const util = require('node:util')
const glossy = require('glossy')
const syslogProducer = new glossy.Produce()
const socket = dgram.createSocket('udp4')
const streams = {
stdout: process.stdout.write.bind(process.stdout),
stderr: process.stderr.write.bind(process.stderr)
}
const sendToPapertrail = (chunk, { severity }) => {
const syslog = syslogProducer.produce({
facility: 'user',
severity,
host : config.host,
appName : config.appName,
pid : process.pid,
time : new Date(),
message : String(chunk),
})
socket.send(syslog, 0, syslog.length, config.port, config.host, err => {
if (err) {
streams.stderr.write(util.inspect(err), 'utf8', () => {})
}
})
}
for (const [streamName, streamWrite] of Object.entries(streams)) {
process[streamName].write = (chunk, encoding, cb) => {
sendToPapertrail(chunk, {
severity: streamName === 'stdout'
? 6 // Info
: 4 // Warning
})
return streamWrite(chunk, encoding, cb)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment