Skip to content
Snippets Groups Projects
Commit fcf823ad authored by Markus Grigull's avatar Markus Grigull
Browse files

Add unified logger output and file logging.

parent f75baf3b
Branches
No related tags found
1 merge request!5Resolve "Add log level config option"
......@@ -2,6 +2,7 @@
node_modules/
npm-debug.log
public/*
log*.txt
# OS X
.DS_Store
......@@ -26,6 +26,8 @@ module.exports = {
databaseURL: 'mongodb://localhost:27017/',
port: 4000,
secret: 'longsecretislong',
logLevel: 'info',
logFile: 'log.txt',
admin: {
username: 'admin',
password: 'admin'
......@@ -36,6 +38,8 @@ module.exports = {
databaseURL: 'mongodb://database:27017/',
port: 4000,
secret: 'longsecretislong',
logLevel: 'warn',
logFile: 'villasweb-backend_log.txt',
admin: {
username: 'admin',
password: 'admin'
......
......@@ -8,9 +8,10 @@
"body-parser": "^1.15.2",
"cors": "^2.7.1",
"express": "^4.14.0",
"express-winston": "^2.4.0",
"formidable": "^1.0.17",
"jsonwebtoken": "^7.0.1",
"mongoose": "^4.5.1",
"morgan": "^1.7.0"
"winston": "^2.3.1"
}
}
......@@ -23,10 +23,12 @@
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var cors = require('cors');
var winston = require('winston');
var expressWinston = require('express-winston');
// local include
var logger = require('./utils/logger');
var users = require('./routes/users');
var projects = require('./routes/projects');
var visualizations = require('./routes/visualizations');
......@@ -45,10 +47,20 @@ var app = express();
// load configuration
var config = require('./config')[app.get('env')];
// configure logger
if (config.logLevel) {
logger.transports.console.level = config.logLevel;
}
if (config.logFile) {
logger.transports.file.filename = config.logFile;
logger.transports.file.silent = false;
}
// configure app
app.use(expressWinston.logger({ winstonInstance: logger }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan('dev'));
app.use(cors());
// connect to database
......@@ -75,7 +87,7 @@ app.use(function(req, res, next) {
});
// development error handler
console.log("Environment: " + app.get('env'));
logger.info("Environment: " + app.get('env'));
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
......@@ -92,7 +104,7 @@ app.use(function(err, req, res, next) {
// start the app
app.listen(config.port, function() {
console.log('Express server listening on port ' + config.port);
logger.info('Express server listening on port ' + config.port);
});
// add admin account
......@@ -100,7 +112,7 @@ if (config.admin) {
// check if admin account exists
User.findOne({ username: config.admin.username }, function(err, user) {
if (err) {
console.log(err);
logger.error(err);
return;
}
......@@ -109,11 +121,11 @@ if (config.admin) {
var newUser = User({ username: config.admin.username, password: config.admin.password, role: 'admin' });
newUser.save(function(err) {
if (err) {
console.log(err);
logger.error(err);
return;
}
console.log('Created default admin user from config file');
logger.warn('Created default admin user from config file');
});
}
});
......
/**
* File: logger.js
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
* Date: 24.05.2017
*
* This file is part of VILLASweb-backend.
*
* VILLASweb-backend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VILLASweb-backend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VILLASweb-backend. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
// include modules
var winston = require('winston');
const logger = new winston.Logger({
transports: [
new winston.transports.Console({
prettyPrint: false,
colorize: true,
timestamp: function() {
return new Intl.DateTimeFormat('de-DE', {
hour12: false,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).format(Date.now());
}
}),
new winston.transports.File({
filename: 'log.txt',
tailable: true,
maxsize: 32768,
maxFiles: 32,
json: false,
silent: true,
timestamp: function() {
return new Intl.DateTimeFormat('de-DE', {
hour12: false,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).format(Date.now());
}
})
]
});
module.exports = logger;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment