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": { "Database": {
"MySqlConnectionUri": "none", "MySqlConnectionUri": "none",
"PublishUploadPath": "none" "PublishUploadPath": "none"
},
"Logging": {
"LogLevel": "silly",
"LogDirectory": "C:\\lbry\\speech\\logs\\"
} }
} }

View file

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

View file

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

View file

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

View file

@ -41,7 +41,7 @@ module.exports = {
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'get', method: 'get',
params: { uri, timeout: 20 }, params: { uri, timeout: 60 },
}) })
.then(({ data }) => { .then(({ data }) => {
console.log(">> 'get' success"); 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", "sequelize": "^4.1.0",
"socket.io": "^2.0.1", "socket.io": "^2.0.1",
"socketio-file-upload": "^0.6.0", "socketio-file-upload": "^0.6.0",
"universal-analytics": "^0.4.13" "universal-analytics": "^0.4.13",
"winston": "^2.3.1"
}, },
"devDependencies": { "devDependencies": {
"eslint": "3.19.0", "eslint": "3.19.0",

View file

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

View file

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

View file

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

View file

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

View file

@ -1,11 +1,12 @@
const publishController = require('../controllers/publishController.js'); const publishController = require('../controllers/publishController.js');
const logger = require('winston');
module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => { module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
const http = require('http').Server(app); const http = require('http').Server(app);
const io = require('socket.io')(http); const io = require('socket.io')(http);
io.on('connection', socket => { io.on('connection', socket => {
console.log('a user connected via sockets'); logger.silly('a user connected via sockets');
// create visitor record // create visitor record
const visitor = ua(googleAnalyticsId, { https: true }); const visitor = ua(googleAnalyticsId, { https: true });
// attach upload listeners // attach upload listeners
@ -13,28 +14,30 @@ module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
uploader.dir = hostedContentPath; uploader.dir = hostedContentPath;
uploader.listen(socket); uploader.listen(socket);
uploader.on('start', ({ file }) => { uploader.on('start', ({ file }) => {
logger.info('Client started an upload', file);
// server side test to make sure file is not a bad file type // server side test to make sure file is not a bad file type
if (/\.exe$/.test(file.name)) { if (/\.exe$/.test(file.name)) {
uploader.abort(file.id, socket); uploader.abort(file.id, socket);
} }
}); });
uploader.on('error', ({ error }) => { 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); socket.emit('publish-status', error);
}); });
uploader.on('saved', ({ file }) => { uploader.on('saved', ({ file }) => {
console.log('uploaded ', file.name);
if (file.success) { if (file.success) {
logger.info(`Client successfully uploaded ${file.name}`);
socket.emit('publish-status', 'file upload successfully completed'); 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); publishController.publish(file.meta.name, file.name, file.pathName, file.meta.type, file.meta.license, file.meta.nsfw, socket, visitor);
} else { } else {
logger.error(`An error occurred in uploading the client's file`);
socket.emit('publish-failure', 'file uploaded, but with errors'); 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 // handle disconnect
socket.on('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 Handlebars = require('handlebars');
const config = require('config'); const config = require('config');
const ua = require('universal-analytics'); const ua = require('universal-analytics');
const winston = require('winston');
const googleAnalyticsId = config.get('AnalyticsConfig.googleId'); const googleAnalyticsId = config.get('AnalyticsConfig.googleId');
const hostedContentPath = config.get('Database.PublishUploadPath'); 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 // set port
const PORT = 3000; const PORT = 3000;
// initialize express app // initialize express app
@ -63,6 +69,6 @@ const http = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath
// start server // start server
db.sequelize.sync({}).then(() => { db.sequelize.sync({}).then(() => {
http.listen(PORT, () => { http.listen(PORT, () => {
console.log(`Listening on PORT ${PORT}`); winston.info(`Server is listening on PORT ${PORT}`);
}); });
}); });