@mcesystems/logging-g4 1.0.70 → 1.0.72

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/dist/index.mjs CHANGED
@@ -20275,55 +20275,172 @@ var require_src = __commonJS2({
20275
20275
  }
20276
20276
  });
20277
20277
  var import_debug = __toESM2(require_src());
20278
- var logInfo = (0, import_debug.default)("info");
20279
- var logTask = (0, import_debug.default)("task");
20280
- var logError = (0, import_debug.default)("error");
20281
- var logDetail = (0, import_debug.default)("detail");
20282
- var logDebug = (0, import_debug.default)("debug");
20283
- var logWarning = (0, import_debug.default)("warning");
20284
- var logColor = (0, import_debug.default)("color");
20285
- logInfo.color = "19";
20286
- logTask.color = "25";
20287
- logError.color = "1";
20288
- logDetail.color = "199";
20289
- logWarning.color = "186";
20290
- logDebug.color = "211";
20291
- logColor.enabled = true;
20292
- function logNamespace(namespace) {
20293
- logInfo.namespace = `${namespace}:info`;
20294
- logTask.namespace = `${namespace}:task`;
20295
- logError.namespace = `${namespace}:error`;
20296
- logDetail.namespace = `${namespace}:detail`;
20297
- logWarning.namespace = `${namespace}:warning`;
20298
- logDebug.namespace = `${namespace}:debug`;
20299
- }
20300
- function setLogLevel(level) {
20301
- switch (level) {
20302
- case "info":
20303
- logInfo.enabled = true;
20304
- logTask.enabled = true;
20305
- logError.enabled = true;
20306
- logWarning.enabled = true;
20307
- logDetail.enabled = false;
20308
- logDebug.enabled = false;
20309
- break;
20310
- case "debug":
20311
- logInfo.enabled = true;
20312
- logTask.enabled = true;
20313
- logError.enabled = true;
20314
- logWarning.enabled = true;
20315
- logDetail.enabled = true;
20316
- logDebug.enabled = true;
20317
- break;
20318
- case "none":
20319
- logInfo.enabled = false;
20320
- logTask.enabled = false;
20321
- logError.enabled = false;
20322
- logWarning.enabled = false;
20323
- logDetail.enabled = false;
20324
- logDebug.enabled = false;
20325
- break;
20278
+ function createLoggers(namespace, logLevel = process.env.LOG_LEVEL ?? "none") {
20279
+ const logInfo22 = (0, import_debug.default)(`${namespace}:info`);
20280
+ const logTask = (0, import_debug.default)(`${namespace}:task`);
20281
+ const logError2 = (0, import_debug.default)(`${namespace}:error`);
20282
+ const logDetail2 = (0, import_debug.default)(`${namespace}:detail`);
20283
+ const logDebug = (0, import_debug.default)(`${namespace}:debug`);
20284
+ const logWarning = (0, import_debug.default)(`${namespace}:warning`);
20285
+ const logColor = (0, import_debug.default)(`${namespace}:color`);
20286
+ logInfo22.color = "19";
20287
+ logTask.color = "25";
20288
+ logError2.color = "1";
20289
+ logDetail2.color = "199";
20290
+ logWarning.color = "186";
20291
+ logDebug.color = "211";
20292
+ logColor.enabled = true;
20293
+ function setNamespace(namespace2) {
20294
+ logInfo22.namespace = `${namespace2}:info`;
20295
+ logTask.namespace = `${namespace2}:task`;
20296
+ logError2.namespace = `${namespace2}:error`;
20297
+ logDetail2.namespace = `${namespace2}:detail`;
20298
+ logWarning.namespace = `${namespace2}:warning`;
20299
+ logDebug.namespace = `${namespace2}:debug`;
20300
+ }
20301
+ function setLogLevel(level) {
20302
+ switch (level) {
20303
+ case "info":
20304
+ logInfo22.enabled = true;
20305
+ logTask.enabled = true;
20306
+ logError2.enabled = true;
20307
+ logWarning.enabled = true;
20308
+ logDetail2.enabled = false;
20309
+ logDebug.enabled = false;
20310
+ break;
20311
+ case "debug":
20312
+ logInfo22.enabled = true;
20313
+ logTask.enabled = true;
20314
+ logError2.enabled = true;
20315
+ logWarning.enabled = true;
20316
+ logDetail2.enabled = true;
20317
+ logDebug.enabled = true;
20318
+ break;
20319
+ case "none":
20320
+ logInfo22.enabled = false;
20321
+ logTask.enabled = false;
20322
+ logError2.enabled = false;
20323
+ logWarning.enabled = false;
20324
+ logDetail2.enabled = false;
20325
+ logDebug.enabled = false;
20326
+ break;
20327
+ }
20328
+ }
20329
+ setLogLevel(logLevel);
20330
+ function setColors(type, color) {
20331
+ switch (type) {
20332
+ case "info":
20333
+ logInfo22.color = color;
20334
+ break;
20335
+ case "task":
20336
+ logTask.color = color;
20337
+ break;
20338
+ case "error":
20339
+ logError2.color = color;
20340
+ break;
20341
+ case "detail":
20342
+ logDetail2.color = color;
20343
+ break;
20344
+ case "warning":
20345
+ logWarning.color = color;
20346
+ break;
20347
+ case "debug":
20348
+ logDebug.color = color;
20349
+ break;
20350
+ default:
20351
+ throw new Error(`Invalid log type: ${type}`);
20352
+ }
20353
+ }
20354
+ function printColors() {
20355
+ for (let i = 0; i < 256; i++) {
20356
+ logColor.color = `${i}`;
20357
+ logColor(`${i}: ${i}`);
20358
+ }
20359
+ }
20360
+ function logDataDetail(data, prefix = "# ") {
20361
+ if (data === null || data === void 0) {
20362
+ return `${prefix}<null or undefined>`;
20363
+ }
20364
+ if (typeof data !== "object") {
20365
+ return `${prefix}${String(data)}`;
20366
+ }
20367
+ const keys = Object.keys(data);
20368
+ let result = "";
20369
+ for (const key of keys) {
20370
+ result += `${prefix}${key}: `;
20371
+ if (key in data) {
20372
+ let dataKey = key;
20373
+ if (key in data) {
20374
+ dataKey = key;
20375
+ const value = data[dataKey];
20376
+ if (value === null || value === void 0) {
20377
+ result += `<${value === null ? "null" : "undefined"}>
20378
+ `;
20379
+ } else if (typeof value === "object") {
20380
+ result += `
20381
+ ${logDataDetail(value, `${prefix} `)}
20382
+ `;
20383
+ } else {
20384
+ result += `${value}
20385
+ `;
20386
+ }
20387
+ }
20388
+ }
20389
+ }
20390
+ return result.trim();
20391
+ }
20392
+ function header(title, padding = 0) {
20393
+ return `${" ".repeat(padding)}${"=".repeat(80)}
20394
+ ${" ".repeat(40 - title.length / 2)}${title}
20395
+ ${" ".repeat(padding)}${"=".repeat(80)}`;
20396
+ }
20397
+ function logHeader(title) {
20398
+ logInfo22(`${header(title, 2)}`);
20399
+ }
20400
+ function logDataObject2(title, ...args) {
20401
+ const stack = new Error().stack?.split("\n")[2] || "";
20402
+ const stackMatch = stack.match(/\((.*):(\d+):(\d+)\)$/) || stack.match(/at (.*):(\d+):(\d+)$/);
20403
+ let callerDetails = "";
20404
+ if (stackMatch) {
20405
+ const functionMatch = stack.match(/at (.+) \(/);
20406
+ const functionName = functionMatch ? functionMatch[1] : "<anonymous>";
20407
+ callerDetails = `${functionName}`;
20408
+ }
20409
+ logDetail2(`${header(`*${title}* ${callerDetails}`)}
20410
+ ${args.reduce((acc, arg) => `${acc}
20411
+ ${logDataDetail(arg)}`, "")}
20412
+
20413
+ ${"=".repeat(80)}`);
20414
+ }
20415
+ function logErrorObject2(error, extraDetails) {
20416
+ if (error instanceof Error) {
20417
+ logError2(`${extraDetails ? header(extraDetails, 1) : header(error.message, 1)}
20418
+ Error Message:
20419
+ ${error.message}
20420
+ Error Stack:
20421
+ ${error.stack}
20422
+ ${"=".repeat(80)}`);
20423
+ } else {
20424
+ logError2(`${extraDetails ? header(extraDetails, 1) : header(String(error), 1)}
20425
+ ${"=".repeat(80)}`);
20426
+ }
20326
20427
  }
20428
+ return {
20429
+ logInfo: logInfo22,
20430
+ logTask,
20431
+ logError: logError2,
20432
+ logDetail: logDetail2,
20433
+ logDebug,
20434
+ logWarning,
20435
+ logColor,
20436
+ setColors,
20437
+ printColors,
20438
+ logHeader,
20439
+ logDataObject: logDataObject2,
20440
+ logErrorObject: logErrorObject2,
20441
+ setLogLevel,
20442
+ setNamespace
20443
+ };
20327
20444
  }
20328
20445
  var AUTHORIZE_QUERY = `
20329
20446
  query Authorize($input: AuthorizeInput!) {
@@ -20342,8 +20459,7 @@ var TOKEN_QUERY = `
20342
20459
  }
20343
20460
  }
20344
20461
  `;
20345
- setLogLevel(process.env.LOG_LEVEL ?? "none");
20346
- logNamespace("auth");
20462
+ var { logInfo } = createLoggers("auth");
20347
20463
  var random = promisify(randomBytes);
20348
20464
  async function getAuthToken(credentials, apiUrl) {
20349
20465
  const authApiUrl = apiUrl || process.env.AUTH_API_URL;
@@ -21264,120 +21380,172 @@ var require_src4 = __commonJS3({
21264
21380
  }
21265
21381
  });
21266
21382
  var import_debug2 = __toESM3(require_src4());
21267
- var logInfo2 = (0, import_debug2.default)("info");
21268
- var logTask2 = (0, import_debug2.default)("task");
21269
- var logError2 = (0, import_debug2.default)("error");
21270
- var logDetail2 = (0, import_debug2.default)("detail");
21271
- var logDebug2 = (0, import_debug2.default)("debug");
21272
- var logWarning2 = (0, import_debug2.default)("warning");
21273
- var logColor2 = (0, import_debug2.default)("color");
21274
- logInfo2.color = "19";
21275
- logTask2.color = "25";
21276
- logError2.color = "1";
21277
- logDetail2.color = "199";
21278
- logWarning2.color = "186";
21279
- logDebug2.color = "211";
21280
- logColor2.enabled = true;
21281
- function logNamespace2(namespace) {
21282
- logInfo2.namespace = `${namespace}:info`;
21283
- logTask2.namespace = `${namespace}:task`;
21284
- logError2.namespace = `${namespace}:error`;
21285
- logDetail2.namespace = `${namespace}:detail`;
21286
- logWarning2.namespace = `${namespace}:warning`;
21287
- logDebug2.namespace = `${namespace}:debug`;
21288
- }
21289
- function setLogLevel2(level) {
21290
- switch (level) {
21291
- case "info":
21292
- logInfo2.enabled = true;
21293
- logTask2.enabled = true;
21294
- logError2.enabled = true;
21295
- logWarning2.enabled = true;
21296
- logDetail2.enabled = false;
21297
- logDebug2.enabled = false;
21298
- break;
21299
- case "debug":
21300
- logInfo2.enabled = true;
21301
- logTask2.enabled = true;
21302
- logError2.enabled = true;
21303
- logWarning2.enabled = true;
21304
- logDetail2.enabled = true;
21305
- logDebug2.enabled = true;
21306
- break;
21307
- case "none":
21308
- logInfo2.enabled = false;
21309
- logTask2.enabled = false;
21310
- logError2.enabled = false;
21311
- logWarning2.enabled = false;
21312
- logDetail2.enabled = false;
21313
- logDebug2.enabled = false;
21314
- break;
21315
- }
21316
- }
21317
- function logDataDetail(data, prefix = "# ") {
21318
- if (data === null || data === void 0) {
21319
- return `${prefix}<null or undefined>`;
21320
- }
21321
- if (typeof data !== "object") {
21322
- return `${prefix}${String(data)}`;
21323
- }
21324
- const keys = Object.keys(data);
21325
- let result = "";
21326
- for (const key of keys) {
21327
- result += `${prefix}${key}: `;
21328
- if (key in data) {
21329
- let dataKey = key;
21383
+ function createLoggers2(namespace, logLevel = process.env.LOG_LEVEL ?? "none") {
21384
+ const logInfo3 = (0, import_debug2.default)(`${namespace}:info`);
21385
+ const logTask = (0, import_debug2.default)(`${namespace}:task`);
21386
+ const logError2 = (0, import_debug2.default)(`${namespace}:error`);
21387
+ const logDetail2 = (0, import_debug2.default)(`${namespace}:detail`);
21388
+ const logDebug = (0, import_debug2.default)(`${namespace}:debug`);
21389
+ const logWarning = (0, import_debug2.default)(`${namespace}:warning`);
21390
+ const logColor = (0, import_debug2.default)(`${namespace}:color`);
21391
+ logInfo3.color = "19";
21392
+ logTask.color = "25";
21393
+ logError2.color = "1";
21394
+ logDetail2.color = "199";
21395
+ logWarning.color = "186";
21396
+ logDebug.color = "211";
21397
+ logColor.enabled = true;
21398
+ function setNamespace(namespace2) {
21399
+ logInfo3.namespace = `${namespace2}:info`;
21400
+ logTask.namespace = `${namespace2}:task`;
21401
+ logError2.namespace = `${namespace2}:error`;
21402
+ logDetail2.namespace = `${namespace2}:detail`;
21403
+ logWarning.namespace = `${namespace2}:warning`;
21404
+ logDebug.namespace = `${namespace2}:debug`;
21405
+ }
21406
+ function setLogLevel(level) {
21407
+ switch (level) {
21408
+ case "info":
21409
+ logInfo3.enabled = true;
21410
+ logTask.enabled = true;
21411
+ logError2.enabled = true;
21412
+ logWarning.enabled = true;
21413
+ logDetail2.enabled = false;
21414
+ logDebug.enabled = false;
21415
+ break;
21416
+ case "debug":
21417
+ logInfo3.enabled = true;
21418
+ logTask.enabled = true;
21419
+ logError2.enabled = true;
21420
+ logWarning.enabled = true;
21421
+ logDetail2.enabled = true;
21422
+ logDebug.enabled = true;
21423
+ break;
21424
+ case "none":
21425
+ logInfo3.enabled = false;
21426
+ logTask.enabled = false;
21427
+ logError2.enabled = false;
21428
+ logWarning.enabled = false;
21429
+ logDetail2.enabled = false;
21430
+ logDebug.enabled = false;
21431
+ break;
21432
+ }
21433
+ }
21434
+ setLogLevel(logLevel);
21435
+ function setColors(type, color) {
21436
+ switch (type) {
21437
+ case "info":
21438
+ logInfo3.color = color;
21439
+ break;
21440
+ case "task":
21441
+ logTask.color = color;
21442
+ break;
21443
+ case "error":
21444
+ logError2.color = color;
21445
+ break;
21446
+ case "detail":
21447
+ logDetail2.color = color;
21448
+ break;
21449
+ case "warning":
21450
+ logWarning.color = color;
21451
+ break;
21452
+ case "debug":
21453
+ logDebug.color = color;
21454
+ break;
21455
+ default:
21456
+ throw new Error(`Invalid log type: ${type}`);
21457
+ }
21458
+ }
21459
+ function printColors() {
21460
+ for (let i = 0; i < 256; i++) {
21461
+ logColor.color = `${i}`;
21462
+ logColor(`${i}: ${i}`);
21463
+ }
21464
+ }
21465
+ function logDataDetail(data, prefix = "# ") {
21466
+ if (data === null || data === void 0) {
21467
+ return `${prefix}<null or undefined>`;
21468
+ }
21469
+ if (typeof data !== "object") {
21470
+ return `${prefix}${String(data)}`;
21471
+ }
21472
+ const keys = Object.keys(data);
21473
+ let result = "";
21474
+ for (const key of keys) {
21475
+ result += `${prefix}${key}: `;
21330
21476
  if (key in data) {
21331
- dataKey = key;
21332
- const value = data[dataKey];
21333
- if (value === null || value === void 0) {
21334
- result += `<${value === null ? "null" : "undefined"}>
21477
+ let dataKey = key;
21478
+ if (key in data) {
21479
+ dataKey = key;
21480
+ const value = data[dataKey];
21481
+ if (value === null || value === void 0) {
21482
+ result += `<${value === null ? "null" : "undefined"}>
21335
21483
  `;
21336
- } else if (typeof value === "object") {
21337
- result += `
21338
- ${logDataDetail(value, `${prefix} `)}
21484
+ } else if (typeof value === "object") {
21485
+ result += `
21486
+ ${logDataDetail(value, `${prefix} `)}
21339
21487
  `;
21340
- } else {
21341
- result += `${value}
21488
+ } else {
21489
+ result += `${value}
21342
21490
  `;
21491
+ }
21343
21492
  }
21344
21493
  }
