write own logger

since npmlog wants to add in "node-pre-gyp" onto every line
This commit is contained in:
Cynthia Foxwell 2021-08-22 14:20:49 -06:00
parent 0276b3ff64
commit a259ee8da2
8 changed files with 103 additions and 29 deletions

View file

@ -23,7 +23,6 @@
"jimp": "^0.16.1",
"murmurhash": "^2.0.0",
"node-fetch": "^2.6.1",
"npmlog": "^4.1.2",
"sharp": "^0.28.3",
"sqlite3": "^5.0.2"
},

View file

@ -8,7 +8,6 @@ specifiers:
jimp: ^0.16.1
murmurhash: ^2.0.0
node-fetch: ^2.6.1
npmlog: ^4.1.2
prettier: ^2.3.0
sharp: ^0.28.3
sqlite3: ^5.0.2
@ -16,11 +15,10 @@ specifiers:
dependencies:
colorcolor: 1.1.1
dumpy: github.com/Cynosphere/dumpy.js/5fc22353cdcb97084bab572266390e780d9f7a7b
eris: github.com/abalabahaha/eris/4bb65f07114b01bf52e0ad2eac6a494fad45616e
eris: github.com/abalabahaha/eris/53da0d5111cb884bffd2f2c1d0d5674a98558c26
jimp: 0.16.1
murmurhash: 2.0.0
node-fetch: 2.6.1
npmlog: 4.1.2
sharp: 0.28.3
sqlite3: 5.0.2
@ -2258,10 +2256,10 @@ packages:
node-fetch: 2.6.1
dev: false
github.com/abalabahaha/eris/4bb65f07114b01bf52e0ad2eac6a494fad45616e:
resolution: {tarball: https://codeload.github.com/abalabahaha/eris/tar.gz/4bb65f07114b01bf52e0ad2eac6a494fad45616e}
github.com/abalabahaha/eris/53da0d5111cb884bffd2f2c1d0d5674a98558c26:
resolution: {tarball: https://codeload.github.com/abalabahaha/eris/tar.gz/53da0d5111cb884bffd2f2c1d0d5674a98558c26}
name: eris
version: 0.15.2-dev
version: 0.16.0-dev
engines: {node: '>=10.4.0'}
dependencies:
ws: 7.5.3

View file

@ -1,6 +1,5 @@
const Eris = require("eris");
const logger = require("npmlog");
logger.level = "verbose";
const logger = require("./lib/logger.js");
const fs = require("fs");
const {resolve} = require("path");
const sqlite3 = require("sqlite3");
@ -29,9 +28,9 @@ function registerCommand(cmdObj) {
const aliases = cmdObj.getAliases();
logger.info(
"hf:cmd",
"Registered command '%s'%s",
cmdObj.name,
`Registered command '${cmdObj.name}'${
aliases.length > 0 ? ` (aliases: ${aliases.join(", ")})` : ""
}`
);
}
}
@ -51,7 +50,7 @@ const CommandDispatcher = require("./lib/commandDispatcher.js");
for (const file of fs.readdirSync(resolve(__dirname, "modules"))) {
require(resolve(__dirname, "modules", file));
logger.info("hf:modules", "Loaded module: '%s'", file);
logger.info("hf:modules", `Loaded module: '${file}'`);
}
bot.on("messageCreate", async (msg) => {
@ -77,10 +76,7 @@ bot.on("ready", async () => {
logger.info("hf:main", "Connected to Discord.");
logger.info(
"hf:main",
"Logged in as: %s#%s (%s)",
bot.user.username,
bot.user.discriminator,
bot.user.id
`Logged in as: ${bot.user.username}#${bot.user.discriminator} (${bot.user.id})`
);
const channel = await bot.getDMChannel(config.owner_id);
@ -90,26 +86,29 @@ bot.on("ready", async () => {
});
bot.on("error", (err) => {
logger.error("hf:main", "Catching error:", err);
logger.error("hf:main", "Catching error: " + err);
});
bot.on("warn", (err) => {
logger.warn("hf:main", "Catching warn:", err);
logger.warn("hf:main", "Catching warn: " + err);
});
bot.on("shardDisconnect", (err, id) => {
logger.verbose("hf:shard", "Disconnecting from shard %s: %s", id, err);
logger.verbose("hf:shard", `Disconnecting from shard ${id}: ${err}`);
});
bot.on("shardResume", (id) => {
logger.verbose("hf:shard", "Resuming shard %s", id);
logger.verbose("hf:shard", "Resuming on shard " + id);
});
bot.on("shardPreReady", (id) => {
logger.verbose("hf:shard", "Shard %s getting ready", id);
logger.verbose("hf:shard", `Shard ${id} getting ready`);
});
bot.on("shardReady", (id) => {
logger.verbose("hf:shard", "Shard %s ready", id);
logger.verbose("hf:shard", `Shard ${id} ready`);
});
bot.on("unknown", (packet, id) => {
logger.verbose("hf:main", "Shard %s caught unknown packet: %j", id, packet);
logger.verbose(
"hf:main",
`Shard ${id} caught unknown packet: ${JSON.stringify(packet)}`
);
});
bot.connect();

View file

@ -1,4 +1,4 @@
const logger = require("npmlog");
const logger = require("./logger.js");
const {pastelize, getTopColor} = require("./utils.js");
function parseArguments(str) {

View file

@ -1,4 +1,4 @@
const logger = require("npmlog");
const logger = require("./logger.js");
const eventTable = {};

78
src/lib/logger.js Normal file
View file

@ -0,0 +1,78 @@
const ESCAPE = "\x1b";
const NORMAL = "[3";
const BRIGHT = "[9";
const NORMAL_BG = "[4";
const BRIGHT_BG = "[10";
const NAMES = [
"Black",
"Red",
"Green",
"Yellow",
"Blue",
"Magenta",
"Cyan",
"White",
];
const COLORS = {};
for (const index in NAMES) {
COLORS[NAMES[index].toUpperCase()] = index;
}
COLORS.DEFAULT = 9;
function color(name, bright = false, bg = false) {
name = name.toUpperCase();
let out = "" + ESCAPE;
if (bg) {
if (bright && name != "DEFAULT") {
out += BRIGHT_BG;
} else {
out += NORMAL_BG;
}
} else {
if (bright && name != "DEFAULT") {
out += BRIGHT;
} else {
out += NORMAL;
}
}
out += COLORS[name] + "m";
return out;
}
const LEVEL_COLORS = {
silly: ["magenta", true],
verbose: ["green", true],
debug: ["cyan", true],
log: ["blue", false],
info: ["blue", false],
warn: ["yellow", false],
error: ["red", false],
};
function baseLogger(prefix, level, message) {
level = level || "info";
let out = "";
const [col, bright] = LEVEL_COLORS[level];
out += `${color(col, bright)}[${level.toUpperCase()}] `;
out += `${color("magenta")}[${prefix}] ${color("default")}`;
out += message;
console.log(out);
}
module.exports = {};
for (const level in LEVEL_COLORS) {
module.exports[level] = (prefix, message) =>
baseLogger(prefix, level, message);
}

View file

@ -1,4 +1,4 @@
const logger = require("npmlog");
const logger = require("./logger.js");
const timerTable = {};

View file

@ -1,7 +1,7 @@
const Command = require("../lib/command.js");
const CATEGORY = "bot";
const logger = require("npmlog");
const logger = require("../lib/logger.js");
const child_process = require("child_process");
const {inspect} = require("util");
const {resolve} = require("path");
@ -57,7 +57,7 @@ reload.callback = function (msg, line) {
}
try {
logger.info("hf:modules", "Reloading module: '%s'", line);
logger.info("hf:modules", `Reloading module: '${line}'`);
delete require.cache[require.resolve(`./${line}.js`)];
require(`./${line}.js`);
return {reaction: "\uD83D\uDC4C"};