@rivetkit/core
Version:
202 lines (191 loc) • 5.62 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
var _chunkHIB3AS73cjs = require('./chunk-HIB3AS73.cjs');
// src/common/log-levels.ts
var LogLevels = {
TRACE: 0,
DEBUG: 1,
INFO: 2,
WARN: 3,
ERROR: 4,
CRITICAL: 5
};
var LevelNameMap = {
0: "TRACE",
1: "DEBUG",
2: "INFO",
3: "WARN",
4: "ERROR",
5: "CRITICAL"
};
// src/common/logfmt.ts
var LOG_LEVEL_COLORS = {
[LogLevels.CRITICAL]: "\x1B[31m",
// Red
[LogLevels.ERROR]: "\x1B[31m",
// Red
[LogLevels.WARN]: "\x1B[33m",
// Yellow
[LogLevels.INFO]: "\x1B[32m",
// Green
[LogLevels.DEBUG]: "\x1B[36m",
// Cyan
[LogLevels.TRACE]: "\x1B[36m"
// Cyan
};
var RESET_COLOR = "\x1B[0m";
function stringify(...data) {
let line = "";
for (let i = 0; i < data.length; i++) {
const [key, valueRaw] = data[i];
let isNull = false;
let valueString;
if (valueRaw == null) {
isNull = true;
valueString = "";
} else {
valueString = valueRaw.toString();
}
if (valueString.length > 512 && key !== "msg" && key !== "error")
valueString = `${valueString.slice(0, 512)}...`;
const needsQuoting = valueString.indexOf(" ") > -1 || valueString.indexOf("=") > -1;
const needsEscaping = valueString.indexOf('"') > -1 || valueString.indexOf("\\") > -1;
valueString = valueString.replace(/\n/g, "\\n");
if (needsEscaping) valueString = valueString.replace(/["\\]/g, "\\$&");
if (needsQuoting || needsEscaping) valueString = `"${valueString}"`;
if (valueString === "" && !isNull) valueString = '""';
if (LOGGER_CONFIG.enableColor) {
let color = "\x1B[2m";
if (key === "level") {
const level = LogLevels[valueString];
const levelColor = LOG_LEVEL_COLORS[level];
if (levelColor) {
color = levelColor;
}
} else if (key === "msg") {
color = "\x1B[32m";
} else if (key === "trace") {
color = "\x1B[34m";
}
line += `\x1B[0m\x1B[1m${key}\x1B[0m\x1B[2m=\x1B[0m${color}${valueString}${RESET_COLOR}`;
} else {
line += `${key}=${valueString}`;
}
if (i !== data.length - 1) {
line += " ";
}
}
return line;
}
function formatTimestamp(date) {
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
const day = String(date.getUTCDate()).padStart(2, "0");
const hours = String(date.getUTCHours()).padStart(2, "0");
const minutes = String(date.getUTCMinutes()).padStart(2, "0");
const seconds = String(date.getUTCSeconds()).padStart(2, "0");
const milliseconds = String(date.getUTCMilliseconds()).padStart(3, "0");
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}Z`;
}
function castToLogValue(v) {
if (typeof v === "string" || typeof v === "number" || typeof v === "boolean" || v === null || v === void 0) {
return v;
}
if (v instanceof Error) {
return String(v);
}
try {
return JSON.stringify(v);
} catch (e) {
return "[cannot stringify]";
}
}
var LOGGER_CONFIG = {
enableColor: false,
enableSpreadObject: false,
enableErrorStack: false
};
// src/common/log.ts
var Logger = class {
constructor(name, level) {
this.name = name;
this.level = level;
}
log(level, message, ...args) {
const record = {
msg: message,
args,
level,
loggerName: this.name,
datetime: /* @__PURE__ */ new Date(),
levelName: LevelNameMap[level]
};
if (this.#shouldLog(level)) {
this.#logRecord(record);
}
}
#shouldLog(level) {
return level >= LogLevels[this.level];
}
#logRecord(record) {
console.log(formatter(record));
}
trace(message, ...args) {
this.log(LogLevels.TRACE, message, ...args);
}
debug(message, ...args) {
this.log(LogLevels.DEBUG, message, ...args);
}
info(message, ...args) {
this.log(LogLevels.INFO, message, ...args);
}
warn(message, ...args) {
this.log(LogLevels.WARN, message, ...args);
}
error(message, ...args) {
this.log(LogLevels.ERROR, message, ...args);
}
critical(message, ...args) {
this.log(LogLevels.CRITICAL, message, ...args);
}
};
var loggers = {};
function getLogger(name = "default") {
const defaultLogLevelEnv = _chunkHIB3AS73cjs.getEnvUniversal.call(void 0,
"_LOG_LEVEL"
);
const defaultLogLevel = _nullishCoalesce(defaultLogLevelEnv, () => ( "INFO"));
if (!loggers[name]) {
loggers[name] = new Logger(name, defaultLogLevel);
}
return loggers[name];
}
function formatter(log) {
const args = [];
for (let i = 0; i < log.args.length; i++) {
const logArg = log.args[i];
if (logArg && typeof logArg === "object") {
for (const k in logArg) {
const v = logArg[k];
pushArg(k, v, args);
}
} else {
pushArg(`arg${i}`, logArg, args);
}
}
const logTs = _chunkHIB3AS73cjs.getEnvUniversal.call(void 0, "_LOG_TIMESTAMP") === "1";
const logTarget = _chunkHIB3AS73cjs.getEnvUniversal.call(void 0, "_LOG_TARGET") === "1";
return stringify(
...logTs ? [["ts", formatTimestamp(/* @__PURE__ */ new Date())]] : [],
["level", LevelNameMap[log.level]],
...logTarget ? [["target", log.loggerName]] : [],
["msg", log.msg],
...args
);
}
function pushArg(k, v, args) {
args.push([k, castToLogValue(v)]);
}
function setupLogging() {
}
exports.Logger = Logger; exports.getLogger = getLogger; exports.setupLogging = setupLogging;
//# sourceMappingURL=chunk-4KRNEW7D.cjs.map