You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.8 KiB
JavaScript

"use strict";
const chalk = require("chalk");
const util = require("util");
const pad = require("pad");
function concatenateArguments(args) {
let stringified = args.map((arg) => {
if (typeof arg === "string") {
return arg;
} else {
return util.inspect(arg, {depth: null, colors: false});
}
});
return stringified.join(" ");
}
let minimumLabelWidth = 9;
function padLabel(label) {
/* The template string below is to ensure that the label is always passed in as a string, otherwise the `pad` library will get confused. */
return pad(`${label}`, minimumLabelWidth);
}
module.exports = function createScrapeLogger({ name, itemFormatter }) {
return {
log: function log(message) {
console.log(`${chalk.bold.gray(`[${name}]`)} ${message}`);
},
logWithLabel: function logWithLabel(color, label, message) {
let labelWidth = Math.max(minimumLabelWidth, label.length);
let lines = message.split("\n");
this.log(`${color(`[${padLabel(label)}]`)} ${lines[0]}`);
lines.slice(1).forEach((line) => {
this.log(`${color(`${pad(labelWidth + 1, "")}|`)} ${line}`);
});
},
warning: function logWarning(...args) {
let message = concatenateArguments(args);
this.logWithLabel(chalk.bold.yellow, "⚠ WARNING", message);
},
error: function logError(...args) {
let message = concatenateArguments(args);
this.logWithLabel(chalk.bold.red, "✖ ERROR", message);
},
info: function logInfo(...args) {
let message = concatenateArguments(args);
this.logWithLabel(chalk.bold.cyan, "* info", message);
},
done: function done(item) {
let formatted = itemFormatter({ item });
this.logWithLabel(chalk.bold.green, "✔ done", formatted);
},
debug: function debug(...args) {
if (process.env.SCRAPER_DEBUG === "1") {
let message = concatenateArguments(args);
this.log(chalk.gray(message));
}
}
};
};