Created
January 8, 2019 15:46
-
-
Save maishsk/922835b06d848c4afe7be69762e8ee0b to your computer and use it in GitHub Desktop.
server with logging to file
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
const express = require('express') | |
const app = express() | |
const port = 3000 | |
let winston = require('winston'); | |
let logger = winston.createLogger({ | |
level: 'info', | |
format: winston.format.combine( | |
winston.format.timestamp(), | |
winston.format.printf(info => { | |
return `${info.timestamp} ${info.level}: ${info.message}`; | |
}) | |
), | |
transports: [ | |
new winston.transports.Console(), | |
new winston.transports.File({filename: 'app.log'}) | |
] | |
}); | |
app.get('/hello', (request, response) => { | |
response.send('Hello from Express!'), | |
logger.log('info', 'A request was received'); | |
}) | |
app.listen(port, (err) => { | |
if (err) { | |
return console.log('something bad happened', err) | |
} | |
console.log(`server is listening on ${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment