logquill 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +216 -0
- package/dist/index.cjs +1074 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +772 -2
- package/dist/index.d.ts +772 -2
- package/dist/index.mjs +1056 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +53 -1
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var zlib = require('zlib');
|
|
3
4
|
var fs = require('fs');
|
|
4
5
|
var path = require('path');
|
|
5
6
|
|
|
6
|
-
// src/levels.ts
|
|
7
|
+
// src/core/levels.ts
|
|
7
8
|
var Level = /* @__PURE__ */ ((Level2) => {
|
|
8
9
|
Level2[Level2["TRACE"] = 5] = "TRACE";
|
|
9
10
|
Level2[Level2["DEBUG"] = 10] = "DEBUG";
|
|
@@ -38,7 +39,7 @@ function parseLevel(level) {
|
|
|
38
39
|
return level;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
// src/records.ts
|
|
42
|
+
// src/core/records.ts
|
|
42
43
|
function utcTimestamp() {
|
|
43
44
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
44
45
|
}
|
|
@@ -52,14 +53,14 @@ function createRecord(params) {
|
|
|
52
53
|
};
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
// src/formatter.ts
|
|
56
|
+
// src/core/formatter.ts
|
|
56
57
|
var JSONFormatter = class {
|
|
57
58
|
format(record) {
|
|
58
59
|
return JSON.stringify(record);
|
|
59
60
|
}
|
|
60
61
|
};
|
|
61
62
|
|
|
62
|
-
// src/context-plugin.ts
|
|
63
|
+
// src/plugins/context-plugin.ts
|
|
63
64
|
var ContextPlugin = class {
|
|
64
65
|
context;
|
|
65
66
|
constructor(context) {
|
|
@@ -70,7 +71,7 @@ var ContextPlugin = class {
|
|
|
70
71
|
}
|
|
71
72
|
};
|
|
72
73
|
|
|
73
|
-
// src/redact-plugin.ts
|
|
74
|
+
// src/plugins/redact-plugin.ts
|
|
74
75
|
var DEFAULT_REDACTED_KEYS = ["password", "token", "secret", "api_key", "authorization"];
|
|
75
76
|
var RedactPlugin = class {
|
|
76
77
|
keys;
|
|
@@ -88,7 +89,7 @@ var RedactPlugin = class {
|
|
|
88
89
|
}
|
|
89
90
|
};
|
|
90
91
|
|
|
91
|
-
// src/sampling-plugin.ts
|
|
92
|
+
// src/plugins/sampling-plugin.ts
|
|
92
93
|
var SamplingPlugin = class {
|
|
93
94
|
rate;
|
|
94
95
|
rng;
|
|
@@ -104,7 +105,7 @@ var SamplingPlugin = class {
|
|
|
104
105
|
}
|
|
105
106
|
};
|
|
106
107
|
|
|
107
|
-
// src/transport.ts
|
|
108
|
+
// src/transports/transport.ts
|
|
108
109
|
var Transport = class {
|
|
109
110
|
formatter;
|
|
110
111
|
constructor(formatter = new JSONFormatter()) {
|
|
@@ -130,7 +131,1050 @@ var CollectingTransport = class extends Transport {
|
|
|
130
131
|
}
|
|
131
132
|
};
|
|
132
133
|
|
|
133
|
-
// src/
|
|
134
|
+
// src/transports/batching-transport.ts
|
|
135
|
+
var BatchingTransport = class extends Transport {
|
|
136
|
+
maxRecords;
|
|
137
|
+
maxBytes;
|
|
138
|
+
buffer = [];
|
|
139
|
+
bufferBytes = 0;
|
|
140
|
+
constructor(options = {}) {
|
|
141
|
+
super(options.formatter);
|
|
142
|
+
this.maxRecords = options.maxRecords ?? 100;
|
|
143
|
+
this.maxBytes = options.maxBytes ?? 1e6;
|
|
144
|
+
}
|
|
145
|
+
/** Converts a written record into the buffered item type. Defaults to the record itself. */
|
|
146
|
+
toItem(formatted, record) {
|
|
147
|
+
return record;
|
|
148
|
+
}
|
|
149
|
+
/** Estimated byte size of one buffered item, used for the `maxBytes` bound. */
|
|
150
|
+
sizeOf(item) {
|
|
151
|
+
return JSON.stringify(item).length;
|
|
152
|
+
}
|
|
153
|
+
write(formatted, record) {
|
|
154
|
+
const item = this.toItem(formatted, record);
|
|
155
|
+
this.buffer.push(item);
|
|
156
|
+
this.bufferBytes += this.sizeOf(item);
|
|
157
|
+
if (this.buffer.length >= this.maxRecords || this.bufferBytes >= this.maxBytes) {
|
|
158
|
+
this.flush();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Send the current batch now, even if it hasn't reached a bound. */
|
|
162
|
+
flush() {
|
|
163
|
+
if (this.buffer.length === 0) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const batch = this.buffer;
|
|
167
|
+
this.buffer = [];
|
|
168
|
+
this.bufferBytes = 0;
|
|
169
|
+
const result = this.sendBatch(batch);
|
|
170
|
+
if (result) {
|
|
171
|
+
result.catch((error) => {
|
|
172
|
+
console.error(`${this.constructor.name}: failed to send log batch`, error);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
close() {
|
|
177
|
+
this.flush();
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// src/transports/sql/base-sql-transport.ts
|
|
182
|
+
function metaString(meta, key) {
|
|
183
|
+
const value = meta[key];
|
|
184
|
+
return typeof value === "string" ? value : null;
|
|
185
|
+
}
|
|
186
|
+
var BaseSQLTransport = class extends BatchingTransport {
|
|
187
|
+
tableName;
|
|
188
|
+
ensureSchema;
|
|
189
|
+
schemaEnsured = false;
|
|
190
|
+
constructor(options = {}) {
|
|
191
|
+
super(options);
|
|
192
|
+
this.tableName = options.tableName ?? "logs";
|
|
193
|
+
this.ensureSchema = options.ensureSchema ?? false;
|
|
194
|
+
}
|
|
195
|
+
toItem(_formatted, record) {
|
|
196
|
+
return {
|
|
197
|
+
timestamp: record.timestamp,
|
|
198
|
+
level: record.level,
|
|
199
|
+
logger: record.logger,
|
|
200
|
+
message: record.message,
|
|
201
|
+
meta: JSON.stringify(record.meta),
|
|
202
|
+
runId: metaString(record.meta, "runId"),
|
|
203
|
+
spanId: metaString(record.meta, "spanId"),
|
|
204
|
+
parentSpanId: metaString(record.meta, "parentSpanId"),
|
|
205
|
+
traceId: metaString(record.meta, "traceId")
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
sizeOf(row) {
|
|
209
|
+
return row.message.length + row.meta.length + 96;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Minimal, dialect-generic `CREATE TABLE IF NOT EXISTS` for dev/test use via
|
|
213
|
+
* `ensureSchema: true`. Production deployments should manage this table with
|
|
214
|
+
* a real migration instead — override in a subclass for dialect-correct
|
|
215
|
+
* column types (e.g. `JSONB` on Postgres).
|
|
216
|
+
*/
|
|
217
|
+
createTableSQL() {
|
|
218
|
+
return `CREATE TABLE IF NOT EXISTS ${this.tableName} (
|
|
219
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
220
|
+
timestamp TEXT NOT NULL,
|
|
221
|
+
level TEXT NOT NULL,
|
|
222
|
+
logger TEXT NOT NULL,
|
|
223
|
+
message TEXT NOT NULL,
|
|
224
|
+
meta TEXT NOT NULL,
|
|
225
|
+
runId TEXT,
|
|
226
|
+
spanId TEXT,
|
|
227
|
+
parentSpanId TEXT,
|
|
228
|
+
traceId TEXT
|
|
229
|
+
)`;
|
|
230
|
+
}
|
|
231
|
+
async sendBatch(rows) {
|
|
232
|
+
if (this.ensureSchema && !this.schemaEnsured) {
|
|
233
|
+
this.schemaEnsured = true;
|
|
234
|
+
await this.ensureTable();
|
|
235
|
+
}
|
|
236
|
+
await this.insertRows(rows);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
// src/transports/sql/sqlite-transport.ts
|
|
241
|
+
var SQLiteTransport = class extends BaseSQLTransport {
|
|
242
|
+
injectedClient;
|
|
243
|
+
filename;
|
|
244
|
+
client;
|
|
245
|
+
constructor(options = {}) {
|
|
246
|
+
super(options);
|
|
247
|
+
this.injectedClient = options.client;
|
|
248
|
+
this.filename = options.filename ?? ":memory:";
|
|
249
|
+
}
|
|
250
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
251
|
+
resolvedClient() {
|
|
252
|
+
return this.injectedClient ?? this.client;
|
|
253
|
+
}
|
|
254
|
+
async importClient() {
|
|
255
|
+
let DatabaseCtor;
|
|
256
|
+
try {
|
|
257
|
+
const moduleName = "better-sqlite3";
|
|
258
|
+
const mod = await import(moduleName);
|
|
259
|
+
DatabaseCtor = mod.default;
|
|
260
|
+
} catch {
|
|
261
|
+
throw new Error(
|
|
262
|
+
"SQLiteTransport: install `better-sqlite3` to use this transport without providing a client \u2014 `npm install better-sqlite3`"
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
this.client = new DatabaseCtor(this.filename);
|
|
266
|
+
return this.client;
|
|
267
|
+
}
|
|
268
|
+
async ensureTable() {
|
|
269
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
270
|
+
client.exec(this.createTableSQL());
|
|
271
|
+
}
|
|
272
|
+
async insertRows(rows) {
|
|
273
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
274
|
+
const stmt = client.prepare(
|
|
275
|
+
`INSERT INTO ${this.tableName} (timestamp, level, logger, message, meta, runId, spanId, parentSpanId, traceId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
276
|
+
);
|
|
277
|
+
const insertMany = client.transaction((batch) => {
|
|
278
|
+
for (const row of batch) {
|
|
279
|
+
stmt.run(
|
|
280
|
+
row.timestamp,
|
|
281
|
+
row.level,
|
|
282
|
+
row.logger,
|
|
283
|
+
row.message,
|
|
284
|
+
row.meta,
|
|
285
|
+
row.runId,
|
|
286
|
+
row.spanId,
|
|
287
|
+
row.parentSpanId,
|
|
288
|
+
row.traceId
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
insertMany(rows);
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// src/transports/sql/postgres-transport.ts
|
|
297
|
+
var PostgresTransport = class extends BaseSQLTransport {
|
|
298
|
+
injectedClient;
|
|
299
|
+
connectionString;
|
|
300
|
+
connectionConfig;
|
|
301
|
+
client;
|
|
302
|
+
constructor(options = {}) {
|
|
303
|
+
super(options);
|
|
304
|
+
this.injectedClient = options.client;
|
|
305
|
+
this.connectionString = options.connectionString;
|
|
306
|
+
this.connectionConfig = options.connectionConfig;
|
|
307
|
+
}
|
|
308
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
309
|
+
resolvedClient() {
|
|
310
|
+
return this.injectedClient ?? this.client;
|
|
311
|
+
}
|
|
312
|
+
async importClient() {
|
|
313
|
+
let PoolCtor;
|
|
314
|
+
try {
|
|
315
|
+
const moduleName = "pg";
|
|
316
|
+
const mod = await import(moduleName);
|
|
317
|
+
const resolved = mod.default?.Pool ?? mod.Pool;
|
|
318
|
+
if (!resolved) {
|
|
319
|
+
throw new Error("no Pool export found");
|
|
320
|
+
}
|
|
321
|
+
PoolCtor = resolved;
|
|
322
|
+
} catch {
|
|
323
|
+
throw new Error(
|
|
324
|
+
"PostgresTransport: install `pg` to use this transport without providing a client \u2014 `npm install pg`"
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const config = this.connectionString ? { connectionString: this.connectionString } : this.connectionConfig ?? {};
|
|
328
|
+
this.client = new PoolCtor(config);
|
|
329
|
+
return this.client;
|
|
330
|
+
}
|
|
331
|
+
/** Postgres-correct `CREATE TABLE IF NOT EXISTS`: `SERIAL` primary key, `JSONB` for `meta`. */
|
|
332
|
+
createTableSQL() {
|
|
333
|
+
return `CREATE TABLE IF NOT EXISTS ${this.tableName} (
|
|
334
|
+
id SERIAL PRIMARY KEY,
|
|
335
|
+
timestamp TEXT NOT NULL,
|
|
336
|
+
level TEXT NOT NULL,
|
|
337
|
+
logger TEXT NOT NULL,
|
|
338
|
+
message TEXT NOT NULL,
|
|
339
|
+
meta JSONB NOT NULL,
|
|
340
|
+
"runId" TEXT,
|
|
341
|
+
"spanId" TEXT,
|
|
342
|
+
"parentSpanId" TEXT,
|
|
343
|
+
"traceId" TEXT
|
|
344
|
+
)`;
|
|
345
|
+
}
|
|
346
|
+
async ensureTable() {
|
|
347
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
348
|
+
await client.query(this.createTableSQL(), []);
|
|
349
|
+
}
|
|
350
|
+
async insertRows(rows) {
|
|
351
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
352
|
+
const columns = 9;
|
|
353
|
+
const values = [];
|
|
354
|
+
const placeholders = [];
|
|
355
|
+
rows.forEach((row, rowIndex) => {
|
|
356
|
+
const base = rowIndex * columns;
|
|
357
|
+
placeholders.push(
|
|
358
|
+
`(${Array.from({ length: columns }, (_, col) => `$${String(base + col + 1)}`).join(", ")})`
|
|
359
|
+
);
|
|
360
|
+
values.push(
|
|
361
|
+
row.timestamp,
|
|
362
|
+
row.level,
|
|
363
|
+
row.logger,
|
|
364
|
+
row.message,
|
|
365
|
+
row.meta,
|
|
366
|
+
row.runId,
|
|
367
|
+
row.spanId,
|
|
368
|
+
row.parentSpanId,
|
|
369
|
+
row.traceId
|
|
370
|
+
);
|
|
371
|
+
});
|
|
372
|
+
const sql = `INSERT INTO ${this.tableName} (timestamp, level, logger, message, meta, "runId", "spanId", "parentSpanId", "traceId") VALUES ${placeholders.join(", ")}`;
|
|
373
|
+
await client.query(sql, values);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
// src/transports/sql/mysql-transport.ts
|
|
378
|
+
var MySQLTransport = class extends BaseSQLTransport {
|
|
379
|
+
injectedClient;
|
|
380
|
+
connectionString;
|
|
381
|
+
connectionConfig;
|
|
382
|
+
client;
|
|
383
|
+
constructor(options = {}) {
|
|
384
|
+
super(options);
|
|
385
|
+
this.injectedClient = options.client;
|
|
386
|
+
this.connectionString = options.connectionString;
|
|
387
|
+
this.connectionConfig = options.connectionConfig;
|
|
388
|
+
}
|
|
389
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
390
|
+
resolvedClient() {
|
|
391
|
+
return this.injectedClient ?? this.client;
|
|
392
|
+
}
|
|
393
|
+
async importClient() {
|
|
394
|
+
let createPool;
|
|
395
|
+
try {
|
|
396
|
+
const moduleName = "mysql2/promise";
|
|
397
|
+
const mod = await import(moduleName);
|
|
398
|
+
const resolved = mod.default?.createPool ?? mod.createPool;
|
|
399
|
+
if (!resolved) {
|
|
400
|
+
throw new Error("no createPool export found");
|
|
401
|
+
}
|
|
402
|
+
createPool = resolved;
|
|
403
|
+
} catch {
|
|
404
|
+
throw new Error(
|
|
405
|
+
"MySQLTransport: install `mysql2` to use this transport without providing a client \u2014 `npm install mysql2`"
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
const target = this.connectionString ?? this.connectionConfig ?? {};
|
|
409
|
+
this.client = createPool(target);
|
|
410
|
+
return this.client;
|
|
411
|
+
}
|
|
412
|
+
/** MySQL-correct `CREATE TABLE IF NOT EXISTS`: `AUTO_INCREMENT` primary key, `JSON` column type for `meta`. */
|
|
413
|
+
createTableSQL() {
|
|
414
|
+
return `CREATE TABLE IF NOT EXISTS ${this.tableName} (
|
|
415
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
416
|
+
timestamp VARCHAR(64) NOT NULL,
|
|
417
|
+
level VARCHAR(16) NOT NULL,
|
|
418
|
+
logger VARCHAR(255) NOT NULL,
|
|
419
|
+
message TEXT NOT NULL,
|
|
420
|
+
meta JSON NOT NULL,
|
|
421
|
+
runId VARCHAR(255),
|
|
422
|
+
spanId VARCHAR(255),
|
|
423
|
+
parentSpanId VARCHAR(255),
|
|
424
|
+
traceId VARCHAR(255)
|
|
425
|
+
)`;
|
|
426
|
+
}
|
|
427
|
+
async ensureTable() {
|
|
428
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
429
|
+
await client.execute(this.createTableSQL(), []);
|
|
430
|
+
}
|
|
431
|
+
async insertRows(rows) {
|
|
432
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
433
|
+
const columns = 9;
|
|
434
|
+
const values = [];
|
|
435
|
+
const placeholders = [];
|
|
436
|
+
for (const row of rows) {
|
|
437
|
+
placeholders.push(`(${Array.from({ length: columns }, () => "?").join(", ")})`);
|
|
438
|
+
values.push(
|
|
439
|
+
row.timestamp,
|
|
440
|
+
row.level,
|
|
441
|
+
row.logger,
|
|
442
|
+
row.message,
|
|
443
|
+
row.meta,
|
|
444
|
+
row.runId,
|
|
445
|
+
row.spanId,
|
|
446
|
+
row.parentSpanId,
|
|
447
|
+
row.traceId
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
const sql = `INSERT INTO ${this.tableName} (timestamp, level, logger, message, meta, runId, spanId, parentSpanId, traceId) VALUES ${placeholders.join(", ")}`;
|
|
451
|
+
await client.execute(sql, values);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// src/transports/nosql/mongodb-transport.ts
|
|
456
|
+
var MongoDBTransport = class extends BatchingTransport {
|
|
457
|
+
injectedCollection;
|
|
458
|
+
connectionString;
|
|
459
|
+
database;
|
|
460
|
+
collectionName;
|
|
461
|
+
collection;
|
|
462
|
+
constructor(options = {}) {
|
|
463
|
+
super(options);
|
|
464
|
+
this.injectedCollection = options.collection;
|
|
465
|
+
this.connectionString = options.connectionString;
|
|
466
|
+
this.database = options.database ?? "logquill";
|
|
467
|
+
this.collectionName = options.collectionName ?? "logs";
|
|
468
|
+
}
|
|
469
|
+
/** Synchronously available collection, if one was injected or already connected — avoids an unnecessary microtask hop on the hot path. */
|
|
470
|
+
resolvedCollection() {
|
|
471
|
+
return this.injectedCollection ?? this.collection;
|
|
472
|
+
}
|
|
473
|
+
async importCollection() {
|
|
474
|
+
if (!this.connectionString) {
|
|
475
|
+
throw new Error(
|
|
476
|
+
"MongoDBTransport: provide either a `collection` or a `connectionString` to connect with \u2014 neither was given"
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
let MongoClientCtor;
|
|
480
|
+
try {
|
|
481
|
+
const moduleName = "mongodb";
|
|
482
|
+
const mod = await import(moduleName);
|
|
483
|
+
MongoClientCtor = mod.MongoClient;
|
|
484
|
+
} catch {
|
|
485
|
+
throw new Error(
|
|
486
|
+
"MongoDBTransport: install `mongodb` to use this transport without providing a client \u2014 `npm install mongodb`"
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
const client = new MongoClientCtor(this.connectionString);
|
|
490
|
+
await client.connect();
|
|
491
|
+
this.collection = client.db(this.database).collection(this.collectionName);
|
|
492
|
+
return this.collection;
|
|
493
|
+
}
|
|
494
|
+
async sendBatch(batch) {
|
|
495
|
+
const collection = this.resolvedCollection() ?? await this.importCollection();
|
|
496
|
+
await collection.insertMany(batch.map((record) => ({ ...record })));
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
// src/transports/nosql/dynamodb-transport.ts
|
|
501
|
+
var DYNAMO_BATCH_LIMIT = 25;
|
|
502
|
+
function toAttributeValue(value) {
|
|
503
|
+
if (value === null || value === void 0) {
|
|
504
|
+
return { NULL: true };
|
|
505
|
+
}
|
|
506
|
+
if (typeof value === "string") {
|
|
507
|
+
return { S: value };
|
|
508
|
+
}
|
|
509
|
+
if (typeof value === "number") {
|
|
510
|
+
return { N: String(value) };
|
|
511
|
+
}
|
|
512
|
+
if (typeof value === "boolean") {
|
|
513
|
+
return { BOOL: value };
|
|
514
|
+
}
|
|
515
|
+
if (typeof value === "bigint") {
|
|
516
|
+
return { N: value.toString() };
|
|
517
|
+
}
|
|
518
|
+
if (Array.isArray(value)) {
|
|
519
|
+
return { L: value.map((entry) => toAttributeValue(entry)) };
|
|
520
|
+
}
|
|
521
|
+
if (typeof value === "object") {
|
|
522
|
+
const m = {};
|
|
523
|
+
for (const [key, entryValue] of Object.entries(value)) {
|
|
524
|
+
m[key] = toAttributeValue(entryValue);
|
|
525
|
+
}
|
|
526
|
+
return { M: m };
|
|
527
|
+
}
|
|
528
|
+
return { NULL: true };
|
|
529
|
+
}
|
|
530
|
+
function marshallItem(item) {
|
|
531
|
+
return toAttributeValue({ ...item }).M;
|
|
532
|
+
}
|
|
533
|
+
var DynamoDBTransport = class extends BatchingTransport {
|
|
534
|
+
injectedClient;
|
|
535
|
+
tableName;
|
|
536
|
+
region;
|
|
537
|
+
client;
|
|
538
|
+
constructor(options = {}) {
|
|
539
|
+
super(options);
|
|
540
|
+
this.injectedClient = options.client;
|
|
541
|
+
this.tableName = options.tableName ?? "logs";
|
|
542
|
+
this.region = options.region;
|
|
543
|
+
}
|
|
544
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
545
|
+
resolvedClient() {
|
|
546
|
+
return this.injectedClient ?? this.client;
|
|
547
|
+
}
|
|
548
|
+
async importClient() {
|
|
549
|
+
let DynamoDBClientCtor;
|
|
550
|
+
let BatchWriteItemCommandCtor;
|
|
551
|
+
try {
|
|
552
|
+
const moduleName = "@aws-sdk/client-dynamodb";
|
|
553
|
+
const mod = await import(moduleName);
|
|
554
|
+
DynamoDBClientCtor = mod.DynamoDBClient;
|
|
555
|
+
BatchWriteItemCommandCtor = mod.BatchWriteItemCommand;
|
|
556
|
+
} catch {
|
|
557
|
+
throw new Error(
|
|
558
|
+
"DynamoDBTransport: install `@aws-sdk/client-dynamodb` to use this transport without providing a client \u2014 `npm install @aws-sdk/client-dynamodb`"
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
const sdkClient = new DynamoDBClientCtor(this.region ? { region: this.region } : {});
|
|
562
|
+
this.client = {
|
|
563
|
+
async batchWriteItems(tableName, items) {
|
|
564
|
+
const requestItems = {
|
|
565
|
+
[tableName]: items.map((item) => ({ PutRequest: { Item: marshallItem(item) } }))
|
|
566
|
+
};
|
|
567
|
+
return sdkClient.send(new BatchWriteItemCommandCtor({ RequestItems: requestItems }));
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
return this.client;
|
|
571
|
+
}
|
|
572
|
+
/** `meta.runId`, else `meta.traceId`, else the logger name — see the class doc for why. */
|
|
573
|
+
partitionKey(record) {
|
|
574
|
+
const runId = record.meta.runId;
|
|
575
|
+
if (typeof runId === "string" && runId.length > 0) {
|
|
576
|
+
return runId;
|
|
577
|
+
}
|
|
578
|
+
const traceId = record.meta.traceId;
|
|
579
|
+
if (typeof traceId === "string" && traceId.length > 0) {
|
|
580
|
+
return traceId;
|
|
581
|
+
}
|
|
582
|
+
return record.logger;
|
|
583
|
+
}
|
|
584
|
+
toDynamoItem(record) {
|
|
585
|
+
const spanId = record.meta.spanId;
|
|
586
|
+
const parentSpanId = record.meta.parentSpanId;
|
|
587
|
+
const traceId = record.meta.traceId;
|
|
588
|
+
return {
|
|
589
|
+
runId: this.partitionKey(record),
|
|
590
|
+
timestamp: record.timestamp,
|
|
591
|
+
level: record.level,
|
|
592
|
+
logger: record.logger,
|
|
593
|
+
message: record.message,
|
|
594
|
+
meta: record.meta,
|
|
595
|
+
...typeof spanId === "string" ? { spanId } : {},
|
|
596
|
+
...typeof parentSpanId === "string" ? { parentSpanId } : {},
|
|
597
|
+
...typeof traceId === "string" ? { traceId } : {}
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
async sendBatch(batch) {
|
|
601
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
602
|
+
const items = batch.map((record) => this.toDynamoItem(record));
|
|
603
|
+
const chunks = [];
|
|
604
|
+
for (let offset = 0; offset < items.length; offset += DYNAMO_BATCH_LIMIT) {
|
|
605
|
+
chunks.push(items.slice(offset, offset + DYNAMO_BATCH_LIMIT));
|
|
606
|
+
}
|
|
607
|
+
await Promise.all(chunks.map((chunk) => client.batchWriteItems(this.tableName, chunk)));
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
// src/transports/nosql/redis-transport.ts
|
|
612
|
+
var RedisTransport = class extends BatchingTransport {
|
|
613
|
+
injectedClient;
|
|
614
|
+
url;
|
|
615
|
+
stream;
|
|
616
|
+
client;
|
|
617
|
+
constructor(options = {}) {
|
|
618
|
+
super(options);
|
|
619
|
+
this.injectedClient = options.client;
|
|
620
|
+
this.url = options.url ?? "redis://localhost:6379";
|
|
621
|
+
this.stream = options.stream ?? "logquill:logs";
|
|
622
|
+
}
|
|
623
|
+
/** Synchronously available client, if one was injected or already connected — avoids an unnecessary microtask hop on the hot path. */
|
|
624
|
+
resolvedClient() {
|
|
625
|
+
return this.injectedClient ?? this.client;
|
|
626
|
+
}
|
|
627
|
+
async importClient() {
|
|
628
|
+
let createClient;
|
|
629
|
+
try {
|
|
630
|
+
const moduleName = "redis";
|
|
631
|
+
const mod = await import(moduleName);
|
|
632
|
+
createClient = mod.createClient;
|
|
633
|
+
} catch {
|
|
634
|
+
throw new Error(
|
|
635
|
+
"RedisTransport: install `redis` to use this transport without providing a client \u2014 `npm install redis`"
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
const client = createClient({ url: this.url });
|
|
639
|
+
await client.connect();
|
|
640
|
+
this.client = client;
|
|
641
|
+
return client;
|
|
642
|
+
}
|
|
643
|
+
toFields(record) {
|
|
644
|
+
return {
|
|
645
|
+
timestamp: record.timestamp,
|
|
646
|
+
level: record.level,
|
|
647
|
+
logger: record.logger,
|
|
648
|
+
message: record.message,
|
|
649
|
+
meta: JSON.stringify(record.meta)
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
async sendBatch(batch) {
|
|
653
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
654
|
+
await Promise.all(batch.map((record) => client.xAdd(this.stream, "*", this.toFields(record))));
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
// src/transports/queue/base-queue-transport.ts
|
|
659
|
+
var BaseQueueTransport = class extends BatchingTransport {
|
|
660
|
+
topic;
|
|
661
|
+
constructor(options) {
|
|
662
|
+
super(options);
|
|
663
|
+
this.topic = options.topic;
|
|
664
|
+
}
|
|
665
|
+
sendBatch(batch) {
|
|
666
|
+
return this.publishBatch(batch);
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
// src/transports/queue/kafka-transport.ts
|
|
671
|
+
function metaKey(meta, key) {
|
|
672
|
+
const value = meta[key];
|
|
673
|
+
return typeof value === "string" ? value : void 0;
|
|
674
|
+
}
|
|
675
|
+
var KafkaTransport = class extends BaseQueueTransport {
|
|
676
|
+
injectedClient;
|
|
677
|
+
brokers;
|
|
678
|
+
client;
|
|
679
|
+
constructor(options) {
|
|
680
|
+
super(options);
|
|
681
|
+
this.injectedClient = options.client;
|
|
682
|
+
this.brokers = options.brokers ?? ["localhost:9092"];
|
|
683
|
+
}
|
|
684
|
+
/** Synchronously available producer, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
685
|
+
resolvedClient() {
|
|
686
|
+
return this.injectedClient ?? this.client;
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Builds the real `kafkajs` producer and connects it. Connection happens
|
|
690
|
+
* here, once, as part of acquiring the driver — not on every
|
|
691
|
+
* `publishBatch()` call — so an injected `client` (tests, or a caller's
|
|
692
|
+
* own already-connected producer) is trusted to already be ready and is
|
|
693
|
+
* never re-connected.
|
|
694
|
+
*/
|
|
695
|
+
async importClient() {
|
|
696
|
+
let producer;
|
|
697
|
+
try {
|
|
698
|
+
const moduleName = "kafkajs";
|
|
699
|
+
const mod = await import(moduleName);
|
|
700
|
+
producer = new mod.Kafka({ brokers: this.brokers }).producer();
|
|
701
|
+
await producer.connect?.();
|
|
702
|
+
} catch {
|
|
703
|
+
throw new Error(
|
|
704
|
+
"KafkaTransport: install `kafkajs` to use this transport without providing a client \u2014 `npm install kafkajs`"
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
this.client = producer;
|
|
708
|
+
return producer;
|
|
709
|
+
}
|
|
710
|
+
async publishBatch(records) {
|
|
711
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
712
|
+
await client.send({
|
|
713
|
+
topic: this.topic,
|
|
714
|
+
messages: records.map((record) => ({
|
|
715
|
+
key: metaKey(record.meta, "runId") ?? metaKey(record.meta, "traceId") ?? null,
|
|
716
|
+
value: JSON.stringify(record)
|
|
717
|
+
}))
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
// src/transports/queue/rabbitmq-transport.ts
|
|
723
|
+
var RabbitMQTransport = class extends BaseQueueTransport {
|
|
724
|
+
injectedClient;
|
|
725
|
+
url;
|
|
726
|
+
client;
|
|
727
|
+
constructor(options) {
|
|
728
|
+
super(options);
|
|
729
|
+
this.injectedClient = options.client;
|
|
730
|
+
this.url = options.url ?? "amqp://localhost";
|
|
731
|
+
}
|
|
732
|
+
/** Synchronously available channel, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
733
|
+
resolvedClient() {
|
|
734
|
+
return this.injectedClient ?? this.client;
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Opens the real `amqplib` connection/channel and asserts the queue
|
|
738
|
+
* exists. Both happen here, once, as part of acquiring the driver — not on
|
|
739
|
+
* every `publishBatch()` call — so an injected `client` (tests, or a
|
|
740
|
+
* caller's own already-open channel) is trusted to already have its queue
|
|
741
|
+
* set up and is never re-asserted.
|
|
742
|
+
*/
|
|
743
|
+
async importClient() {
|
|
744
|
+
let channel;
|
|
745
|
+
try {
|
|
746
|
+
const moduleName = "amqplib";
|
|
747
|
+
const mod = await import(moduleName);
|
|
748
|
+
const connection = await mod.connect(this.url);
|
|
749
|
+
channel = await connection.createChannel();
|
|
750
|
+
await channel.assertQueue?.(this.topic);
|
|
751
|
+
} catch {
|
|
752
|
+
throw new Error(
|
|
753
|
+
"RabbitMQTransport: install `amqplib` to use this transport without providing a client \u2014 `npm install amqplib`"
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
this.client = channel;
|
|
757
|
+
return channel;
|
|
758
|
+
}
|
|
759
|
+
async publishBatch(records) {
|
|
760
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
761
|
+
for (const record of records) {
|
|
762
|
+
client.sendToQueue(this.topic, Buffer.from(JSON.stringify(record)));
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
// src/transports/queue/sqs-transport.ts
|
|
768
|
+
var SQS_BATCH_LIMIT = 10;
|
|
769
|
+
var SQSTransport = class extends BaseQueueTransport {
|
|
770
|
+
injectedClient;
|
|
771
|
+
region;
|
|
772
|
+
client;
|
|
773
|
+
constructor(options) {
|
|
774
|
+
super(options);
|
|
775
|
+
this.injectedClient = options.client;
|
|
776
|
+
this.region = options.region;
|
|
777
|
+
}
|
|
778
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
779
|
+
resolvedClient() {
|
|
780
|
+
return this.injectedClient ?? this.client;
|
|
781
|
+
}
|
|
782
|
+
async importClient() {
|
|
783
|
+
let client;
|
|
784
|
+
try {
|
|
785
|
+
const moduleName = "@aws-sdk/client-sqs";
|
|
786
|
+
const mod = await import(moduleName);
|
|
787
|
+
const sdkClient = new mod.SQSClient({ region: this.region });
|
|
788
|
+
const CommandCtor = mod.SendMessageBatchCommand;
|
|
789
|
+
client = {
|
|
790
|
+
sendMessageBatch: (queueUrl, entries) => sdkClient.send(
|
|
791
|
+
new CommandCtor({
|
|
792
|
+
QueueUrl: queueUrl,
|
|
793
|
+
Entries: entries.map((entry) => ({ Id: entry.id, MessageBody: entry.body }))
|
|
794
|
+
})
|
|
795
|
+
)
|
|
796
|
+
};
|
|
797
|
+
} catch {
|
|
798
|
+
throw new Error(
|
|
799
|
+
"SQSTransport: install `@aws-sdk/client-sqs` to use this transport without providing a client \u2014 `npm install @aws-sdk/client-sqs`"
|
|
800
|
+
);
|
|
801
|
+
}
|
|
802
|
+
this.client = client;
|
|
803
|
+
return client;
|
|
804
|
+
}
|
|
805
|
+
async publishBatch(records) {
|
|
806
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
807
|
+
const chunks = [];
|
|
808
|
+
for (let start = 0; start < records.length; start += SQS_BATCH_LIMIT) {
|
|
809
|
+
chunks.push(records.slice(start, start + SQS_BATCH_LIMIT));
|
|
810
|
+
}
|
|
811
|
+
await Promise.all(
|
|
812
|
+
chunks.map(
|
|
813
|
+
(chunk) => client.sendMessageBatch(
|
|
814
|
+
this.topic,
|
|
815
|
+
chunk.map((record, index) => ({ id: String(index), body: JSON.stringify(record) }))
|
|
816
|
+
)
|
|
817
|
+
)
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
// src/transports/queue/pubsub-transport.ts
|
|
823
|
+
var PubSubTransport = class extends BaseQueueTransport {
|
|
824
|
+
injectedClient;
|
|
825
|
+
projectId;
|
|
826
|
+
client;
|
|
827
|
+
constructor(options) {
|
|
828
|
+
super(options);
|
|
829
|
+
this.injectedClient = options.client;
|
|
830
|
+
this.projectId = options.projectId;
|
|
831
|
+
}
|
|
832
|
+
/** Synchronously available topic reference, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
833
|
+
resolvedClient() {
|
|
834
|
+
return this.injectedClient ?? this.client;
|
|
835
|
+
}
|
|
836
|
+
async importClient() {
|
|
837
|
+
let topic;
|
|
838
|
+
try {
|
|
839
|
+
const moduleName = "@google-cloud/pubsub";
|
|
840
|
+
const mod = await import(moduleName);
|
|
841
|
+
topic = new mod.PubSub({ projectId: this.projectId }).topic(this.topic);
|
|
842
|
+
} catch {
|
|
843
|
+
throw new Error(
|
|
844
|
+
"PubSubTransport: install `@google-cloud/pubsub` to use this transport without providing a client \u2014 `npm install @google-cloud/pubsub`"
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
this.client = topic;
|
|
848
|
+
return topic;
|
|
849
|
+
}
|
|
850
|
+
async publishBatch(records) {
|
|
851
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
852
|
+
await Promise.all(records.map((record) => client.publishMessage({ data: Buffer.from(JSON.stringify(record)) })));
|
|
853
|
+
}
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
// src/transports/cloud/cloudwatch-transport.ts
|
|
857
|
+
var CloudWatchTransport = class extends BatchingTransport {
|
|
858
|
+
logGroupName;
|
|
859
|
+
logStreamName;
|
|
860
|
+
region;
|
|
861
|
+
injectedClient;
|
|
862
|
+
client;
|
|
863
|
+
constructor(options) {
|
|
864
|
+
super(options);
|
|
865
|
+
this.logGroupName = options.logGroupName;
|
|
866
|
+
this.logStreamName = options.logStreamName;
|
|
867
|
+
this.region = options.region;
|
|
868
|
+
this.injectedClient = options.client;
|
|
869
|
+
}
|
|
870
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
871
|
+
resolvedClient() {
|
|
872
|
+
return this.injectedClient ?? this.client;
|
|
873
|
+
}
|
|
874
|
+
async importClient() {
|
|
875
|
+
let ClientCtor;
|
|
876
|
+
let PutLogEventsCommandCtor;
|
|
877
|
+
try {
|
|
878
|
+
const moduleName = "@aws-sdk/client-cloudwatch-logs";
|
|
879
|
+
const mod = await import(moduleName);
|
|
880
|
+
ClientCtor = mod.CloudWatchLogsClient;
|
|
881
|
+
PutLogEventsCommandCtor = mod.PutLogEventsCommand;
|
|
882
|
+
} catch {
|
|
883
|
+
throw new Error(
|
|
884
|
+
"CloudWatchTransport: install `@aws-sdk/client-cloudwatch-logs` to use this transport without providing a client \u2014 `npm install @aws-sdk/client-cloudwatch-logs`"
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
const sdkClient = new ClientCtor({ region: this.region });
|
|
888
|
+
this.client = {
|
|
889
|
+
putLogEvents: (logGroupName, logStreamName, events) => sdkClient.send(
|
|
890
|
+
new PutLogEventsCommandCtor({
|
|
891
|
+
logGroupName,
|
|
892
|
+
logStreamName,
|
|
893
|
+
logEvents: events
|
|
894
|
+
})
|
|
895
|
+
)
|
|
896
|
+
};
|
|
897
|
+
return this.client;
|
|
898
|
+
}
|
|
899
|
+
async sendBatch(batch) {
|
|
900
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
901
|
+
const events = batch.map((record) => ({
|
|
902
|
+
timestamp: Date.parse(record.timestamp),
|
|
903
|
+
message: this.format(record)
|
|
904
|
+
})).sort((a, b) => a.timestamp - b.timestamp);
|
|
905
|
+
await client.putLogEvents(this.logGroupName, this.logStreamName, events);
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
|
|
909
|
+
// src/transports/cloud/cloud-logging-transport.ts
|
|
910
|
+
function gcpSeverity(levelValue) {
|
|
911
|
+
const level = parseLevel(levelValue);
|
|
912
|
+
switch (level) {
|
|
913
|
+
case 5 /* TRACE */:
|
|
914
|
+
case 10 /* DEBUG */:
|
|
915
|
+
return "DEBUG";
|
|
916
|
+
case 20 /* INFO */:
|
|
917
|
+
return "INFO";
|
|
918
|
+
case 30 /* WARN */:
|
|
919
|
+
return "WARNING";
|
|
920
|
+
case 40 /* ERROR */:
|
|
921
|
+
return "ERROR";
|
|
922
|
+
case 50 /* FATAL */:
|
|
923
|
+
return "CRITICAL";
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
var CloudLoggingTransport = class extends BatchingTransport {
|
|
927
|
+
logName;
|
|
928
|
+
projectId;
|
|
929
|
+
injectedClient;
|
|
930
|
+
client;
|
|
931
|
+
constructor(options = {}) {
|
|
932
|
+
super(options);
|
|
933
|
+
this.logName = options.logName ?? "logquill";
|
|
934
|
+
this.projectId = options.projectId;
|
|
935
|
+
this.injectedClient = options.client;
|
|
936
|
+
}
|
|
937
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
938
|
+
resolvedClient() {
|
|
939
|
+
return this.injectedClient ?? this.client;
|
|
940
|
+
}
|
|
941
|
+
async importClient() {
|
|
942
|
+
let LoggingCtor;
|
|
943
|
+
try {
|
|
944
|
+
const moduleName = "@google-cloud/logging";
|
|
945
|
+
const mod = await import(moduleName);
|
|
946
|
+
LoggingCtor = mod.Logging;
|
|
947
|
+
} catch {
|
|
948
|
+
throw new Error(
|
|
949
|
+
"CloudLoggingTransport: install `@google-cloud/logging` to use this transport without providing a client \u2014 `npm install @google-cloud/logging`"
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
const logging = new LoggingCtor({ projectId: this.projectId });
|
|
953
|
+
const log = logging.log(this.logName);
|
|
954
|
+
this.client = {
|
|
955
|
+
writeLogEntries: (entries) => log.write(entries)
|
|
956
|
+
};
|
|
957
|
+
return this.client;
|
|
958
|
+
}
|
|
959
|
+
async sendBatch(batch) {
|
|
960
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
961
|
+
const entries = batch.map((record) => ({
|
|
962
|
+
severity: gcpSeverity(record.level),
|
|
963
|
+
timestamp: record.timestamp,
|
|
964
|
+
jsonPayload: JSON.parse(this.format(record))
|
|
965
|
+
}));
|
|
966
|
+
await client.writeLogEntries(entries);
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
// src/transports/cloud/app-insights-transport.ts
|
|
971
|
+
function appInsightsSeverity(levelValue) {
|
|
972
|
+
const level = parseLevel(levelValue);
|
|
973
|
+
switch (level) {
|
|
974
|
+
case 5 /* TRACE */:
|
|
975
|
+
case 10 /* DEBUG */:
|
|
976
|
+
return 0;
|
|
977
|
+
// Verbose
|
|
978
|
+
case 20 /* INFO */:
|
|
979
|
+
return 1;
|
|
980
|
+
// Information
|
|
981
|
+
case 30 /* WARN */:
|
|
982
|
+
return 2;
|
|
983
|
+
// Warning
|
|
984
|
+
case 40 /* ERROR */:
|
|
985
|
+
return 3;
|
|
986
|
+
// Error
|
|
987
|
+
case 50 /* FATAL */:
|
|
988
|
+
return 4;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
var AppInsightsTransport = class extends BatchingTransport {
|
|
992
|
+
connectionString;
|
|
993
|
+
injectedClient;
|
|
994
|
+
client;
|
|
995
|
+
constructor(options = {}) {
|
|
996
|
+
super(options);
|
|
997
|
+
this.connectionString = options.connectionString;
|
|
998
|
+
this.injectedClient = options.client;
|
|
999
|
+
}
|
|
1000
|
+
/** Synchronously available client, if one was injected or already imported — avoids an unnecessary microtask hop on the hot path. */
|
|
1001
|
+
resolvedClient() {
|
|
1002
|
+
return this.injectedClient ?? this.client;
|
|
1003
|
+
}
|
|
1004
|
+
async importClient() {
|
|
1005
|
+
let TelemetryClientCtor;
|
|
1006
|
+
try {
|
|
1007
|
+
const moduleName = "applicationinsights";
|
|
1008
|
+
const mod = await import(moduleName);
|
|
1009
|
+
TelemetryClientCtor = mod.TelemetryClient;
|
|
1010
|
+
} catch {
|
|
1011
|
+
throw new Error(
|
|
1012
|
+
"AppInsightsTransport: install `applicationinsights` to use this transport without providing a client \u2014 `npm install applicationinsights`"
|
|
1013
|
+
);
|
|
1014
|
+
}
|
|
1015
|
+
const telemetryClient = new TelemetryClientCtor(this.connectionString);
|
|
1016
|
+
this.client = {
|
|
1017
|
+
trackTraceBatch: (traces) => {
|
|
1018
|
+
for (const trace of traces) {
|
|
1019
|
+
telemetryClient.trackTrace(trace);
|
|
1020
|
+
}
|
|
1021
|
+
telemetryClient.flush();
|
|
1022
|
+
return Promise.resolve();
|
|
1023
|
+
}
|
|
1024
|
+
};
|
|
1025
|
+
return this.client;
|
|
1026
|
+
}
|
|
1027
|
+
async sendBatch(batch) {
|
|
1028
|
+
const client = this.resolvedClient() ?? await this.importClient();
|
|
1029
|
+
const traces = batch.map((record) => ({
|
|
1030
|
+
message: this.format(record),
|
|
1031
|
+
severity: appInsightsSeverity(record.level)
|
|
1032
|
+
}));
|
|
1033
|
+
await client.trackTraceBatch(traces);
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
|
|
1037
|
+
// src/transports/cloud/datadog-transport.ts
|
|
1038
|
+
async function fetchDatadogSender(url, apiKey, batch) {
|
|
1039
|
+
const response = await fetch(url, {
|
|
1040
|
+
method: "POST",
|
|
1041
|
+
headers: { "Content-Type": "application/json", "DD-API-KEY": apiKey },
|
|
1042
|
+
body: `[${batch.join(",")}]`
|
|
1043
|
+
});
|
|
1044
|
+
if (!response.ok) {
|
|
1045
|
+
throw new Error(
|
|
1046
|
+
`DatadogTransport: request to ${url} failed with status ${String(response.status)} \u2014 check the API key and site region`
|
|
1047
|
+
);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
var DatadogTransport = class extends BatchingTransport {
|
|
1051
|
+
url;
|
|
1052
|
+
apiKey;
|
|
1053
|
+
site;
|
|
1054
|
+
sender;
|
|
1055
|
+
constructor(options) {
|
|
1056
|
+
super(options);
|
|
1057
|
+
this.apiKey = options.apiKey;
|
|
1058
|
+
this.site = options.site ?? "datadoghq.com";
|
|
1059
|
+
this.url = `https://http-intake.logs.${this.site}/api/v2/logs`;
|
|
1060
|
+
this.sender = options.sender ?? fetchDatadogSender;
|
|
1061
|
+
}
|
|
1062
|
+
sendBatch(batch) {
|
|
1063
|
+
const formatted = batch.map((record) => this.format(record));
|
|
1064
|
+
return this.sender(this.url, this.apiKey, formatted);
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
|
|
1068
|
+
// src/transports/cloud/elasticsearch-transport.ts
|
|
1069
|
+
async function fetchElasticsearchSender(url, headers, body) {
|
|
1070
|
+
const response = await fetch(url, {
|
|
1071
|
+
method: "POST",
|
|
1072
|
+
headers: { ...headers, "Content-Type": "application/x-ndjson" },
|
|
1073
|
+
body
|
|
1074
|
+
});
|
|
1075
|
+
if (!response.ok) {
|
|
1076
|
+
throw new Error(`ElasticsearchTransport: request to ${url} failed with status ${String(response.status)}`);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
var ElasticsearchTransport = class extends BatchingTransport {
|
|
1080
|
+
url;
|
|
1081
|
+
index;
|
|
1082
|
+
apiKey;
|
|
1083
|
+
sender;
|
|
1084
|
+
constructor(options) {
|
|
1085
|
+
super(options);
|
|
1086
|
+
this.index = options.index ?? "logs";
|
|
1087
|
+
this.url = `${options.node.replace(/\/+$/, "")}/_bulk`;
|
|
1088
|
+
this.apiKey = options.apiKey;
|
|
1089
|
+
this.sender = options.sender ?? fetchElasticsearchSender;
|
|
1090
|
+
}
|
|
1091
|
+
sendBatch(batch) {
|
|
1092
|
+
const lines = [];
|
|
1093
|
+
for (const record of batch) {
|
|
1094
|
+
lines.push(JSON.stringify({ index: { _index: this.index } }));
|
|
1095
|
+
lines.push(this.format(record));
|
|
1096
|
+
}
|
|
1097
|
+
const body = `${lines.join("\n")}
|
|
1098
|
+
`;
|
|
1099
|
+
const headers = {};
|
|
1100
|
+
if (this.apiKey !== void 0) {
|
|
1101
|
+
headers.Authorization = `ApiKey ${this.apiKey}`;
|
|
1102
|
+
}
|
|
1103
|
+
return this.sender(this.url, headers, body);
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
async function fetchNewRelicSender(url, headers, body) {
|
|
1107
|
+
const response = await fetch(url, { method: "POST", headers, body });
|
|
1108
|
+
return {
|
|
1109
|
+
ok: response.ok,
|
|
1110
|
+
status: response.status,
|
|
1111
|
+
retryAfter: response.headers.get("retry-after")
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
function withoutEventType(record) {
|
|
1115
|
+
const meta = { ...record.meta };
|
|
1116
|
+
delete meta.eventType;
|
|
1117
|
+
return { ...record, meta };
|
|
1118
|
+
}
|
|
1119
|
+
function resumeTimestamp(retryAfter, now) {
|
|
1120
|
+
if (retryAfter === null) {
|
|
1121
|
+
return now + 6e4;
|
|
1122
|
+
}
|
|
1123
|
+
const seconds = Number(retryAfter);
|
|
1124
|
+
if (Number.isFinite(seconds)) {
|
|
1125
|
+
return now + seconds * 1e3;
|
|
1126
|
+
}
|
|
1127
|
+
const dateMs = Date.parse(retryAfter);
|
|
1128
|
+
return Number.isNaN(dateMs) ? now + 6e4 : dateMs;
|
|
1129
|
+
}
|
|
1130
|
+
var NewRelicTransport = class extends BatchingTransport {
|
|
1131
|
+
url;
|
|
1132
|
+
region;
|
|
1133
|
+
licenseKey;
|
|
1134
|
+
sender;
|
|
1135
|
+
clock;
|
|
1136
|
+
pausedUntil = null;
|
|
1137
|
+
constructor(options) {
|
|
1138
|
+
super(options);
|
|
1139
|
+
this.licenseKey = options.licenseKey;
|
|
1140
|
+
this.region = options.region ?? "US";
|
|
1141
|
+
this.url = this.region === "EU" ? "https://log-api.eu.newrelic.com/log/v1" : "https://log-api.newrelic.com/log/v1";
|
|
1142
|
+
this.sender = options.sender ?? fetchNewRelicSender;
|
|
1143
|
+
this.clock = options.clock ?? Date.now;
|
|
1144
|
+
}
|
|
1145
|
+
async sendBatch(batch) {
|
|
1146
|
+
const now = this.clock();
|
|
1147
|
+
if (this.pausedUntil !== null && now < this.pausedUntil) {
|
|
1148
|
+
console.error(
|
|
1149
|
+
`NewRelicTransport: sends paused until ${new Date(this.pausedUntil).toISOString()} after a 429 rate-limit response \u2014 skipping this batch rather than making a doomed request`
|
|
1150
|
+
);
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
this.pausedUntil = null;
|
|
1154
|
+
const records = batch.map((record) => withoutEventType(record));
|
|
1155
|
+
const body = zlib.gzipSync(Buffer.from(JSON.stringify(records)));
|
|
1156
|
+
const headers = {
|
|
1157
|
+
"Content-Type": "application/json",
|
|
1158
|
+
"Content-Encoding": "gzip",
|
|
1159
|
+
"Api-Key": this.licenseKey
|
|
1160
|
+
};
|
|
1161
|
+
const result = await this.sender(this.url, headers, body);
|
|
1162
|
+
if (result.status === 429) {
|
|
1163
|
+
this.pausedUntil = resumeTimestamp(result.retryAfter, now);
|
|
1164
|
+
console.error(
|
|
1165
|
+
`NewRelicTransport: received 429 from New Relic \u2014 pausing sends until ${new Date(this.pausedUntil).toISOString()}. Reduce log volume or increase batching to stay under the rate limit.`
|
|
1166
|
+
);
|
|
1167
|
+
return;
|
|
1168
|
+
}
|
|
1169
|
+
if (!result.ok) {
|
|
1170
|
+
throw new Error(
|
|
1171
|
+
`NewRelicTransport: request to ${this.url} failed with status ${String(result.status)} \u2014 check the license key and region`
|
|
1172
|
+
);
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
};
|
|
1176
|
+
|
|
1177
|
+
// src/transports/console-transport.ts
|
|
134
1178
|
var COLORS = {
|
|
135
1179
|
[5 /* TRACE */]: "\x1B[90m",
|
|
136
1180
|
// gray
|
|
@@ -218,7 +1262,7 @@ var FileTransport = class extends Transport {
|
|
|
218
1262
|
}
|
|
219
1263
|
};
|
|
220
1264
|
|
|
221
|
-
// src/http-transport.ts
|
|
1265
|
+
// src/transports/http-transport.ts
|
|
222
1266
|
async function fetchSender(url, batch) {
|
|
223
1267
|
const response = await fetch(url, {
|
|
224
1268
|
method: "POST",
|
|
@@ -265,7 +1309,7 @@ var HTTPTransport = class extends Transport {
|
|
|
265
1309
|
}
|
|
266
1310
|
};
|
|
267
1311
|
|
|
268
|
-
// src/logger.ts
|
|
1312
|
+
// src/core/logger.ts
|
|
269
1313
|
var Logger = class _Logger {
|
|
270
1314
|
name;
|
|
271
1315
|
transports;
|
|
@@ -367,18 +1411,37 @@ var Logger = class _Logger {
|
|
|
367
1411
|
};
|
|
368
1412
|
|
|
369
1413
|
// src/index.ts
|
|
370
|
-
var VERSION = "0.
|
|
1414
|
+
var VERSION = "0.2.0";
|
|
371
1415
|
|
|
1416
|
+
exports.AppInsightsTransport = AppInsightsTransport;
|
|
1417
|
+
exports.BaseQueueTransport = BaseQueueTransport;
|
|
1418
|
+
exports.BaseSQLTransport = BaseSQLTransport;
|
|
1419
|
+
exports.BatchingTransport = BatchingTransport;
|
|
1420
|
+
exports.CloudLoggingTransport = CloudLoggingTransport;
|
|
1421
|
+
exports.CloudWatchTransport = CloudWatchTransport;
|
|
372
1422
|
exports.CollectingTransport = CollectingTransport;
|
|
373
1423
|
exports.ConsoleTransport = ConsoleTransport;
|
|
374
1424
|
exports.ContextPlugin = ContextPlugin;
|
|
375
1425
|
exports.DEFAULT_REDACTED_KEYS = DEFAULT_REDACTED_KEYS;
|
|
1426
|
+
exports.DatadogTransport = DatadogTransport;
|
|
1427
|
+
exports.DynamoDBTransport = DynamoDBTransport;
|
|
1428
|
+
exports.ElasticsearchTransport = ElasticsearchTransport;
|
|
376
1429
|
exports.FileTransport = FileTransport;
|
|
377
1430
|
exports.HTTPTransport = HTTPTransport;
|
|
378
1431
|
exports.JSONFormatter = JSONFormatter;
|
|
1432
|
+
exports.KafkaTransport = KafkaTransport;
|
|
379
1433
|
exports.Level = Level;
|
|
380
1434
|
exports.Logger = Logger;
|
|
1435
|
+
exports.MongoDBTransport = MongoDBTransport;
|
|
1436
|
+
exports.MySQLTransport = MySQLTransport;
|
|
1437
|
+
exports.NewRelicTransport = NewRelicTransport;
|
|
1438
|
+
exports.PostgresTransport = PostgresTransport;
|
|
1439
|
+
exports.PubSubTransport = PubSubTransport;
|
|
1440
|
+
exports.RabbitMQTransport = RabbitMQTransport;
|
|
381
1441
|
exports.RedactPlugin = RedactPlugin;
|
|
1442
|
+
exports.RedisTransport = RedisTransport;
|
|
1443
|
+
exports.SQLiteTransport = SQLiteTransport;
|
|
1444
|
+
exports.SQSTransport = SQSTransport;
|
|
382
1445
|
exports.SamplingPlugin = SamplingPlugin;
|
|
383
1446
|
exports.Transport = Transport;
|
|
384
1447
|
exports.VERSION = VERSION;
|