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

Issue 2678 - fixes for nodejs 14 upgrade #2682

Merged
merged 32 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b7d7997
ISSUE #2678 removed fs.extra dependency
carmenfan Jul 7, 2021
3c2d949
Remove unused libraries, replace ip-to-location with geoip-lite
carmenfan Jul 8, 2021
2a7bbc0
ISSUE #2678 centralise UUID generation in utils
carmenfan Jul 8, 2021
14eec19
ISSUE #2678 couple more upgrades
carmenfan Jul 8, 2021
11351fa
ISSUE #2678 linter fix
carmenfan Jul 8, 2021
ff5d7e5
ISSUE #2678 fix uuid lib update
carmenfan Jul 8, 2021
ff6c67a
ISSUE #2678 format logger
carmenfan Jul 8, 2021
e866674
ISSUE #2678 log user name to request if available
carmenfan Jul 8, 2021
ebaeb80
ISSUE #2678 option to have no colours on the log
carmenfan Jul 8, 2021
453bf22
ISSUE #2678 track network latency, reformat the log
carmenfan Jul 8, 2021
3428510
ISSUE #2678 add network label on all network io logging
carmenfan Jul 8, 2021
8c2a394
ISSUE #2678 NET instead of NETWORK
carmenfan Jul 8, 2021
74bb4ef
ISSUE #2678 more log formatting, pass req to genResponseLog on the st…
carmenfan Jul 9, 2021
a09b6d2
ISSUE #2678 mocha now needs a recursive flag
carmenfan Jul 9, 2021
9c9ea6d
ISSUE #2678 fix user creations
carmenfan Jul 9, 2021
0fbd88b
Merge branch 'staging' into ISSUE_2678
carmenfan Jul 9, 2021
53fa5fc
ISSUE #2678 downgrade socket io - it's not working
carmenfan Jul 9, 2021
181075e
ISSUE #2678 fix all createUser calls
carmenfan Jul 9, 2021
ffd6b23
ISSUE #2678 mocha needs to run with --exit; only log if there's trans…
carmenfan Jul 9, 2021
c7c0c8c
ISSUE #2678 remove tag check
carmenfan Jul 9, 2021
9f2368d
ISSUE #2678 only run meta - struggling to reproduce.
carmenfan Jul 9, 2021
ef79776
Revert "ISSUE #2678 only run meta - struggling to reproduce."
carmenfan Jul 9, 2021
983f5f5
ISSUE #2678 if no collaborator/issues?
carmenfan Jul 9, 2021
eefe677
ISSUE #2678 try downgrading mocha
carmenfan Jul 9, 2021
b950641
ISSUE #2678 debug logging on metadata endpoint
carmenfan Jul 9, 2021
456ec0c
ISSUE #2678 more debugging
carmenfan Jul 9, 2021
0a505b4
ISSUE #2678 more debugging
carmenfan Jul 9, 2021
362fc2f
ISSUE #2678 more debugging
carmenfan Jul 10, 2021
0be7b32
ISSUE #2678 downgrade nyc
carmenfan Jul 10, 2021
ad7a015
ISSUE #2678 don't run code coverage?
carmenfan Jul 10, 2021
e886ecb
ISSUE #2678 move mocha back to v9
carmenfan Jul 10, 2021
98a02db
Merge branch 'staging' into ISSUE_2678
carmenfan Jul 11, 2021
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ matrix:
script:
- cd ../backend
- mkdir coverage
- unbuffer yarn test:coverall
- unbuffer yarn test
- cd ../
- NODE_ENV=test NODE_CONFIG_DIR='./config' node "./backend/3drepo.js" &
- cd ./frontend
Expand Down
3 changes: 1 addition & 2 deletions backend/3drepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ function logCreateService(serverConfig) {
"Loading " +
serverConfig.service +
" on " + serverConfig.hostname + ":" +
serverConfig.port,
serverConfig.host_dir
serverConfig.port
);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ config.default_format = coalesce(config.default_format, "html");
// config.external = (config.js_debug_level === "debug") ? frontend_scripts.debug_scripts : frontend_scripts.prod_scripts;

