UNPKG

@logtail/edge

Version:

Better Stack Edge runtime logger (formerly Logtail)

82 lines 3.8 kB
import { encode } from "@msgpack/msgpack"; import { LogLevel } from "@logtail/types"; import { Base } from "@logtail/core"; import { getStackContext } from "./context"; import { EdgeWithExecutionContext } from "./edgeWithExecutionContext"; export class Edge extends Base { constructor(sourceToken, options) { var _a; super(sourceToken, options); this._warnedAboutMissingCtx = false; this.warnAboutMissingExecutionContext = (_a = options === null || options === void 0 ? void 0 : options.warnAboutMissingExecutionContext) !== null && _a !== void 0 ? _a : true; // Sync function const sync = async (logs) => { // Compress the data using CompressionStream const compressedData = await new Response(new Blob([this.encodeAsMsgpack(logs)]).stream().pipeThrough(new CompressionStream("gzip"))).arrayBuffer(); const res = await fetch(this._options.endpoint, { method: "POST", headers: { "Content-Type": "application/msgpack", "Content-Encoding": "gzip", Authorization: `Bearer ${this._sourceToken}`, "User-Agent": "logtail-js(edge)", }, body: compressedData, }); if (res.ok) { return logs; } throw new Error(res.statusText); }; // Set the throttled sync function this.setSync(sync); } withExecutionContext(ctx) { return new EdgeWithExecutionContext(this, ctx); } /** * @param message (string | Error) - Log message * @param level (ILogLevel) - Level to log at (debug|info|warn|error) * @param context (Context | Error | any) - Log context for passing structured data * @param ctx (ExecutionContext) - Execution context of particular worker request * @returns Promise<ILogtailLog> after syncing */ async log(message, level, context = {}, ctx) { // Process/sync the log, per `Base` logic const stackContext = getStackContext(this); context = Object.assign(Object.assign({}, stackContext), context); const log = super.log(message, level, context); if (ctx) { ctx.waitUntil(log); } else if (this.warnAboutMissingExecutionContext && !this._warnedAboutMissingCtx) { this._warnedAboutMissingCtx = true; const warningMessage = "ExecutionContext hasn't been passed to the `log` method, which means syncing logs cannot be guaranteed. " + "To ensure your logs will reach Better Stack, use `logger.withExecutionContext(ctx)` to log in your handler function. " + "See https://betterstack.com/docs/logs/js-edge-execution-context/ for details."; console.warn(warningMessage); this.log(warningMessage, LogLevel.Warn, stackContext).catch(() => { }); // Flush immediately to ensure warning will get sent to Better Stack await this.flush(); } // Return the transformed log return (await log); } async debug(message, context = {}, ctx) { return this.log(message, LogLevel.Debug, context, ctx); } async info(message, context = {}, ctx) { return this.log(message, LogLevel.Info, context, ctx); } async warn(message, context = {}, ctx) { return this.log(message, LogLevel.Warn, context, ctx); } async error(message, context = {}, ctx) { return this.log(message, LogLevel.Error, context, ctx); } encodeAsMsgpack(logs) { const encoded = encode(logs); return new Uint8Array(encoded.buffer, encoded.byteOffset, encoded.byteLength); } } //# sourceMappingURL=edge.js.map