astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
134 lines (133 loc) • 3.26 kB
JavaScript
import { blue, bold, dim, red, yellow } from "kleur/colors";
const dateTimeFormat = new Intl.DateTimeFormat([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false
});
const levels = {
debug: 20,
info: 30,
warn: 40,
error: 50,
silent: 90
};
function log(opts, level, label, message, newLine = true) {
const logLevel = opts.level;
const dest = opts.dest;
const event = {
label,
level,
message,
newLine
};
if (!isLogLevelEnabled(logLevel, level)) {
return;
}
dest.write(event);
}
function isLogLevelEnabled(configuredLogLevel, level) {
return levels[configuredLogLevel] <= levels[level];
}
function info(opts, label, message, newLine = true) {
return log(opts, "info", label, message, newLine);
}
function warn(opts, label, message, newLine = true) {
return log(opts, "warn", label, message, newLine);
}
function error(opts, label, message, newLine = true) {
return log(opts, "error", label, message, newLine);
}
function debug(...args) {
if ("_astroGlobalDebug" in globalThis) {
globalThis._astroGlobalDebug(...args);
}
}
function getEventPrefix({ level, label }) {
const timestamp = `${dateTimeFormat.format(/* @__PURE__ */ new Date())}`;
const prefix = [];
if (level === "error" || level === "warn") {
prefix.push(bold(timestamp));
prefix.push(`[${level.toUpperCase()}]`);
} else {
prefix.push(timestamp);
}
if (label) {
prefix.push(`[${label}]`);
}
if (level === "error") {
return red(prefix.join(" "));
}
if (level === "warn") {
return yellow(prefix.join(" "));
}
if (prefix.length === 1) {
return dim(prefix[0]);
}
return dim(prefix[0]) + " " + blue(prefix.splice(1).join(" "));
}
function timerMessage(message, startTime = Date.now()) {
let timeDiff = Date.now() - startTime;
let timeDisplay = timeDiff < 750 ? `${Math.round(timeDiff)}ms` : `${(timeDiff / 1e3).toFixed(1)}s`;
return `${message} ${dim(timeDisplay)}`;
}
class Logger {
options;
constructor(options) {
this.options = options;
}
info(label, message, newLine = true) {
info(this.options, label, message, newLine);
}
warn(label, message, newLine = true) {
warn(this.options, label, message, newLine);
}
error(label, message, newLine = true) {
error(this.options, label, message, newLine);
}
debug(label, ...messages) {
debug(label, ...messages);
}
level() {
return this.options.level;
}
forkIntegrationLogger(label) {
return new AstroIntegrationLogger(this.options, label);
}
}
class AstroIntegrationLogger {
options;
label;
constructor(logging, label) {
this.options = logging;
this.label = label;
}
/**
* Creates a new logger instance with a new label, but the same log options.
*/
fork(label) {
return new AstroIntegrationLogger(this.options, label);
}
info(message) {
info(this.options, this.label, message);
}
warn(message) {
warn(this.options, this.label, message);
}
error(message) {
error(this.options, this.label, message);
}
debug(message) {
debug(this.label, message);
}
}
export {
AstroIntegrationLogger,
Logger,
dateTimeFormat,
debug,
getEventPrefix,
isLogLevelEnabled,
levels,
timerMessage
};