// Log file options
config.logfile = coalesce(config.logfile, {});
config.logfile = config.logfile || {};

if (!config.logfile.filename) {
config.logfile.logDirectory = coalesce(config.logfile.logDirectory, "/var/log");
Expand Down
3 changes: 1 addition & 2 deletions backend/handler/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const fs = require("fs");
const path = require("path");
const ResponseCodes = require("../response_codes");
const systemLogger = require("../logger").systemLogger;
const nodeuuid = require("uuid/v1");
const farmhash = require("farmhash");
const utils = require("../utils");

Expand Down Expand Up @@ -72,7 +71,7 @@ class FSHandler {
}

storeFile(data) {
const _id = nodeuuid();
const _id = utils.generateUUID({string: true});
const folderNames = generateFoldernames(_id, config.fs.levels);
const link = path.posix.join(folderNames, _id);

Expand Down
4 changes: 2 additions & 2 deletions backend/handler/gridfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"use strict";

const DB = require("./db");
const nodeuuid = require("uuid/v1");
const utils = require("../utils");

class GridFSHandler {
constructor() {
Expand All @@ -40,7 +40,7 @@ class GridFSHandler {
}

storeFile(account, col, data) {
const _id = nodeuuid();
const _id = utils.generateUUID({string: true});
return DB.storeFileInGridFS(account, this.cleanColName(col), _id, data).then(() => (
{_id, link: _id, size: data.length, type: "gridfs"}
));
Expand Down
172 changes: 49 additions & 123 deletions backend/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,88 +20,55 @@ const config = require("./config.js");
const winston = require("winston");
require("winston-daily-rotate-file");

let log;
const stringFormat = ({ level, message, label, timestamp }) => `${timestamp} [${level}] [${label || "APP"}] ${message}`;

/**
* The repoLogger init and factory
*
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {string} id - Unique logger ID
* @returns
*/
const repoLogger = function (req, res, id) {

const self = this instanceof repoLogger ? this : Object.create(repoLogger.prototype);

self.uid = id;

if (req) {
self.session = req.session;
self.req = req;
}

self.res = res;

if (!log) {
log = createLogger();
}

self.logger = log;
self.startTime = (new Date())
.getTime();

return self;
};
const logger = createLogger();
const SystemLogger = {};

function createLogger() {

// Custom logging levels for logger
const customLevels = {
levels: {
nothing: -1,
fatal: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5
}
};

let fileOutTransport;
const transporters = [];

if (config.logfile.logDirectory) {
fileOutTransport = new winston.transports.DailyRotateFile({
filename: config.logfile.logDirectory + "/3drepo",
datePattern: "-yyyy-MM-dd.log",
level: config.logfile.file_level
});
} else {
fileOutTransport = new winston.transports.File({
level: config.logfile.file_level,
filename: config.logfile.filename
});
transporters.push(
new winston.transports.DailyRotateFile({
filename: config.logfile.logDirectory + "/3drepo",
datePattern: "-yyyy-MM-dd.log",
timestamp: true,
level: config.logfile.file_level
})
);
}

const transports = [
fileOutTransport
];

if (config.logfile.silent === undefined || config.logfile.silent === false) {
transports.push(new winston.transports.Console({
transporters.push(new winston.transports.Console({
timestamp: true,
colorize: true,
level: config.logfile.console_level
}));
}

let format = winston.format.combine(
winston.format.timestamp(),
winston.format.align(),
winston.format.printf(stringFormat)
);

if (!config.logfile.noColors) {
format = winston.format.combine(
winston.format.colorize(),
format
);
}

// Creates logger which outputs to both the console
// and a log file simultaneously
// Levels are set separately in the config.
return winston.createLogger({
levels: customLevels.levels,
transports: transports
});
if (transporters.length > 0) {
return winston.createLogger({
transports: transporters,
format
});
}
}

/**
Expand All @@ -112,38 +79,16 @@ function createLogger() {
* @param {string} msg - Message to log
* @param {Object} meta - Extra data to put into the log file
*/
repoLogger.prototype.logMessage = function (type, msg, meta) {
const currentTime = (new Date())
.getTime();
const timeDiff = currentTime - this.startTime;

const metadata = Object.assign({}, meta);

if (this.session && this.session.user) {
metadata.username = this.session.user.username;
}

if (this.req) {
if (this.req.method) {
metadata.method = this.req.method;
}
if (this.req.originalUrl) {
metadata.url = this.req.originalUrl;
}
}

this.logger.log(type, (new Date())
.toISOString() + "\t" + this.uid + "\t" + msg + " [" + timeDiff + " ms]", {message: JSON.stringify(metadata)});
};
const logMessage = (msg, meta, label) => `${msg} ${meta ? JSON.stringify(meta, label) : ""}`;

/**
* Function to log an info message
*
* @param {string} msg - Information message
* @param {Object} meta - Extra informative metadata
*/
repoLogger.prototype.logInfo = function (msg, meta) {
this.logMessage("info", msg, meta);
SystemLogger.logInfo = (msg, meta, label) => {
logger && logger.info(logMessage(msg, meta, label), {label});
};

/**
Expand All @@ -152,8 +97,8 @@ repoLogger.prototype.logInfo = function (msg, meta) {
* @param {string} msg - Error message
* @param {Object} meta - Extra informative metadata
*/
repoLogger.prototype.logError = function (msg, meta) {
this.logMessage("error", msg, meta);
SystemLogger.logError = (msg, meta, label) => {
logger && logger.error(logMessage(msg, meta, label), {label});
};

/**
Expand All @@ -162,8 +107,8 @@ repoLogger.prototype.logError = function (msg, meta) {
* @param {string} msg - Debug message
* @param {Object} meta - Extra informative metadata
*/
repoLogger.prototype.logDebug = function (msg, meta) {
this.logMessage("debug", msg, meta);
SystemLogger.logDebug = (msg, meta, label) => {
logger && logger.debug(logMessage(msg, meta, label), {label});
};

/**
Expand All @@ -172,8 +117,8 @@ repoLogger.prototype.logDebug = function (msg, meta) {
* @param {string} msg - Warning message
* @param {Object} meta - Extra informative metadata
*/
repoLogger.prototype.logWarning = function (msg, meta) {
this.logMessage("warn", msg, meta);
SystemLogger.logWarning = (msg, meta, label) => {
logger && logger.warning(logMessage(msg, meta, label), {label});
};

/**
Expand All @@ -182,8 +127,8 @@ repoLogger.prototype.logWarning = function (msg, meta) {
* @param {string} msg - Warning message
* @param {Object} meta - Extra informative metadata
*/
repoLogger.prototype.logTrace = function (msg, meta) {
this.logMessage("trace", msg, meta);
SystemLogger.logTrace = (msg, meta, label) => {
logger && logger.trace(logMessage(msg, meta, label), {label});
};

/**
Expand All @@ -192,31 +137,12 @@ repoLogger.prototype.logTrace = function (msg, meta) {
* @param {string} msg - Fatal message
* @param {Object} meta - Extra informative metadata
*/
repoLogger.prototype.logFatal = function (msg, meta) {
this.logMessage("fatal", msg, meta);
SystemLogger.logFatal = (msg, meta, label) => {
logger && logger.fatal(logMessage(msg, meta, label), {label});
};

const systemLogger = new repoLogger(null, null, "system");

/**
* Middleware to call at the start of every request to
* initialize logger
*
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {function} next - Next middleware
* @returns
*/
module.exports.startRequest = function (req, res, next) {

const C = require("./constants");

req[C.REQ_REPO] = {};
req[C.REQ_REPO].logger = systemLogger;

// req[C.REQ_REPO].logger = new repoLogger(req, res, shortid.generate()); // Create logger for this request

next();
module.exports.systemLogger = SystemLogger;
module.exports.logLabels = {
network: "NET"
};

module.exports.systemLogger = systemLogger;
8 changes: 4 additions & 4 deletions backend/middlewares/sessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

"use strict";
const C = require("../constants");
const { systemLogger } = require("../logger");
const config = require("../config");
const session = require("../services/session").session(config);

Expand All @@ -30,9 +30,9 @@ module.exports = async (req, res, next) => {
session(req, res, function(err) {
if(err) {
// something is wrong with the library or the session (i.e. corrupted json file) itself, log the user out
req[C.REQ_REPO].logger.logError(`express-session internal error: ${err}`);
req[C.REQ_REPO].logger.logError(`express-session internal error: ${JSON.stringify(err)}`);
req[C.REQ_REPO].logger.logError(`express-session internal error: ${err.stack}`);
systemLogger.logError(`express-session internal error: ${err}`);
systemLogger.logError(`express-session internal error: ${JSON.stringify(err)}`);
systemLogger.logError(`express-session internal error: ${err.stack}`);
} else {
next();
}
Expand Down
4 changes: 2 additions & 2 deletions backend/models/fileRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Mailer = require("../mailer/mailer");
const ExternalServices = require("../handler/externalServices");
const ResponseCodes = require("../response_codes");
const systemLogger = require("../logger.js").systemLogger;
const nodeuuid = require("uuid/v1");
const utils = require("../utils");

const ORIGINAL_FILE_REF_EXT = ".history.ref";
const UNITY_BUNDLE_REF_EXT = ".stash.unity3d.ref";
Expand Down Expand Up @@ -302,7 +302,7 @@ FileRef.storeFile = async function(account, collection, user, name, data, extraF

FileRef.storeUrlAsResource = async function(account, model, user, name, link, extraFields = null) {
const collName = model + RESOURCES_FILE_REF_EXT;
const refInfo = {_id: nodeuuid(), link, type: "http", ...extraFields };
const refInfo = {_id: utils.generateUUID({string: true}), link, type: "http", ...extraFields };
const ref = await insertRef(account, collName, user, name, refInfo);
return ref;
};
Expand Down
3 changes: 1 addition & 2 deletions backend/models/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"use strict";

const utils = require("../utils");
const nodeuuid = require("uuid/v1");
const responseCodes = require("../response_codes.js");
const Meta = require("./meta");
const { checkRulesValidity } = require("./helper/rule");
Expand Down Expand Up @@ -282,7 +281,7 @@ Group.create = async function (account, model, branch = "master", rid = null, se
});

if (typeCorrect) {
newGroup._id = utils.stringToUUID(nodeuuid());
newGroup._id = utils.generateUUID();
newGroup.author = creator;
newGroup.createdAt = Date.now();

Expand Down
4 changes: 2 additions & 2 deletions backend/models/helper/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ async function importSuccess(account, model, sharedSpacePath, user) {
]);

if (setting) {
systemLogger.logInfo(`Model status changed to ${setting.status} and correlation ID reset`);
systemLogger.logDebug(`Model status changed to ${setting.status} and correlation ID reset`);

const updatedSetting = await setModelImportSuccess(account, model, setting.type === "toy" || setting.type === "sample");

Expand Down Expand Up @@ -271,7 +271,7 @@ function importFail(account, model, sharedSpacePath, user, errCode, errMsg) {
async function setStatus(account, model, status, user) {
try {
const setting = await setModelStatus(account, model, status);
systemLogger.logInfo(`Model status changed to ${status}`);
systemLogger.logDebug(`Model status changed to ${status}`);
ChatEvent.modelStatusChanged(null, account, model, { status, user });

return setting;
Expand Down
Loading