21345
21494
  }
21495
+ return result.trim();
21346
21496
  }
21347
- return result.trim();
21348
- }
21349
- function header(title, padding = 0) {
21350
- return `${" ".repeat(padding)}${"=".repeat(80)}
21497
+ function header(title, padding = 0) {
21498
+ return `${" ".repeat(padding)}${"=".repeat(80)}
21351
21499
  ${" ".repeat(40 - title.length / 2)}${title}
21352
21500
  ${" ".repeat(padding)}${"=".repeat(80)}`;
21353
- }
21354
- function logDataObject(title, ...args) {
21355
- const stack = new Error().stack?.split("\n")[2] || "";
21356
- const stackMatch = stack.match(/\((.*):(\d+):(\d+)\)$/) || stack.match(/at (.*):(\d+):(\d+)$/);
21357
- let callerDetails = "";
21358
- if (stackMatch) {
21359
- const functionMatch = stack.match(/at (.+) \(/);
21360
- const functionName = functionMatch ? functionMatch[1] : "<anonymous>";
21361
- callerDetails = `${functionName}`;
21362
- }
21363
- logDetail2(`${header(`*${title}* ${callerDetails}`)}
21501
+ }
21502
+ function logHeader(title) {
21503
+ logInfo3(`${header(title, 2)}`);
21504
+ }
21505
+ function logDataObject2(title, ...args) {
21506
+ const stack = new Error().stack?.split("\n")[2] || "";
21507
+ const stackMatch = stack.match(/\((.*):(\d+):(\d+)\)$/) || stack.match(/at (.*):(\d+):(\d+)$/);
21508
+ let callerDetails = "";
21509
+ if (stackMatch) {
21510
+ const functionMatch = stack.match(/at (.+) \(/);
21511
+ const functionName = functionMatch ? functionMatch[1] : "<anonymous>";
21512
+ callerDetails = `${functionName}`;
21513
+ }
21514
+ logDetail2(`${header(`*${title}* ${callerDetails}`)}
21364
21515
  ${args.reduce((acc, arg) => `${acc}
21365
21516
  ${logDataDetail(arg)}`, "")}
21366
-
21517
+
21367
21518
  ${"=".repeat(80)}`);
21368
- }
21369
- function logErrorObject(error, extraDetails) {
21370
- if (error instanceof Error) {
21371
- logError2(`${extraDetails ? header(extraDetails, 1) : header(error.message, 1)}
21372
- Error Message:
21373
- ${error.message}
21374
- Error Stack:
21375
- ${error.stack}
21376
- ${"=".repeat(80)}`);
21377
- } else {
21378
- logError2(`${extraDetails ? header(extraDetails, 1) : header(String(error), 1)}
21379
- ${"=".repeat(80)}`);
21380
21519
  }
21520
+ function logErrorObject2(error, extraDetails) {
21521
+ if (error instanceof Error) {
21522
+ logError2(`${extraDetails ? header(extraDetails, 1) : header(error.message, 1)}
21523
+ Error Message:
21524
+ ${error.message}
21525
+ Error Stack:
21526
+ ${error.stack}
21527
+ ${"=".repeat(80)}`);
21528
+ } else {
21529
+ logError2(`${extraDetails ? header(extraDetails, 1) : header(String(error), 1)}
21530
+ ${"=".repeat(80)}`);
21531
+ }
21532
+ }
21533
+ return {
21534
+ logInfo: logInfo3,
21535
+ logTask,
21536
+ logError: logError2,
21537
+ logDetail: logDetail2,
21538
+ logDebug,
21539
+ logWarning,
21540
+ logColor,
21541
+ setColors,
21542
+ printColors,
21543
+ logHeader,
21544
+ logDataObject: logDataObject2,
21545
+ logErrorObject: logErrorObject2,
21546
+ setLogLevel,
21547
+ setNamespace
21548
+ };
21381
21549
  }
21382
21550
 
21383
21551
  // ../../node_modules/.pnpm/uuid@13.0.0/node_modules/uuid/dist-node/stringify.js
@@ -21434,16 +21602,79 @@ function v4(options, buf, offset) {
21434
21602
  }
21435
21603
  var v4_default = v4;
21436
21604
 
21605
+ // src/logic/cronSchedule.ts
21606
+ function getNextCronRun(intervalMs) {
21607
+ const now = Date.now();
21608
+ const midnight = new Date(now);
21609
+ midnight.setHours(0, 0, 0, 0);
21610
+ const msSinceMidnight = now - midnight.getTime();
21611
+ const nextBoundaryMs = Math.ceil(msSinceMidnight / intervalMs) * intervalMs;
21612
+ let nextRun = midnight.getTime() + nextBoundaryMs;
21613
+ if (nextRun <= now) {
21614
+ nextRun += intervalMs;
21615
+ }
21616
+ return nextRun;
21617
+ }
21618
+
21619
+ // src/logic/pendingEventsStorage.ts
21620
+ import { readFile, unlink, writeFile } from "node:fs/promises";
21621
+ async function readPendingEvents(filePath) {
21622
+ try {
21623
+ const data = await readFile(filePath, "utf-8");
21624
+ return JSON.parse(data);
21625
+ } catch (error) {
21626
+ const err = error;
21627
+ if (err.code === "ENOENT") {
21628
+ return [];
21629
+ }
21630
+ throw error;
21631
+ }
21632
+ }
21633
+ async function writePendingEvents(filePath, events) {
21634
+ await writeFile(filePath, JSON.stringify(events), "utf-8");
21635
+ }
21636
+ async function deletePendingEvents(filePath) {
21637
+ try {
21638
+ await unlink(filePath);
21639
+ } catch (error) {
21640
+ const err = error;
21641
+ if (err.code !== "ENOENT") {
21642
+ throw error;
21643
+ }
21644
+ }
21645
+ }
21646
+
21437
21647
  // src/logic/logClient.ts
21648
+ var { logInfo: logInfo2, logError, logErrorObject, logDetail, logDataObject } = createLoggers2("logging");
21438
21649
  var LogClient = class {
21439
21650
  credentials;
21440
21651
  loggingClient;
21441
- offlinetime;
21442
21652
  cachedEvents = [];
21443
21653
  initialized = false;
21654
+ firstOfflineTime;
21655
+ sendTimeoutId;
21656
+ timeBetweenLogMs;
21657
+ maxOfflineTimeMs;
21658
+ pendingEventsPath;
21659
+ sendInProgress = false;
21660
+ schedulerStarted = false;
21444
21661
  async init() {
21445
- setLogLevel2(process.env.LOG_LEVEL ?? "none");
21446
- logNamespace2("logging");
21662
+ if (typeof this.timeBetweenLogMs !== "number") {
21663
+ const DEFAULT_TIME_BETWEEN_LOG_MS = 10 * 60 * 1e3;
21664
+ const DEFAULT_MAX_OFFLINE_TIME_MS = 60 * 60 * 1e3;
21665
+ const timeBetweenLog = process.env.TIME_BETWEEN_LOG;
21666
+ const maxOfflineTime = process.env.MAX_OFFLINE_TIME;
21667
+ const cachedEventsPath = process.env.CACHED_EVENTS_PATH;
21668
+ if (!cachedEventsPath) {
21669
+ throw new Error("CACHED_EVENTS_PATH must be set");
21670
+ }
21671
+ this.timeBetweenLogMs = Number(timeBetweenLog) || DEFAULT_TIME_BETWEEN_LOG_MS;
21672
+ this.maxOfflineTimeMs = Number(maxOfflineTime) || DEFAULT_MAX_OFFLINE_TIME_MS;
21673
+ this.pendingEventsPath = cachedEventsPath;
21674
+ if (!Number.isFinite(this.timeBetweenLogMs) || this.timeBetweenLogMs <= 0 || !Number.isFinite(this.maxOfflineTimeMs) || this.maxOfflineTimeMs <= 0) {
21675
+ throw new Error("TIME_BETWEEN_LOG and MAX_OFFLINE_TIME must be positive numbers");
21676
+ }
21677
+ }
21447
21678
  const os4 = await import("node:os");
21448
21679
  const path = await import("node:path");
21449
21680
  const fs = await import("node:fs/promises");
@@ -21456,7 +21687,7 @@ var LogClient = class {
21456
21687
  const tokenData = await fs.readFile(tokenFilePath, "utf-8");
21457
21688
  tokenFile = JSON.parse(tokenData);
21458
21689
  logInfo2(`Loaded token from temp file: ${tokenFilePath}`);
21459
- logDetail2(
21690
+ logDetail(
21460
21691
  `Token will expire ${(tokenFile.time + tokenFile.token.expiresIn * 1e3 - Date.now()) / (1e3 * 60)} minutes`
21461
21692
  );
21462
21693
  } catch (_error) {
@@ -21482,18 +21713,81 @@ var LogClient = class {
21482
21713
  } else {
21483
21714
  token = tokenFile.token;
21484
21715
  }
21485
- if (!token) {
21486
- throw new Error("Token not found");
21487
- }
21488
- try {
21716
+ if (token) {
21489
21717
  const loggingApi = process.env.LOGGING_API_URL ?? "";
21490
21718
  if (!loggingApi) {
21491
21719
  throw new Error("LOGGING_API_URL is not set");
21492
21720
  }
21493
21721
  this.loggingClient = new import_gql_logging_client.default(loggingApi, token.accessToken);
21494
21722
  this.initialized = true;
21495
- } catch (error) {
21496
- logError2("Check your internet connection", error);
21723
+ } else {
21724
+ logError("Check your internet connection - token not found");
21725
+ }
21726
+ if (!this.schedulerStarted) {
21727
+ this.schedulerStarted = true;
21728
+ this.scheduleNextRun();
21729
+ }
21730
+ }
21731
+ scheduleNextRun() {
21732
+ if (this.sendTimeoutId !== void 0) {
21733
+ clearTimeout(this.sendTimeoutId);
21734
+ this.sendTimeoutId = void 0;
21735
+ }
21736
+ const nextRun = getNextCronRun(this.timeBetweenLogMs);
21737
+ const delay = Math.max(0, nextRun - Date.now());
21738
+ this.sendTimeoutId = setTimeout(() => {
21739
+ void this.runScheduledSend();
21740
+ }, delay);
21741
+ }
21742
+ async runScheduledSend() {
21743
+ try {
21744
+ await this.sendBatch();
21745
+ } catch {
21746
+ this.sendTimeoutId = void 0;
21747
+ return;
21748
+ }
21749
+ this.scheduleNextRun();
21750
+ }
21751
+ async sendBatch() {
21752
+ if (this.sendInProgress) {
21753
+ return;
21754
+ }
21755
+ this.sendInProgress = true;
21756
+ try {
21757
+ const loaded = await readPendingEvents(this.pendingEventsPath);
21758
+ const batch = [...this.cachedEvents, ...loaded];
21759
+ this.cachedEvents = [];
21760
+ if (batch.length === 0) {
21761
+ this.firstOfflineTime = void 0;
21762
+ return;
21763
+ }
21764
+ if (!this.loggingClient) {
21765
+ if (this.firstOfflineTime === void 0) {
21766
+ this.firstOfflineTime = Date.now();
21767
+ }
21768
+ await writePendingEvents(this.pendingEventsPath, batch);
21769
+ if (Date.now() - this.firstOfflineTime > this.maxOfflineTimeMs) {
21770
+ throw new Error("Exceeded max offline time");
21771
+ }
21772
+ return;
21773
+ }
21774
+ try {
21775
+ await this.loggingClient.mutate({ events: batch }).promise();
21776
+ await deletePendingEvents(this.pendingEventsPath);
21777
+ this.firstOfflineTime = void 0;
21778
+ logInfo2(`Logging batch result: ${batch.length} events sent`);
21779
+ } catch (error) {
21780
+ logError("Send batch failed", error);
21781
+ if (this.firstOfflineTime === void 0) {
21782
+ this.firstOfflineTime = Date.now();
21783
+ }
21784
+ await writePendingEvents(this.pendingEventsPath, batch);
21785
+ if (Date.now() - this.firstOfflineTime > this.maxOfflineTimeMs) {
21786
+ throw new Error("Exceeded max offline time");
21787
+ }
21788
+ }
21789
+ } finally {
21790
+ this.sendInProgress = false;
21497
21791
  }
21498
21792
  }
21499
21793
  async logEvent(event) {
@@ -21510,34 +21804,8 @@ var LogClient = class {
21510
21804
  ...event
21511
21805
  };
21512
21806
  logDataObject("Logging event", newEvent);
21513
- if (!this.loggingClient) {
21514
- logError2("Logging client not initialized");
21515
- if (!this.offlinetime) {
21516
- this.offlinetime = Date.now();
21517
- } else if (Date.now() - this.offlinetime > +(process.env.OFFLINE_TIME ?? 1e3 * 60 * 60)) {
21518
- if (process.env.APP_TYPE === "node") {
21519
- const { writeFile } = await import("node:fs/promises");
21520
- if (!process.env.CACHED_EVENTS_PATH) {
21521
- throw new Error("CACHED_EVENTS_PATH is not set");
21522
- }
21523
- await writeFile(
21524
- `${process.env.CACHED_EVENTS_PATH}-${Date.now()}.json`,
21525
- JSON.stringify(this.cachedEvents)
21526
- );
21527
- logInfo2(`Cached events saved to: ${process.env.CACHED_EVENTS_PATH}`);
21528
- this.offlinetime = Date.now();
21529
- this.cachedEvents = [];
21530
- }
21531
- }
21532
- this.cachedEvents.push(newEvent);
21533
- return;
21534
- }
21535
- const result = await this.loggingClient.mutate({
21536
- events: [newEvent, ...this.cachedEvents]
21537
- }).promise();
21538
- this.cachedEvents = [];
21539
- logInfo2(`Logging event result: ${JSON.stringify(result)}`);
21540
- return result;
21807
+ this.cachedEvents.push(newEvent);
21808
+ return "delayed";
21541
21809
  }
21542
21810
  };
21543
21811
  export {