开发者

Logging in express js to a output file?

What is best way to log my express j开发者_JAVA百科s 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


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.


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' })
  ]
});


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


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}));


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


You could try cluster http://learnboost.github.io/cluster/ for Node. Use express to build the app, while the cluster takes over the rest of the 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


For logging or debugging, Install winston package

npm i winston

Call package in this file

const { createLogger, transports, format } = require('winston');

const logger = createLogger({
  format: format.combine(
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
    format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
  ),
  transports: [
    new transports.File({
      filename: './logs/logs.log',
      json: false,
      maxsize: 5242880,
      maxFiles: 5,
    }),
    new transports.Console(),
  ]
});

Write this code where you want to debug, you can call different log like info, warn, error.

logger.info('some info log message');
logger.warn('some warn log message');
logger.error('some error log message');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