trigger.dev
Version:
A Command-Line Interface for Trigger.dev projects
113 lines • 4.68 kB
JavaScript
// This is a copy of the logger utility from the wrangler repo: https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/logger.ts
import { format } from "node:util";
import chalk from "chalk";
import CLITable from "cli-table3";
import { formatMessagesSync } from "esbuild";
import { env } from "std-env";
const LOGGER_LEVELS = {
none: -1,
error: 0,
warn: 1,
info: 2,
log: 3,
debug: 4,
};
/** A map from LOGGER_LEVEL to the error `kind` needed by `formatMessagesSync()`. */
const LOGGER_LEVEL_FORMAT_TYPE_MAP = {
error: "error",
warn: "warning",
info: undefined,
log: undefined,
debug: undefined,
};
function getLoggerLevel() {
const fromEnv = env.TRIGGER_LOG_LEVEL?.toLowerCase();
if (fromEnv !== undefined) {
if (fromEnv in LOGGER_LEVELS)
return fromEnv;
const expected = Object.keys(LOGGER_LEVELS)
.map((level) => `"${level}"`)
.join(" | ");
console.warn(`Unrecognised TRIGGER_LOG_LEVEL value ${JSON.stringify(fromEnv)}, expected ${expected}, defaulting to "log"...`);
}
return "log";
}
class Logger {
constructor() { }
loggerLevel = getLoggerLevel();
columns = process.stdout.columns;
debug = (...args) => this.doLog("debug", args);
ignore = (...args) => { };
debugWithSanitization = (label, ...args) => {
this.doLog("debug", [label, ...args]);
};
info = (...args) => this.doLog("info", args);
log = (...args) => this.doLog("log", args);
/** @deprecated **ONLY USE THIS IN THE CLI** - It will hang the process when used in deployed code (!) */
warn = (...args) => this.doLog("warn", args);
/** @deprecated **ONLY USE THIS IN THE CLI** - It will hang the process when used in deployed code (!) */
error = (...args) => this.doLog("error", args);
/** An error printed in line with the CLI's step output: a bar line above it and a bar before each line under it. */
commandError = (...args) => {
if (LOGGER_LEVELS[this.loggerLevel] < LOGGER_LEVELS.error)
return;
const formatted = this.formatMessage("error", format(...args), this.columns ? this.columns - 1 : undefined);
console.error([chalk.gray("│"), ...withStepBars(formatted)].join("\n"));
};
table(data, level) {
const keys = data.length === 0 ? [] : Object.keys(data[0]);
const t = new CLITable({
head: keys,
style: {
head: chalk.level ? ["blue"] : [],
border: chalk.level ? ["gray"] : [],
},
});
t.push(...data.map((row) => keys.map((k) => row[k])));
return this.doLog(level ?? "log", [t.toString()]);
}
doLog(messageLevel, args) {
const message = this.formatMessage(messageLevel, format(...args));
// only send logs to the terminal if their level is at least the configured log-level
if (LOGGER_LEVELS[this.loggerLevel] >= LOGGER_LEVELS[messageLevel]) {
console[messageLevel](message);
}
}
formatMessage(level, message, terminalWidth = this.columns) {
const kind = LOGGER_LEVEL_FORMAT_TYPE_MAP[level];
if (kind) {
// Format the message using the esbuild formatter.
// The first line of the message is the main `text`,
// subsequent lines are put into the `notes`.
const [firstLine, ...otherLines] = message.split("\n");
const notes = otherLines.length > 0 ? otherLines.map((text) => ({ text })) : undefined;
const formatted = formatMessagesSync([{ text: firstLine, notes }], {
color: chalk.level > 0,
kind,
terminalWidth,
})[0];
return addIconGap(formatted);
}
else {
return message;
}
}
}
/**
* A drop-in replacement for `console` for outputting logging messages.
*
* Errors and Warnings will get additional formatting to highlight them to the user.
* You can also set a `logger.loggerLevel` value to one of "debug", "log", "warn" or "error",
* to filter out logging messages.
*/
export const logger = new Logger();
/** Puts a grey bar before every line after the first, so the message lines up with the CLI's steps. */
function withStepBars(formatted) {
const [firstLine = "", ...rest] = formatted.trimEnd().split("\n");
return [firstLine, ...rest.map((line) => `${chalk.gray("│")}${line}`)];
}
/** Adds a space between the error or warning icon and its label. */
function addIconGap(formatted) {
return formatted.replace(/^(\S*[✘▲]) /, "$1 ");
}
//# sourceMappingURL=logger.js.map