Colorize logs

backend-refactor
Sven Slootweg 4 years ago
parent 96b3e5b9e3
commit b37db99618

@ -12,6 +12,7 @@ const pipe = require("@promistream/pipe");
const parallelize = require("@ppstreams/parallelize");
const initialize = require("./initialize");
const logStatus = require("./log-status");
// TODO: Publish this as a separate package
// Inverts an object of arrays, eg. {a: [x, y], b: [x, z]} becomes {x: [a, b], y: [a], z: [b]}
@ -107,7 +108,7 @@ module.exports = function createKernel(configuration) {
return pipe([
taskStream,
simpleSink((completedItem) => {
console.log(`[completed] ${completedItem.id}`);
logStatus(task, chalk.bold.green, "completed", completedItem.id);
})
]).read();
} else {

@ -0,0 +1,7 @@
"use strict";
const chalk = require("chalk");
module.exports = function logStatus(task, color, type, message) {
console.log(`${chalk.bold(`[${task}]`)} ${color(`[${type}]`)} ${message}`);
};

@ -1,6 +1,8 @@
"use strict";
const defaultValue = require("default-value");
const chalk = require("chalk");
const logStatus = require("../log-status");
module.exports = function (state) {
const queries = require("../queries")(state);
@ -8,7 +10,7 @@ module.exports = function (state) {
return function createDatabaseMutationAPI({ tx, item, taskVersion, task }) {
return {
createItem: function (options) {
console.log(`[${task}][new] ${options.id}`);
logStatus(task, chalk.gray, "new", options.id);
return queries.createItem(tx, {
...options,

@ -11,6 +11,8 @@ const buffer = require("@promistream/buffer");
const pipe = require("@promistream/pipe");
const createMutationAPIWrapper = require("./mutation-api/wrapper");
const logStatus = require("./log-status");
const chalk = require("chalk");
// FIXME: Revert inlining of task_states once switched to PostgreSQL 12+, which can do this automatically using NOT MATERIALIZED
let query = `
@ -128,7 +130,7 @@ module.exports = function (state) {
buffer(),
globalRateLimiter,
processTaskSafely(task, (item, tx) => {
console.log(`[${task}][started] ${item.id}`);
logStatus(task, chalk.bold.cyan, "started", item.id);
let context = { tx, item, task, taskVersion };
@ -171,7 +173,7 @@ module.exports = function (state) {
expires_at: dateFns.add(new Date(), { seconds: ttlInSeconds })
});
}).catch((error) => {
console.warn(`[${task}][failed] ${item.id}`, error);
logStatus(task, chalk.bold.red, "failed", item.id);
return Promise.try(() => {
// Task failed -- note, cannot use tx here because it has failed

Loading…
Cancel
Save