Logging in express js to a output file?

node.jsExpress

node.js Problem Overview


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 logger automatically logs the request and responses. I need to log some application data into the log files. Can this be done using express.logger?

Regards, Lalith

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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.

Solution 3 - node.js

You should try winston

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)(),
    new (winston.transports.File)({ filename: 'somefile.log' })
  ]
});

Solution 4 - node.js

winston is kinda silly, multi-transport logging == tee(1), or just tail the file and transfer the data off, easy as pie

Solution 5 - node.js

Use log4js:

var log4js = require('log4js');
log4js.configure({
    appenders: [{type: 'console'},
                {type: 'file', filename: 'express.log', category: 'dev'}]
});

var logger = log4js.getLogger('dev');
logger.setLevel('DEBUG');

app.use(log4js.connectLogger(logger, {level: log4js.levels.DEBUG}));

Solution 6 - node.js

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!')
})

Solution 7 - node.js

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.

  1. app.use(express.logger()); // in your express apps, ex: app.js
  2. cluster.use(cluster.logger('logs')) ; // in your cluster server, ex: server.js

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLalithView Question on Stackoverflow
Solution 1 - node.jsDidView Answer on Stackoverflow
Solution 2 - node.jsChad WilliamsView Answer on Stackoverflow
Solution 3 - node.jsgeneralhenryView Answer on Stackoverflow
Solution 4 - node.jstjholowaychukView Answer on Stackoverflow
Solution 5 - node.jselwarrenView Answer on Stackoverflow
Solution 6 - node.jsneurosnapView Answer on Stackoverflow
Solution 7 - node.jsLen XuView Answer on Stackoverflow