From ba4ef5bf909dd69c675a2a67ee57dd2ec377e689 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 22 Jul 2020 16:10:33 -0700 Subject: [PATCH] blueslip: Convert Logger to an ES6 class. Signed-off-by: Anders Kaseorg --- static/js/blueslip.js | 97 +++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 55 deletions(-) diff --git a/static/js/blueslip.js b/static/js/blueslip.js index b0da2052b6..094b7e8078 100644 --- a/static/js/blueslip.js +++ b/static/js/blueslip.js @@ -12,69 +12,56 @@ if (Error.stackTraceLimit !== undefined) { Error.stackTraceLimit = 100000; } -function Logger() { - this._memory_log = []; +function pad(num, width) { + return num.toString().padStart(width, "0"); } -Logger.prototype = (function () { - function pad(num, width) { - let ret = num.toString(); - while (ret.length < width) { - ret = "0" + ret; +function make_logger_func(name) { + return function Logger_func(...args) { + const now = new Date(); + const date_str = + now.getUTCFullYear() + + "-" + + pad(now.getUTCMonth() + 1, 2) + + "-" + + pad(now.getUTCDate(), 2) + + " " + + pad(now.getUTCHours(), 2) + + ":" + + pad(now.getUTCMinutes(), 2) + + ":" + + pad(now.getUTCSeconds(), 2) + + "." + + pad(now.getUTCMilliseconds(), 3) + + " UTC"; + + const str_args = args.map((x) => (typeof x === "object" ? JSON.stringify(x) : x)); + + const log_entry = date_str + " " + name.toUpperCase() + ": " + str_args.join(""); + this._memory_log.push(log_entry); + + // Don't let the log grow without bound + if (this._memory_log.length > 1000) { + this._memory_log.shift(); } - return ret; - } - function make_logger_func(name) { - return function Logger_func(...args) { - const now = new Date(); - const date_str = - now.getUTCFullYear() + - "-" + - pad(now.getUTCMonth() + 1, 2) + - "-" + - pad(now.getUTCDate(), 2) + - " " + - pad(now.getUTCHours(), 2) + - ":" + - pad(now.getUTCMinutes(), 2) + - ":" + - pad(now.getUTCSeconds(), 2) + - "." + - pad(now.getUTCMilliseconds(), 3) + - " UTC"; - - const str_args = args.map((x) => (typeof x === "object" ? JSON.stringify(x) : x)); - - const log_entry = date_str + " " + name.toUpperCase() + ": " + str_args.join(""); - this._memory_log.push(log_entry); - - // Don't let the log grow without bound - if (this._memory_log.length > 1000) { - this._memory_log.shift(); - } - - if (console[name] !== undefined) { - return console[name](...args); - } - return; - }; - } - - const proto = { - get_log: function Logger_get_log() { - return this._memory_log; - }, + if (console[name] !== undefined) { + return console[name](...args); + } }; +} - const methods = ["debug", "log", "info", "warn", "error"]; - let i; - for (i = 0; i < methods.length; i += 1) { - proto[methods[i]] = make_logger_func(methods[i]); +class Logger { + _memory_log = []; + + get_log() { + return this._memory_log; } +} - return proto; -})(); +for (const name of ["debug", "log", "info", "warn", "error"]) { + Logger.prototype[name] = make_logger_func(name); +} const logger = new Logger();