Created
July 24, 2019 12:30
-
-
Save megastef/ce0ae69628ecedc007c397e15cc5c822 to your computer and use it in GitHub Desktop.
Webserver for winston test
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 Logsene = require('winston-logsene') | |
var winston = require('winston') | |
const {createLogger, format} = require('winston') | |
// example for custom rewriter, e.g. add myServerIp field to all logs | |
var myServerIp = '10.0.0.12' | |
var logger = createLogger({ | |
levels: winston.config.npm.levels, | |
// format: winston.format.json(), | |
transports: [new Logsene({ | |
// set log level, defaut is 'info' | |
level: 'debug', | |
// optional set a format function | |
format: format.splat(), | |
token: process.env.LOGS_TOKEN, | |
rewriter: function (level, msg, meta) { | |
meta.ip = myServerIp | |
return meta | |
} | |
})] | |
}) | |
logger.on('flush', function () { console.log("logs flushed", arguments)}) | |
logger.on('error', console.error) | |
//logger.on('logged', console.error) | |
//Lets require/import the HTTP module | |
var http = require('http'); | |
//Lets define a port we want to listen to | |
const PORT=7080; | |
//We need a function which handles requests and send response | |
function handleRequest(request, response){ | |
logger.info ('request for: ' + request.url, request.headers) | |
response.end('It Works!! Path Hit: ' + request.url); | |
// log memory usage to Logsene | |
logger.info('memory usage' + process.memoryUsage().rss, process.memoryUsage()) | |
} | |
//Create a server | |
var server = http.createServer(handleRequest); | |
//Lets start our server | |
server.listen(PORT, function(){ | |
//Callback triggered when server is successfully listening. Hurray! | |
console.log("Server listening on: http://localhost:%s", PORT); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment