Refactor logging

This commit is contained in:
Calvin Montgomery 2017-04-04 23:02:31 -07:00
parent b1a328d2e0
commit 8306d2d1b6
29 changed files with 268 additions and 194 deletions

View file

@ -2,7 +2,9 @@ var $util = require("../utilities");
var bcrypt = require("bcrypt");
var db = require("../database");
var Config = require("../config");
var Logger = require("../logger");
import { LoggerFactory } from '@calzoneman/jsli';
const LOGGER = LoggerFactory.getLogger('database/accounts');
var registrationLock = {};
var blackHole = function () { };
@ -450,7 +452,7 @@ module.exports = {
userprof.text = profile.text || "";
callback(null, userprof);
} catch (e) {
Logger.errlog.log("Corrupt profile: " + rows[0].profile +
LOGGER.error("Corrupt profile: " + rows[0].profile +
" (user: " + name + ")");
callback(null, userprof);
}

View file

@ -2,10 +2,12 @@ var db = require("../database");
var valid = require("../utilities").isValidChannelName;
var fs = require("fs");
var path = require("path");
var Logger = require("../logger");
var tables = require("./tables");
var Flags = require("../flags");
var util = require("../utilities");
import { LoggerFactory } from '@calzoneman/jsli';
const LOGGER = LoggerFactory.getLogger('database/channels');
var blackHole = function () { };
@ -193,27 +195,27 @@ module.exports = {
module.exports.deleteBans(name, function (err) {
if (err) {
Logger.errlog.log("Failed to delete bans for " + name + ": " + err);
LOGGER.error("Failed to delete bans for " + name + ": " + err);
}
});
module.exports.deleteLibrary(name, function (err) {
if (err) {
Logger.errlog.log("Failed to delete library for " + name + ": " + err);
LOGGER.error("Failed to delete library for " + name + ": " + err);
}
});
module.exports.deleteAllRanks(name, function (err) {
if (err) {
Logger.errlog.log("Failed to delete ranks for " + name + ": " + err);
LOGGER.error("Failed to delete ranks for " + name + ": " + err);
}
});
fs.unlink(path.join(__dirname, "..", "..", "chandump", name),
function (err) {
if (err && err.code !== "ENOENT") {
Logger.errlog.log("Deleting chandump failed:");
Logger.errlog.log(err);
LOGGER.error("Deleting chandump failed:");
LOGGER.error(err);
}
});
@ -654,7 +656,7 @@ module.exports = {
db.query("UPDATE channels SET last_loaded = ? WHERE id = ?", [new Date(), channelId], error => {
if (error) {
Logger.errlog.log(`Failed to update last_loaded column for channel ID ${channelId}: ${error}`);
LOGGER.error(`Failed to update last_loaded column for channel ID ${channelId}: ${error}`);
}
});
},
@ -669,7 +671,7 @@ module.exports = {
db.query("UPDATE channels SET owner_last_seen = ? WHERE id = ?", [new Date(), channelId], error => {
if (error) {
Logger.errlog.log(`Failed to update owner_last_seen column for channel ID ${channelId}: ${error}`);
LOGGER.error(`Failed to update owner_last_seen column for channel ID ${channelId}: ${error}`);
}
});
}

View file

@ -1,7 +1,9 @@
var db = require("../database");
var Logger = require("../logger");
var Q = require("q");
import Promise from 'bluebird';
import { LoggerFactory } from '@calzoneman/jsli';
const LOGGER = LoggerFactory.getLogger('database/update');
const DB_VERSION = 11;
var hasUpdates = [];
@ -13,7 +15,7 @@ module.exports.checkVersion = function () {
}
if (rows.length === 0) {
Logger.errlog.log("[Warning] db_version key missing from database. Setting " +
LOGGER.warn("db_version key missing from database. Setting " +
"db_version=" + DB_VERSION);
db.query("INSERT INTO `meta` (`key`, `value`) VALUES ('db_version', ?)",
[DB_VERSION],
@ -26,7 +28,7 @@ module.exports.checkVersion = function () {
}
var next = function () {
hasUpdates.push(v);
Logger.syslog.log("Updated database to version " + v);
LOGGER.info("Updated database to version " + v);
if (v < DB_VERSION) {
update(v++, next);
} else {
@ -48,7 +50,7 @@ function update(version, cb) {
Q.nfcall(mergeChannelRanks),
Q.nfcall(mergeChannelBans)
]).done(function () {
Logger.syslog.log("Merged channel tables. Please verify that everything " +
LOGGER.info("Merged channel tables. Please verify that everything " +
"is working correctly, and then type '/delete_old_tables'" +
" into the CyTube process to remove the unused tables.");
cb();
@ -71,7 +73,7 @@ function update(version, cb) {
}
function addMetaColumnToLibraries(cb) {
Logger.syslog.log("[database] db version indicates channel libraries don't have " +
LOGGER.info("db version indicates channel libraries don't have " +
"meta column. Updating...");
Q.nfcall(db.query, "SHOW TABLES")
.then(function (rows) {
@ -85,14 +87,14 @@ function addMetaColumnToLibraries(cb) {
rows.forEach(function (table) {
queue.push(Q.nfcall(db.query, "ALTER TABLE `" + table + "` ADD meta TEXT")
.then(function () {
Logger.syslog.log("Added meta column to " + table);
LOGGER.info("Added meta column to " + table);
})
);
});
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Adding meta column to library tables failed: " + err);
LOGGER.error("Adding meta column to library tables failed: " + err);
}).done(cb);
}
@ -112,12 +114,12 @@ function mergeChannelLibraries(cb) {
"INSERT INTO `channel_libraries` SELECT id, title, seconds, type, meta, ?" +
" AS channel FROM `" + table + "`", [name])
.then(function () {
Logger.syslog.log("Copied " + table + " to channel_libraries");
LOGGER.info("Copied " + table + " to channel_libraries");
}).catch(function (err) {
Logger.errlog.log("Copying " + table + " to channel_libraries failed: " +
LOGGER.error("Copying " + table + " to channel_libraries failed: " +
err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
})
);
@ -125,9 +127,9 @@ function mergeChannelLibraries(cb) {
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Copying libraries to channel_libraries failed: " + err);
LOGGER.error("Copying libraries to channel_libraries failed: " + err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
}).done(function () { cb(null); });
}
@ -148,12 +150,12 @@ function mergeChannelRanks(cb) {
"INSERT INTO `channel_ranks` SELECT name, rank, ?" +
" AS channel FROM `" + table + "`", [name])
.then(function () {
Logger.syslog.log("Copied " + table + " to channel_ranks");
LOGGER.info("Copied " + table + " to channel_ranks");
}).catch(function (err) {
Logger.errlog.log("Copying " + table + " to channel_ranks failed: " +
LOGGER.error("Copying " + table + " to channel_ranks failed: " +
err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
})
);
@ -161,9 +163,9 @@ function mergeChannelRanks(cb) {
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Copying ranks to channel_ranks failed: " + err);
LOGGER.error("Copying ranks to channel_ranks failed: " + err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
}).done(function () { cb(null); });
}
@ -184,12 +186,12 @@ function mergeChannelBans(cb) {
"INSERT INTO `channel_bans` SELECT id, ip, name, bannedby, reason, ?" +
" AS channel FROM `" + table + "`", [name])
.then(function () {
Logger.syslog.log("Copied " + table + " to channel_bans");
LOGGER.info("Copied " + table + " to channel_bans");
}).catch(function (err) {
Logger.errlog.log("Copying " + table + " to channel_bans failed: " +
LOGGER.error("Copying " + table + " to channel_bans failed: " +
err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
})
);
@ -197,9 +199,9 @@ function mergeChannelBans(cb) {
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Copying ranks to channel_bans failed: " + err);
LOGGER.error("Copying ranks to channel_bans failed: " + err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
}).done(function () { cb(null); });
}
@ -217,11 +219,11 @@ module.exports.deleteOldChannelTables = function (cb) {
rows.forEach(function (table) {
queue.push(Q.nfcall(db.query, "DROP TABLE `" + table + "`")
.then(function () {
Logger.syslog.log("Deleted " + table);
LOGGER.info("Deleted " + table);
}).catch(function (err) {
Logger.errlog.log("Deleting " + table + " failed: " + err);
LOGGER.error("Deleting " + table + " failed: " + err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
})
);
@ -229,9 +231,9 @@ module.exports.deleteOldChannelTables = function (cb) {
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Deleting old tables failed: " + err);
LOGGER.error("Deleting old tables failed: " + err);
if (err.stack) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
}
}).done(cb);
};
@ -247,10 +249,10 @@ function fixUtf8mb4(cb) {
Q.allSettled(queries.map(function (query) {
return Q.nfcall(db.query, query);
})).then(function () {
Logger.syslog.log("Fixed utf8mb4");
LOGGER.info("Fixed utf8mb4");
cb();
}).catch(function (e) {
Logger.errlog.log("Failed to fix utf8mb4: " + e);
LOGGER.error("Failed to fix utf8mb4: " + e);
});
};
@ -276,7 +278,7 @@ function fixCustomEmbeds(cb) {
});
Q.allSettled(all).then(function () {
Logger.syslog.log("Converted custom embeds.");
LOGGER.info("Converted custom embeds.");
cb();
});
});
@ -312,7 +314,7 @@ function fixCustomEmbedsInUserPlaylists(cb) {
try {
media = CustomEmbedFilter(item.id);
} catch (e) {
Logger.syslog.log("WARNING: Unable to convert " + item.id);
LOGGER.info("WARNING: Unable to convert " + item.id);
continue;
}
@ -332,19 +334,19 @@ function fixCustomEmbedsInUserPlaylists(cb) {
});
Q.allSettled(all).then(function () {
Logger.syslog.log('Fixed custom embeds in user_playlists');
LOGGER.info('Fixed custom embeds in user_playlists');
cb();
});
}).catch(function (err) {
Logger.errlog.log(err.stack);
LOGGER.error(err.stack);
});
}
function addUsernameDedupeColumn(cb) {
Logger.syslog.log("Adding name_dedupe column on the users table");
LOGGER.info("Adding name_dedupe column on the users table");
db.query("ALTER TABLE users ADD COLUMN name_dedupe VARCHAR(20) UNIQUE DEFAULT NULL", (error) => {
if (error) {
Logger.errlog.log(`Unable to add name_dedupe column: ${error}`);
LOGGER.error(`Unable to add name_dedupe column: ${error}`);
} else {
cb();
}
@ -353,10 +355,10 @@ function addUsernameDedupeColumn(cb) {
function populateUsernameDedupeColumn(cb) {
const dbUsers = require("./accounts");
Logger.syslog.log("Populating name_dedupe column on the users table");
LOGGER.info("Populating name_dedupe column on the users table");
db.query("SELECT id, name FROM users WHERE name_dedupe IS NULL", (err, rows) => {
if (err) {
Logger.errlog.log("Unable to perform database upgrade to add dedupe column: " + err);
LOGGER.error("Unable to perform database upgrade to add dedupe column: " + err);
return;
}
@ -369,12 +371,12 @@ function populateUsernameDedupeColumn(cb) {
}
const dedupedName = dbUsers.dedupeUsername(row.name);
Logger.syslog.log(`Deduping [${row.name}] as [${dedupedName}]`);
LOGGER.info(`Deduping [${row.name}] as [${dedupedName}]`);
conn.query("UPDATE users SET name_dedupe = ? WHERE id = ?", [dedupedName, row.id], (error, res) => {
conn.release();
if (error) {
if (error.errno === 1062) {
Logger.syslog.log(`WARNING: could not set name_dedupe for [${row.name}] due to an existing row for [${dedupedName}]`);
LOGGER.info(`WARNING: could not set name_dedupe for [${row.name}] due to an existing row for [${dedupedName}]`);
resolve();
} else {
reject(error);
@ -388,7 +390,7 @@ function populateUsernameDedupeColumn(cb) {
}, { concurrency: 10 }).then(() => {
cb();
}).catch(error => {
Logger.errlog.log("Unable to perform database upgrade to add dedupe column: " + (error.stack ? error.stack : error));
LOGGER.error("Unable to perform database upgrade to add dedupe column: " + (error.stack ? error.stack : error));
})
});
}
@ -396,13 +398,13 @@ function populateUsernameDedupeColumn(cb) {
function addChannelLastLoadedColumn(cb) {
db.query("ALTER TABLE channels ADD COLUMN last_loaded TIMESTAMP NOT NULL DEFAULT 0", error => {
if (error) {
Logger.errlog.log(`Failed to add last_loaded column: ${error}`);
LOGGER.error(`Failed to add last_loaded column: ${error}`);
return;
}
db.query("ALTER TABLE channels ADD INDEX i_last_loaded (last_loaded)", error => {
if (error) {
Logger.errlog.log(`Failed to add index on last_loaded column: ${error}`);
LOGGER.error(`Failed to add index on last_loaded column: ${error}`);
return;
}
@ -414,13 +416,13 @@ function addChannelLastLoadedColumn(cb) {
function addChannelOwnerLastSeenColumn(cb) {
db.query("ALTER TABLE channels ADD COLUMN owner_last_seen TIMESTAMP NOT NULL DEFAULT 0", error => {
if (error) {
Logger.errlog.log(`Failed to add owner_last_seen column: ${error}`);
LOGGER.error(`Failed to add owner_last_seen column: ${error}`);
return;
}
db.query("ALTER TABLE channels ADD INDEX i_owner_last_seen (owner_last_seen)", error => {
if (error) {
Logger.errlog.log(`Failed to add index on owner_last_seen column: ${error}`);
LOGGER.error(`Failed to add index on owner_last_seen column: ${error}`);
return;
}