@rawnodes/logger 2.4.0 → 2.5.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/dist/index.mjs CHANGED
@@ -6,9 +6,9 @@ import { mkdir } from 'fs/promises';
6
6
  import { createStream } from 'rotating-file-stream';
7
7
  import { EventEmitter } from 'events';
8
8
  import { CloudWatchLogsClient, PutLogEventsCommand, CreateLogGroupCommand, CreateLogStreamCommand, DescribeLogStreamsCommand } from '@aws-sdk/client-cloudwatch-logs';
9
+ import { randomUUID } from 'crypto';
9
10
  import { z } from 'zod';
10
11
  import { relative, basename } from 'path';
11
- import { randomUUID } from 'crypto';
12
12
 
13
13
  // src/state.ts
14
14
  var LoggerStore = class {
@@ -416,13 +416,50 @@ var TelegramTransport = class extends BaseHttpTransport {
416
416
  return text.replace(/[&<>"']/g, (c) => entities[c] || c);
417
417
  }
418
418
  };
419
+ var instanceUuid = randomUUID().slice(0, 8);
420
+ function formatDate() {
421
+ return (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
422
+ }
423
+ function formatDateTime() {
424
+ return (/* @__PURE__ */ new Date()).toISOString().slice(0, 19).replace(/[T:]/g, "-");
425
+ }
426
+ function resolveLogStreamName(config, configHostname) {
427
+ const hostname$1 = configHostname || process.env.HOSTNAME || hostname();
428
+ if (!config) {
429
+ return hostname$1;
430
+ }
431
+ if (typeof config === "string") {
432
+ return config;
433
+ }
434
+ if ("pattern" in config) {
435
+ switch (config.pattern) {
436
+ case "hostname":
437
+ return hostname$1;
438
+ case "hostname-date":
439
+ return `${hostname$1}/${formatDate()}`;
440
+ case "hostname-uuid":
441
+ return `${hostname$1}-${instanceUuid}`;
442
+ case "date":
443
+ return formatDate();
444
+ case "uuid":
445
+ return randomUUID();
446
+ default:
447
+ return hostname$1;
448
+ }
449
+ }
450
+ if ("template" in config) {
451
+ return config.template.replace(/\{hostname\}/g, hostname$1).replace(/\{date\}/g, formatDate()).replace(/\{datetime\}/g, formatDateTime()).replace(/\{uuid\}/g, instanceUuid).replace(/\{pid\}/g, String(process.pid)).replace(/\{env\}/g, process.env.NODE_ENV || "development");
452
+ }
453
+ return hostname$1;
454
+ }
419
455
  var CloudWatchTransport = class extends BaseHttpTransport {
420
456
  config;
421
457
  client;
422
458
  sequenceToken;
423
459
  initialized = false;
424
460
  initPromise = null;
425
- constructor(config) {
461
+ resolvedLogStreamName;
462
+ constructor(config, configHostname) {
426
463
  super({
427
464
  batchSize: config.batchSize ?? 100,
428
465
  flushInterval: config.flushInterval ?? 1e3,
@@ -430,6 +467,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
430
467
  retryDelay: config.retryDelay
431
468
  });
432
469
  this.config = config;
470
+ this.resolvedLogStreamName = resolveLogStreamName(config.logStreamName, configHostname);
433
471
  this.client = new CloudWatchLogsClient({
434
472
  region: config.region,
435
473
  credentials: {
@@ -452,7 +490,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
452
490
  logEvents.sort((a, b) => (a.timestamp ?? 0) - (b.timestamp ?? 0));
453
491
  const command = new PutLogEventsCommand({
454
492
  logGroupName: this.config.logGroupName,
455
- logStreamName: this.config.logStreamName,
493
+ logStreamName: this.resolvedLogStreamName,
456
494
  logEvents,
457
495
  sequenceToken: this.sequenceToken
458
496
  });
@@ -464,7 +502,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
464
502
  await this.fetchSequenceToken();
465
503
  const retryCommand = new PutLogEventsCommand({
466
504
  logGroupName: this.config.logGroupName,
467
- logStreamName: this.config.logStreamName,
505
+ logStreamName: this.resolvedLogStreamName,
468
506
  logEvents,
469
507
  sequenceToken: this.sequenceToken
470
508
  });
@@ -510,7 +548,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
510
548
  await this.client.send(
511
549
  new CreateLogStreamCommand({
512
550
  logGroupName: this.config.logGroupName,
513
- logStreamName: this.config.logStreamName
551
+ logStreamName: this.resolvedLogStreamName
514
552
  })
515
553
  );
516
554
  } catch (error) {
@@ -523,11 +561,11 @@ var CloudWatchTransport = class extends BaseHttpTransport {
523
561
  const response = await this.client.send(
524
562
  new DescribeLogStreamsCommand({
525
563
  logGroupName: this.config.logGroupName,
526
- logStreamNamePrefix: this.config.logStreamName,
564
+ logStreamNamePrefix: this.resolvedLogStreamName,
527
565
  limit: 1
528
566
  })
529
567
  );
530
- const stream = response.logStreams?.find((s) => s.logStreamName === this.config.logStreamName);
568
+ const stream = response.logStreams?.find((s) => s.logStreamName === this.resolvedLogStreamName);
531
569
  this.sequenceToken = stream?.uploadSequenceToken;
532
570
  }
533
571
  isResourceAlreadyExistsError(error) {
@@ -707,7 +745,7 @@ function createStreams(config, store) {
707
745
  });
708
746
  }
709
747
  for (const cloudwatchConfig of toArray(config.cloudwatch)) {
710
- const transport = new CloudWatchTransport(cloudwatchConfig);
748
+ const transport = new CloudWatchTransport(cloudwatchConfig, config.hostname);
711
749
  const cwStream = createHttpTransportStream(transport, cloudwatchConfig.level, cloudwatchConfig.rules, store);
712
750
  streams.push({
713
751
  level: "trace",
@@ -921,6 +959,24 @@ var TelegramConfigSchema = z.object({
921
959
  threadId: z.number().int().optional(),
922
960
  replyToMessageId: z.number().int().optional()
923
961
  });
962
+ var LogStreamPatternSchema = z.enum([
963
+ "hostname",
964
+ "hostname-date",
965
+ "hostname-uuid",
966
+ "date",
967
+ "uuid"
968
+ ]);
969
+ var LogStreamPatternConfigSchema = z.object({
970
+ pattern: LogStreamPatternSchema
971
+ });
972
+ var LogStreamTemplateConfigSchema = z.object({
973
+ template: z.string().min(1, "template is required")
974
+ });
975
+ var LogStreamNameSchema = z.union([
976
+ z.string().min(1, "logStreamName string must not be empty"),
977
+ LogStreamPatternConfigSchema,
978
+ LogStreamTemplateConfigSchema
979
+ ]);
924
980
  var CloudWatchConfigSchema = z.object({
925
981
  level: LogLevelSchema.optional(),
926
982
  rules: z.array(LevelRuleSchema).optional(),
@@ -929,7 +985,7 @@ var CloudWatchConfigSchema = z.object({
929
985
  maxRetries: z.number().int().nonnegative().optional(),
930
986
  retryDelay: z.number().int().positive().optional(),
931
987
  logGroupName: z.string().min(1, "logGroupName is required"),
932
- logStreamName: z.string().min(1, "logStreamName is required"),
988
+ logStreamName: LogStreamNameSchema.optional(),
933
989
  region: z.string().min(1, "region is required"),
934
990
  accessKeyId: z.string().min(1, "accessKeyId is required"),
935
991
  secretAccessKey: z.string().min(1, "secretAccessKey is required"),
@@ -1381,6 +1437,6 @@ function formatLogfmt(data) {
1381
1437
  return Object.entries(flattened).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${key}=${formatLogfmtValue(value)}`).join(" ");
1382
1438
  }
1383
1439
 
1384
- export { BaseHttpTransport, CallerConfigSchema, CloudWatchConfigSchema, CloudWatchTransport, ConsoleConfigSchema, DiscordConfigSchema, DiscordTransport, FileConfigSchema, HttpTransportBaseConfigSchema, LOG_LEVELS, LevelConfigObjectSchema, LevelConfigSchema, LevelRuleSchema, LogFormatSchema, LogLevelSchema, Logger, LoggerConfigSchema, LoggerStore, MessageBuffer, TelegramConfigSchema, TelegramTransport, assertLogLevel, createMasker, createSingletonLogger, extractRequestId, flattenObject, formatCallerInfo, formatLogfmt, formatLogfmtValue, generateRequestId, getCallerInfo, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync, safeValidateConfig, validateConfig };
1440
+ export { BaseHttpTransport, CallerConfigSchema, CloudWatchConfigSchema, CloudWatchTransport, ConsoleConfigSchema, DiscordConfigSchema, DiscordTransport, FileConfigSchema, HttpTransportBaseConfigSchema, LOG_LEVELS, LevelConfigObjectSchema, LevelConfigSchema, LevelRuleSchema, LogFormatSchema, LogLevelSchema, LogStreamNameSchema, LogStreamPatternConfigSchema, LogStreamPatternSchema, LogStreamTemplateConfigSchema, Logger, LoggerConfigSchema, LoggerStore, MessageBuffer, TelegramConfigSchema, TelegramTransport, assertLogLevel, createMasker, createSingletonLogger, extractRequestId, flattenObject, formatCallerInfo, formatLogfmt, formatLogfmtValue, generateRequestId, getCallerInfo, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync, safeValidateConfig, validateConfig };
1385
1441
  //# sourceMappingURL=index.mjs.map
1386
1442
  //# sourceMappingURL=index.mjs.map