@logtail/edge
Version:
Better Stack Edge runtime logger (formerly Logtail)
86 lines • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Edge = void 0;
const msgpack_1 = require("@msgpack/msgpack");
const types_1 = require("@logtail/types");
const core_1 = require("@logtail/core");
const context_1 = require("./context");
const edgeWithExecutionContext_1 = require("./edgeWithExecutionContext");
class Edge extends core_1.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_1.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 = (0, context_1.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, types_1.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, types_1.LogLevel.Debug, context, ctx);
}
async info(message, context = {}, ctx) {
return this.log(message, types_1.LogLevel.Info, context, ctx);
}
async warn(message, context = {}, ctx) {
return this.log(message, types_1.LogLevel.Warn, context, ctx);
}
async error(message, context = {}, ctx) {
return this.log(message, types_1.LogLevel.Error, context, ctx);
}
encodeAsMsgpack(logs) {
const encoded = (0, msgpack_1.encode)(logs);
return new Uint8Array(encoded.buffer, encoded.byteOffset, encoded.byteLength);
}
}
exports.Edge = Edge;
//# sourceMappingURL=edge.js.map