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

feat(logger): add logger, console->logger migration #78

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 14 additions & 11 deletions caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var routeConfig = require("./routes.json");
var helmet = require('helmet');
const fs = require('fs');

/** loading services */
const {logger} = require("./services/logger/winston");

// handlers
const auth = require('./handlers/authHandlers.js');
const monitor = require('./handlers/monitorHandlers.js');
Expand Down Expand Up @@ -138,12 +141,12 @@ for (let i in routeConfig) {
if (Object.prototype.hasOwnProperty.call(routeConfig, i)) {
let rule = routeConfig[i];
if (!rule.method) {
console.error('rule number '+ i +' has no "method"');
logger.error('rule number '+ i +' has no "method"');
process.exit(1);
}
if (rule.method == 'static') {
if (!rule.use) {
console.error('rule number '+ i +' is static and has no "use"');
logger.error('rule number '+ i +' is static and has no "use"');
process.exit(1);
}
app.use(express.static(rule.use));
Expand All @@ -152,15 +155,15 @@ for (let i in routeConfig) {
if (Object.prototype.hasOwnProperty.call(rule.handlers, j)) {
let handler = rule.handlers[j];
if (!rule.route) {
console.error('rule number '+ i +' has no "route"');
logger.error('rule number '+ i +' has no "route"');
process.exit(1);
}
if (!handler.function) {
console.error('rule number '+ i +' handler ' + j + ' has no "function"');
logger.error('rule number '+ i +' handler ' + j + ' has no "function"');
process.exit(1);
}
if (! HANDLERS.hasOwnProperty(handler.function)) {
console.error('handler named "'+ handler.function + '" not found (rule '+ i +' handler ' + j + ')');
logger.error('handler named "'+ handler.function + '" not found (rule '+ i +' handler ' + j + ')');
process.exit(1);
}
let args = handler.args || [];
Expand All @@ -186,9 +189,9 @@ app.use(function(err, req, res, next) {
// wrap strings in a json
if (typeof err === 'string' || err instanceof String) {
err = {'error': err};
console.error(err);
logger.error(err);
} else {
console.error(err.error || err.message || err.toString());
logger.error(err.error || err.message || err.toString());
}
res.status(statusCode).json(err);
});
Expand All @@ -201,17 +204,17 @@ var startApp = function(app) {
var sslPkPath = "./ssl/privatekey.pem";
var sslCertPath = "./ssl/certificate.pem";
if (fs.existsSync(sslPkPath) && fs.existsSync(sslCertPath)) {
console.info("Starting in HTTPS Mode mode");
logger.info("Starting in HTTPS Mode mode");
httpsOptions.key = fs.readFileSync(sslPkPath, 'utf8');
httpsOptions.cert = fs.readFileSync(sslCertPath, 'utf8');
}
} catch (err) {
console.error(err);
logger.error(err);
}
if (httpsOptions.key && httpsOptions.cert) {
https.createServer(httpsOptions, app).listen(PORT, () => console.log('listening HTTPS on ' + PORT));
https.createServer(httpsOptions, app).listen(PORT, () => logger.info(`listening HTTPS on ${PORT}`));
} else {
app.listen(PORT, () => console.log('listening on ' + PORT));
app.listen(PORT, () => logger.info(`listening on ${PORT}`));
}
};
};
Expand Down
31 changes: 16 additions & 15 deletions handlers/authHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jwksClient = require('jwks-rsa');
var atob = require('atob');
var fs = require('fs');
var filterFunction = require('./filterFunction.js');
const {logger} = require("../services/logger/winston");

const {execSync} = require('child_process');
const preCommand = "openssl req -subj ";
Expand All @@ -24,7 +25,7 @@ if (!fs.existsSync('./keys/key') && !fs.existsSync('./keys/key.pub') && GENERATE
try {
execSync(`${preCommand}'/CN=www.camicroscope.com/O=caMicroscope Local Instance Key./C=US'${postCommand}`);
} catch (err) {
console.log({err: err});
logger.info({err: err});
}
}

Expand All @@ -35,13 +36,13 @@ try {
} else {
if (DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
PRIKEY = '';
console.warn('prikey null since DISABLE_SEC and no prikey provided');
logger.warn('prikey null since DISABLE_SEC and no prikey provided');
} else {
console.error('prikey does not exist');
logger.error('prikey does not exist');
}
}
} catch (err) {
console.error(err);
logger.error(err);
}

try {
Expand All @@ -51,13 +52,13 @@ try {
} else {
if (DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
PUBKEY = '';
console.warn('pubkey null since DISABLE_SEC and no prikey provided');
logger.warn('pubkey null since DISABLE_SEC and no prikey provided');
} else {
console.error('pubkey does not exist');
logger.error('pubkey does not exist');
}
}
} catch (err) {
console.error(err);
logger.error(err);
}

if (DISABLE_SEC && !JWK_URL) {
Expand All @@ -69,7 +70,7 @@ if (DISABLE_SEC && !JWK_URL) {
jwksUri: JWK_URL,
});
} else {
console.error('need JWKS URL (JWK_URL)');
logger.error('need JWKS URL (JWK_URL)');
process.exit(1);
}

Expand Down Expand Up @@ -104,9 +105,9 @@ function jwkTokenTrade(jwksClient, signKey, userFunction) {
res.status(401).send('{"err":"no token found"}');
}
jwksClient.getSigningKey(getJwtKid(THISTOKEN), (err, key) => {
// console.log(key);
// logger.info(key);
if (err) {
console.error(err);
logger.error(err);
res.status(401).send({
'err': err,
});
Expand All @@ -131,7 +132,7 @@ function tokenTrade(checkKey, signKey, userFunction) {
}
jwt.verify(THISTOKEN, checkKey, jwtOptions, function(err, token) {
if (err) {
console.error(err);
logger.error(err);
res.status(401).send({
'err': err,
});
Expand All @@ -143,7 +144,7 @@ function tokenTrade(checkKey, signKey, userFunction) {
});
} else {
userFunction(token).then((x) => {
// console.log(x);
// logger.info(x);
if (x === false) {
res.status(401).send({
'err': 'User Unauthorized',
Expand All @@ -161,7 +162,7 @@ function tokenTrade(checkKey, signKey, userFunction) {
});
}
}).catch((e) => {
console.log(e);
logger.info(e);
res.status(401).send(e);
});
}
Expand All @@ -177,7 +178,7 @@ function loginHandler(checkKey) {
try {
token = jwt.decode(getToken(req)) || {};
} catch (e) {
console.warn(e);
logger.warn(e);
}
req.tokenInfo = token;
req.userType = token.userType || DEFAULT_USER_TYPE || 'Null';
Expand All @@ -193,7 +194,7 @@ function loginHandler(checkKey) {
}
jwt.verify(getToken(req), checkKey, jwtOptions, function(err, token) {
if (err) {
console.error(err);
logger.error(err);
res.status(401).send({
'err': err,
});
Expand Down
12 changes: 6 additions & 6 deletions handlers/dataHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var mongo = require('mongodb');

var MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost';
var DISABLE_SEC = (process.env.DISABLE_SEC === 'true') || false;

const {logger} = require("../services/logger/winston");

function mongoFind(database, collection, query) {
return new Promise(function(res, rej) {
Expand Down Expand Up @@ -54,7 +54,7 @@ function mongoDistinct(database, collection, upon, query) {
}
});
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand Down Expand Up @@ -82,7 +82,7 @@ function mongoAdd(database, collection, data) {
}
});
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand All @@ -109,7 +109,7 @@ function mongoDelete(database, collection, query) {
});
}
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand Down Expand Up @@ -139,7 +139,7 @@ function mongoAggregate(database, collection, pipeline) {
});
}
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand All @@ -159,7 +159,7 @@ function mongoUpdate(database, collection, query, newVals) {
}
dbo.collection(collection).updateOne(query, newVals, function(err, result) {
if (err) {
console.log(err);
logger.error(err);
rej(err);
}
delete result.connection;
Expand Down
29 changes: 15 additions & 14 deletions handlers/dataTransformationHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const client = require("mongodb").MongoClient;
const fs = require("fs");
const {logger} = require("../services/logger/winston");

class DataTransformationHandler {
constructor(url, path) {
Expand All @@ -14,7 +15,7 @@ class DataTransformationHandler {
}

startHandler() {
console.log("||-- Start --||");
logger.info("||-- Start --||");
this.counter = 0;
this.max = 300;
this.id = 1;
Expand All @@ -29,7 +30,7 @@ class DataTransformationHandler {
config = JSON.parse(rawdata);
} catch (err) {
if (err.code == "ENOENT") {
console.log(`'${this.filePath}' File Fot Found!`);
logger.info(`'${this.filePath}' File Fot Found!`);
} else {
throw err;
}
Expand All @@ -54,8 +55,8 @@ class DataTransformationHandler {
collection.find(query).toArray((err, rs) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
Expand All @@ -68,8 +69,8 @@ class DataTransformationHandler {
collection.insertOne(defaultData, (err, result) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
Expand All @@ -95,32 +96,32 @@ class DataTransformationHandler {
collection.deleteOne(query, (err, result) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
}
collection.insertOne(config, (err, result) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
}
this.isProcessing = false;
console.log(`||-- The Document At The 'configuration' Collection Has Been Upgraded To Version 1.0.0 --||`);
logger.info(`||-- The Document At The 'configuration' Collection Has Been Upgraded To Version 1.0.0 --||`);
dbc.close();
this.cleanHandler();
});
});
}
});
}).catch((err) => {
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err.message);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err.message);
this.isProcessing = false;
if (this.max == this.counter) {
this.cleanHandler();
Expand All @@ -144,7 +145,7 @@ class DataTransformationHandler {
}

cleanHandler() {
console.log("||-- Done --||");
logger.info("||-- Done --||");
this.counter = 0;
this.max = 300;
this.id = 1;
Expand Down
4 changes: 3 additions & 1 deletion handlers/datasetHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const crypto = require("crypto");
const AdmZip = require('adm-zip');
const path = require('path');

const {logger} = require("../services/logger/winston");

let LABELS_PATH = null;
let IMAGES_SPRITE_PATH = null;

Expand Down Expand Up @@ -155,7 +157,7 @@ function deleteData() {
if (err) {
throw err;
}
console.log(`Temp folder deleted!`);
logger.info(`Temp folder deleted!`);
});
res.json({status: 'Temp folder deleted!'});
};
Expand Down
3 changes: 2 additions & 1 deletion handlers/iipHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var proxy = require('http-proxy-middleware');

var IIP_PATH = process.env.IIP_PATH || 'http://ca-iip/';
const {logger} = require("../services/logger/winston");

iipHandler = function(req, res, next) {
if (req.query) {
Expand All @@ -22,7 +23,7 @@ iipHandler = function(req, res, next) {
proxy({
secure: false,
onError(err, req, res) {
console.log(err);
logger.error(err);
err.statusCode = 500;
next(err);
},
Expand Down
Loading