What is MORGAN in Node.js ? - GeeksforGeeks
Add Server Logs to your Node.js App with Morgan and Winston | heynode.com
Express morgan middleware (expressjs.com)
Node.js - logging / Use morgan and winston - Stack Overflow
Advanced logging with NodeJs | Ugo Lattanzi's tech world (tostring.it)
Morgan is an HTTP request level Middleware. It is a great tool that logs the requests along with some other information depending upon its configuration and the preset used. It proves to be very helpful while debugging and also if you want to create Log files.
Now that we have our logger installed, we'll need a way to use it. In our App.js file we’re going to bring in Morgan for capturing HTTP requests. We'll use Morgan’s standard "combined" format and pass the stream to our Winston logger.
const express = require('express');
const app = express();
const morgan = require('morgan');
const logger = require('./config/winston');
const port = 8080
app.use(morgan("combined", { stream: logger.stream.write }));
app.get('/', function(req, res) {
throw new Error('error thrown navigating to');
});
app.use(function(err, req, res, next) {
logger.error(`${req.method} - ${err.message} - ${req.originalUrl} - ${req.ip}`);
next(err)
})
app.listen(port, console.log(`Listening on port ${port}!`));
This article does an excellent job for what you want to do.
http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
For your specific code you probably need something like this:
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: './logs/all-logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function(message, encoding){
logger.info(message);
}
};
app.use(require("morgan")("combined", { "stream": logger.stream }));
This will set up Winston to write a log to the console as well as a file. Then you can use the last expression to pass output from the morgan middleware into winston.
write logs to a file
single file
Sample app that will log all requests in the Apache combined format to the file access.log
.
var express = require('express')
var fs = require('fs')
var morgan = require('morgan')
var path = require('path')
var app = express()
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
// setup the logger
app.use(morgan('combined', { stream: accessLogStream }))
app.get('/', function (req, res) {
res.send('hello, world!')
})
log file rotation
Sample app that will log all requests in the Apache combined format to one log file per day in the log/
directory using the rotating-file-stream module.
var express = require('express')
var morgan = require('morgan')
var path = require('path')
var rfs = require('rotating-file-stream') // version 2.x
var app = express()
// create a rotating write stream
var accessLogStream = rfs.createStream('access.log', {
interval: '1d', // rotate daily
path: path.join(__dirname, 'log')
})
// setup the logger
app.use(morgan('combined', { stream: accessLogStream }))
app.get('/', function (req, res) {
res.send('hello, world!')
})
Không có nhận xét nào:
Đăng nhận xét