Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logging for basicExample #1596

Merged
merged 7 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions extras/basic_example/basicServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const express = require('express');
const bodyParser = require('body-parser');
const errorhandler = require('errorhandler');
const morgan = require('morgan');
const logger = require('log4js');
// eslint-disable-next-line import/no-unresolved
const N = require('./nuve');
const fs = require('fs');
Expand All @@ -17,6 +18,13 @@ config.erizoController.ssl_key = config.erizoController.ssl_key || '../../cert/k
config.erizoController.ssl_cert = config.erizoController.ssl_cert || '../../cert/cert.pem';
config.basicExample.nuveUrl = config.basicExample.nuveUrl || 'http://localhost:3000';

config.basicExample.logger = config.basicExample.logger || {};
const logFile = config.basicExample.logger.configFile || './log4js_configuration.json';

logger.configure(logFile);
const log = logger.getLogger('BasicExample');


const options = {
key: fs.readFileSync(config.erizoController.ssl_key).toString(),
cert: fs.readFileSync(config.erizoController.ssl_cert).toString(),
Expand All @@ -36,7 +44,18 @@ app.use(errorhandler({
dumpExceptions: true,
showStack: true,
}));
app.use(morgan('dev'));
app.use(morgan('dev', {
stream: {
write: (str) => { log.debug(str.trim()); },
},
skip: (req, res) => (res.statusCode >= 400),
}));
app.use(morgan('dev', {
stream: {
write: (str) => { log.error(str.trim()); },
},
skip: (req, res) => (res.statusCode < 400),
}));
app.use(express.static(`${__dirname}/public`));

app.use(bodyParser.json());
Expand Down Expand Up @@ -105,7 +124,7 @@ const deleteRoomsIfEmpty = (theRooms, callback) => {
deleteRoomsIfEmpty(theRooms, callback);
}
}, (error, status) => {
console.log('Error getting user list for room ', theRoomId, 'reason: ', error);
log.error('Error getting user list for room ', theRoomId, 'reason: ', error);
switch (status) {
case 404:
deleteRoomsIfEmpty(theRooms, callback);
Expand All @@ -122,7 +141,7 @@ const deleteRoomsIfEmpty = (theRooms, callback) => {
};

const cleanExampleRooms = (callback) => {
console.log('Cleaning basic example rooms');
log.debug('Cleaning basic example rooms');
N.API.getRooms((roomlist) => {
const rooms = JSON.parse(roomlist);
const roomsToCheck = [];
Expand All @@ -137,7 +156,7 @@ const cleanExampleRooms = (callback) => {
callback('done');
});
}, (err) => {
console.log('Error cleaning example rooms', err);
log.debug('Error cleaning example rooms', err);
setTimeout(cleanExampleRooms.bind(this, callback), 3000);
});
};
Expand All @@ -157,7 +176,7 @@ app.get('/getUsers/:room', (req, res) => {


app.post('/createToken/', (req, res) => {
console.log('Creating token. Request body: ', req.body);
log.debug('Creating token. Request body: ', req.body);

const username = req.body.username;
const role = req.body.role;
Expand All @@ -174,10 +193,10 @@ app.post('/createToken/', (req, res) => {

const createToken = (tokenRoomId) => {
N.API.createToken(tokenRoomId, username, role, (token) => {
console.log('Token created', token);
log.debug('Token created', token);
res.send(token);
}, (error) => {
console.log('Error creating token', error);
log.error('Error creating token', error);
res.status(401).send('No Erizo Controller found');
});
};
Expand Down Expand Up @@ -215,7 +234,7 @@ cleanExampleRooms(() => {

app.listen(port);
const server = https.createServer(options, app);
console.log('BasicExample started');
log.info(`BasicExample started and listenting on port ${port}`);
server.listen(tlsPort);
});
});
16 changes: 16 additions & 0 deletions extras/basic_example/log4js_configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"appenders": [
{
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%d - %p: %c - %m",
"replaceConsole": true
}
}
],
"levels": {
"BasicExample":"INFO"
}
}

63 changes: 63 additions & 0 deletions extras/basic_example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion extras/basic_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"body-parser": "~1.19.0",
"errorhandler": "~1.5.1",
"express": "~4.17.1",
"morgan": "~1.9.1"
"morgan": "~1.9.1",
"log4js": "~1.0.1"
},
"contributors": [
{
Expand Down
2 changes: 2 additions & 0 deletions scripts/licode_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ config.basicExample = {};
config.basicExample.port = 3001; // default value: 3001
config.basicExample.tlsPort = 3004; // default value: 3004
config.basicExample.nuveUrl = 'http://localhost:3000/';
config.basicExample.logger = {};
config.basicExample.logger.configFile = './log4js_configuration.json'; // default value: "./log4js_configuration.json"

/***** END *****/
// Following lines are always needed.
Expand Down