mongoku
Version:
[](https://github.com/huggingface/Mongoku/actions/workflows/ci.yml)
152 lines (149 loc) • 4.02 kB
JavaScript
import { p as private_env } from './shared-server-BmU87nph.js';
import { AsyncLocalStorage } from 'async_hooks';
import { inspect } from 'node:util';
const contextStore = new AsyncLocalStorage();
class Logger {
structuredLogging;
logHeaders;
constructor() {
this.structuredLogging = private_env.MONGOKU_STRUCTURED_LOG === "true";
this.logHeaders = private_env.MONGOKU_LOG_HEADERS ? private_env.MONGOKU_LOG_HEADERS.split(",").map((h) => h.trim().toLowerCase()).filter(Boolean) : [];
}
/**
* Get request context information from the current request
*/
getRequestContext() {
const event = contextStore.getStore();
if (!event) {
return void 0;
}
const url = new URL(event.request.url);
const context = {
requestId: event.locals.requestId,
method: event.request.method,
url: url.pathname + url.search,
ip: event.getClientAddress(),
userAgent: event.request.headers.get("user-agent") || void 0
};
if (this.logHeaders.length > 0) {
const headers = {};
for (const headerName of this.logHeaders) {
const value = event.request.headers.get(headerName);
if (value) {
headers[headerName] = value;
}
}
if (Object.keys(headers).length > 0) {
context.headers = headers;
}
}
return context;
}
/**
* Format arguments for logging
*/
formatArgs(args) {
return args.map((arg) => {
if (arg instanceof Error) {
return (arg.stack ?? `${arg.name}: ${arg.message}`) + (arg.cause instanceof Error ? `
cause: ${arg.cause.stack ?? `${arg.cause.name}: ${arg.cause.message}`}` : "");
} else if (typeof arg === "string") {
return arg;
} else {
return inspect(arg, { depth: 20, colors: !this.structuredLogging });
}
}).join(" ");
}
/**
* Internal log method that handles both structured and unstructured logging
*/
_log(level, args) {
const message = this.formatArgs(args);
if (this.structuredLogging) {
const logEntry = {
level,
time: (/* @__PURE__ */ new Date()).toISOString(),
message,
...this.getRequestContext()
};
console.log(JSON.stringify(logEntry));
} else {
switch (level) {
case "error":
console.error(message);
break;
case "warn":
console.warn(message);
break;
case "debug":
console.debug(message);
break;
default:
console.log(message);
}
}
}
/**
* Log an informational message
*/
log(...args) {
this._log("info", args);
}
/**
* Log an informational message (alias for log)
*/
info(...args) {
this._log("info", args);
}
/**
* Log an error message
*/
error(...args) {
this._log("error", args);
}
/**
* Log a warning message
*/
warn(...args) {
this._log("warn", args);
}
/**
* Log a debug message
*/
debug(...args) {
this._log("debug", args);
}
/**
* Log an HTTP request
*/
logRequest(statusCode, durationMs) {
const duration = Math.round(durationMs * 100) / 100;
if (this.structuredLogging) {
const logEntry = {
level: "info",
time: (/* @__PURE__ */ new Date()).toISOString(),
type: "request",
statusCode,
duration,
...this.getRequestContext()
};
console.log(JSON.stringify(logEntry));
} else {
const event = contextStore.getStore();
if (event) {
const url = new URL(event.request.url);
const urlPath = url.pathname + url.search;
const method = event.request.method;
const message = `${statusCode} ${method} ${urlPath} ${duration}ms`;
if (statusCode >= 400) {
console.error(message);
} else {
console.log(message);
}
}
}
}
}
const logger = new Logger();
export { contextStore as c, logger as l };
//# sourceMappingURL=logger-PfH_grbh.js.map