Created
May 25, 2017 21:07
-
-
Save adam-hert/572f3819915e861d859381a88d584b68 to your computer and use it in GitHub Desktop.
Adding info to exceptions in Winston
This file contains 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
var http = require('http'); | |
var winston = require('winston'); | |
require('winston-papertrail').Papertrail; | |
var winstonPapertrail = new winston.transports.Papertrail({ | |
host: 'logs.papertrailapp.com', //change to match your destination | |
port: xxxxx, //change to match your port | |
program: 'my_node_app' | |
}) | |
winstonPapertrail.on('error', function(err) { | |
// Handle, report, or silently ignore connection errors and failures | |
}); | |
var logger = new winston.Logger({ | |
transports: [winstonPapertrail], | |
}); | |
//function to generate a GUID | |
function Guid(){ | |
return Math.random().toString(36).replace(/[^a-z]+/g, '') | |
} | |
// Configure HTTP server to respond with Hello World to all requests. | |
var server = http.createServer(function (request, response) { | |
try { | |
//create an exception | |
const m = 1; | |
const n = m + z; | |
} catch (err) { | |
// create GUID and add it to the error message | |
id = ' (' + Guid() + ')' | |
err.message = err.message + id | |
//send the error to papertrail | |
logger.error(err) | |
//send extra info to papertrail with same GUID | |
logger.info(id + ': ' + 'var = '+ 'x') | |
} | |
//basic hello world stuff | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.end("Hello World\n"); | |
}); | |
// Listen on port 80 | |
server.listen(80); | |
// Put a friendly message on the terminal | |
console.log("Server running at localhost:80/"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment