configured logging and added to /routes

This commit is contained in:
bill bittner 2017-06-19 13:10:06 -07:00
parent e53d875f8c
commit a305afe5de
13 changed files with 92 additions and 26 deletions

View file

@ -8,5 +8,9 @@
"Database": {
"MySqlConnectionUri": "none",
"PublishUploadPath": "none"
},
"Logging": {
"LogLevel": "silly",
"LogDirectory": "C:\\lbry\\speech\\logs\\"
}
}

View file

@ -8,5 +8,8 @@
"Database": {
"MySqlConnectionUri": "none",
"PublishUploadPath": "C:\\lbry\\speech\\hosted_content"
},
"Logging": {
"LogLevel": "silly"
}
}

View file

@ -8,5 +8,8 @@
"Database": {
"MySqlConnectionUri": "none",
"PublishUploadPath": "/home/lbry/Downloads/"
},
"Logging": {
"LogLevel": "verbose"
}
}

View file

@ -8,5 +8,8 @@
"Database": {
"MySqlConnectionUri": "none",
"PublishUploadPath": "/home/ubuntu/Downloads/"
},
"Logging": {
"LogLevel": "info"
}
}

View file

@ -41,7 +41,7 @@ module.exports = {
axios
.post('http://localhost:5279/lbryapi', {
method: 'get',
params: { uri, timeout: 20 },
params: { uri, timeout: 60 },
})
.then(({ data }) => {
console.log(">> 'get' success");

View file

@ -0,0 +1,35 @@
const fs = require('fs');
const tsFormat = () => (new Date()).toLocaleTimeString();
module.exports = (winston, logLevel, logDir) => {
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
winston.level = logLevel;
winston.add(
winston.transports.File, {
filename : `${logDir}speechLogs.log`,
level : logLevel,
json : true,
timestamp : tsFormat,
colorize : true,
prettyPrint: true,
}
);
winston.handleExceptions(new winston.transports.File({
filename : `${logDir}uncaughtExceptions.log`,
humanReadableUnhandledException: true,
}));
// winston.exitOnError = false;
winston.error('Level 0');
winston.warn('Level 1');
winston.info('Level 2');
winston.verbose('Level 3');
winston.debug('Level 4');
winston.silly('Level 5');
};

View file

@ -36,7 +36,8 @@
"sequelize": "^4.1.0",
"socket.io": "^2.0.1",
"socketio-file-upload": "^0.6.0",
"universal-analytics": "^0.4.13"
"universal-analytics": "^0.4.13",
"winston": "^2.3.1"
},
"devDependencies": {
"eslint": "3.19.0",

View file

@ -1,29 +1,31 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const lbryApi = require('../helpers/libraries/lbryApi.js');
const logger = require('winston');
module.exports = app => {
// route to run a claim_list request on the daemon
app.get('/api/claim_list/:claim', ({ params }, res) => {
app.get('/api/claim_list/:claim', ({ originalUrl, params }, res) => {
lbryApi
.getClaimsList(params.claim)
.then(claimsList => {
console.log('/api/claim_list/:claim success.');
logger.info(`${originalUrl} returned successfully.`);
res.status(200).json(claimsList);
})
.catch(error => {
console.log('/api/claim_list/:name error:', error);
logger.error(`${originalUrl} returned an error.`, error);
errorHandlers.handleRequestError(error, res);
});
});
// route to run a resolve request on the daemon
app.get('/api/resolve/:uri', ({ params }, res) => {
app.get('/api/resolve/:uri', ({ originalUrl, params }, res) => {
lbryApi
.resolveUri(params.uri)
.then(resolvedUri => {
console.log('/api/resolve/:claim success.');
logger.info(`${originalUrl} returned successfully.`);
res.status(200).json(resolvedUri);
})
.catch(error => {
logger.error(`${originalUrl} returned an error.`, error);
errorHandlers.handleRequestError(error, res);
});
});

View file

@ -1,10 +1,14 @@
const logger = require('winston');
module.exports = app => {
// route for the home page
app.get('/', (req, res) => {
logger.silly(`Get request on /`);
res.status(200).render('index');
});
// a catch-all route if someone visits a page that does not exist
app.use('*', (req, res) => {
logger.error(`Get request on ${req.originalUrl} which was 404`);
res.status(404).render('fourOhFour');
});
};

View file

@ -1,5 +1,6 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const serveController = require('../controllers/serveController.js');
const logger = require('winston');
function serveFile ({ file_type, file_path }, res) {
// set default options
@ -21,7 +22,7 @@ function serveFile ({ file_type, file_path }, res) {
case 'video/mp4':
break;
default:
console.log('sending unknown file type as .jpeg');
logger.warn('sending unknown file type as .jpeg');
options['headers']['Content-Type'] = 'image/jpeg';
break;
}
@ -31,37 +32,37 @@ function serveFile ({ file_type, file_path }, res) {
module.exports = (app, ua, googleAnalyticsId) => {
// route to fetch one free public claim
app.get('/:name/:claim_id', ({ params }, res) => {
app.get('/:name/:claim_id', ({ originalUrl, params }, res) => {
logger.debug(`Get request on ${originalUrl}`);
const routeString = `${params.name}/${params.claim_id}`;
// google analytics
ua(googleAnalyticsId, { https: true }).event('Serve Route', '/name/claimId', routeString).send();
// begin image-serve processes
console.log(`>> GET request on /${routeString}`);
serveController
.getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => {
console.log('/:name/:claim_id success.', fileInfo.file_name);
logger.info(`${originalUrl} returned successfully.`);
serveFile(fileInfo, res);
})
.catch(error => {
console.log('/:name/:claim_id error:', error);
logger.error(`${originalUrl} returned an error.`, error);
errorHandlers.handleRequestError(error, res);
});
});
// route to fetch one free public claim
app.get('/:name', ({ params }, res) => {
app.get('/:name', ({ originalUrl, params }, res) => {
logger.debug(`Get request on ${originalUrl}`);
// google analytics
ua(googleAnalyticsId, { https: true }).event('Serve Route', '/name', params.name).send();
// begin image-serve processes
console.log(`>> GET request on /${params.name}`);
serveController
.getClaimByName(params.name)
.then(fileInfo => {
console.log('/:name success.', fileInfo.file_name);
logger.info(`${originalUrl} returned successfully.`);
serveFile(fileInfo, res);
})
.catch(error => {
console.log('/:name error:', error);
logger.error(`${originalUrl} returned an error.`, error);
errorHandlers.handleRequestError(error, res);
});
});

View file

@ -1,21 +1,22 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const showController = require('../controllers/showController.js');
const logger = require('winston');
module.exports = (app, ua, googleAnalyticsId) => {
// route to fetch all free public claims
app.get('/:name/all', ({ params }, res) => {
console.log(`>> GET request on /${params.name}/all`);
app.get('/:name/all', ({ originalUrl, params }, res) => {
logger.debug(`Get request on ${originalUrl}`);
// google analytics
ua(googleAnalyticsId, { https: true }).event('Show Routes', '/name/all', `${params.name}/all`).send();
// fetch all free public claims
showController
.getAllClaims(params.name)
.then(orderedFreePublicClaims => {
console.log('/:name/all success.');
logger.info(`${originalUrl} returned successfully.`);
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
})
.catch(error => {
console.log('/:name/all error:', error);
logger.error(`${originalUrl} returned an error.`, error);
errorHandlers.handleRequestError(error, res);
});
});

View file

@ -1,11 +1,12 @@
const publishController = require('../controllers/publishController.js');
const logger = require('winston');
module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
const http = require('http').Server(app);
const io = require('socket.io')(http);
io.on('connection', socket => {
console.log('a user connected via sockets');
logger.silly('a user connected via sockets');
// create visitor record
const visitor = ua(googleAnalyticsId, { https: true });
// attach upload listeners
@ -13,28 +14,30 @@ module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
uploader.dir = hostedContentPath;
uploader.listen(socket);
uploader.on('start', ({ file }) => {
logger.info('Client started an upload', file);
// server side test to make sure file is not a bad file type
if (/\.exe$/.test(file.name)) {
uploader.abort(file.id, socket);
}
});
uploader.on('error', ({ error }) => {
console.log('an error occured while uploading', error);
logger.error('an error occured while uploading', error);
socket.emit('publish-status', error);
});
uploader.on('saved', ({ file }) => {
console.log('uploaded ', file.name);
if (file.success) {
logger.info(`Client successfully uploaded ${file.name}`);
socket.emit('publish-status', 'file upload successfully completed');
publishController.publish(file.meta.name, file.name, file.pathName, file.meta.type, file.meta.license, file.meta.nsfw, socket, visitor);
} else {
logger.error(`An error occurred in uploading the client's file`);
socket.emit('publish-failure', 'file uploaded, but with errors');
// to-do: remove the file
// to-do: remove the file if not done automatically
}
});
// handle disconnect
socket.on('disconnect', () => {
console.log('user disconnected');
logger.silly('a user disconnected via sockets');
});
});

View file

@ -6,10 +6,16 @@ const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const config = require('config');
const ua = require('universal-analytics');
const winston = require('winston');
const googleAnalyticsId = config.get('AnalyticsConfig.googleId');
const hostedContentPath = config.get('Database.PublishUploadPath');
// configure logging
const logLevel = config.get('Logging.LogLevel');
const logDir = config.get('Logging.LogDirectory');
require('./helpers/logging/loggerSetup.js')(winston, logLevel, logDir);
// set port
const PORT = 3000;
// initialize express app
@ -63,6 +69,6 @@ const http = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath
// start server
db.sequelize.sync({}).then(() => {
http.listen(PORT, () => {
console.log(`Listening on PORT ${PORT}`);
winston.info(`Server is listening on PORT ${PORT}`);
});
});