This commit is contained in:
Esther 2020-06-22 22:48:48 +01:00
parent c5638a4552
commit 76190ef7d8
No known key found for this signature in database
GPG Key ID: 162A307C5EBD40EA
2 changed files with 24 additions and 62 deletions

34
app.js
View File

@ -2,8 +2,9 @@ var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var mLogger = require('morgan');
var sassMiddleware = require('node-sass-middleware');
var winston = require('winston');
var logger = require('./config/winston');
var indexRouter = require('./routes/index');
var aboutRouter = require('./routes/about');
@ -17,37 +18,10 @@ app.disable('x-powered-by');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
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 ston.transportswin.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function(message, encoding){
logger.info(message);
}
};
if (process.env.NODE_ENV === 'production') {
app.use(require("morgan")("common", { "stream": logger.stream }));
app.use(mLogger("common", { "stream": logger.stream }));
} else {
app.use(require(morgan)('dev'));
app.use(mLogger('dev'));
}
app.use(express.json());

View File

@ -1,41 +1,29 @@
var appRoot = require('app-root-path');
var winston = require('winston');
// define the custom settings for each transport (file, console)
var options = {
file: {
var logger = new winston.createLogger({
transports: [
new winston.transports.File({
level: 'info',
filename: `${appRoot}/logs/app.log`,
filename: './logs/all-logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false,
},
console: {
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
let logger = winston.createLogger({
transports: [
new (winston.transports.Console)(options.console),
new (winston.transports.File)(options.errorFile),
new (winston.transports.File)(options.file)
colorize: true
})
],
exitOnError: false, // do not exit on handled exceptions
exitOnError: false
});
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write: function(message, encoding) {
// use the 'info' log level so the output will be picked up by both transports (file and console)
write: function(message, encoding){
logger.info(message);
},
}
};
module.exports = logger;
module.exports = logger