What is best way to log my express js webserver? The inbuilt express.logger() just displays logs on screen. Can I also log them into a file in /log folder? Also the current
you should try cluster http://learnboost.github.com/cluster/ for Node. Use express to build the app, while the cluster take over the rest tasks including logging.
winston is kinda silly, multi-transport logging == tee(1), or just tail the file and transfer the data off, easy as pie
Look at the connect middleware that express extends. The express.logger() is the same as the connect.logger():
http://expressjs.com/api.html#middleware
http://www.senchalabs.org/connect/logger.html
The logger has a stream option that can be set where you want the output to go. By default it sends it to stdout. Also you can specify the log format you want to use.
You should try winston
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
To send the express or connect logs to a file use Node's writeStream. For example to send the express logs to ./myLogFile.log :
open the stream to your file in append mode with :
var logFile = fs.createWriteStream('./myLogFile.log', {flags: 'a'}); //use {flags: 'w'} to open in write mode
then, in your express config use :
app.use(express.logger({stream: logFile}));
should also work for connect.logger.
For HTTP request logging: https://github.com/expressjs/morgan#write-logs-to-a-file
var express = require('express')
var fs = require('fs')
var morgan = require('morgan')
var app = express()
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))
app.get('/', function (req, res) {
res.send('hello, world!')
})