@rawnodes/logger 2.3.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
@@ -1,3 +1,4 @@
1
+ import { hostname } from 'os';
1
2
  import pino from 'pino';
2
3
  import { AsyncLocalStorage } from 'async_hooks';
3
4
  import { Transform, Writable } from 'stream';
@@ -5,9 +6,9 @@ import { mkdir } from 'fs/promises';
5
6
  import { createStream } from 'rotating-file-stream';
6
7
  import { EventEmitter } from 'events';
7
8
  import { CloudWatchLogsClient, PutLogEventsCommand, CreateLogGroupCommand, CreateLogStreamCommand, DescribeLogStreamsCommand } from '@aws-sdk/client-cloudwatch-logs';
9
+ import { randomUUID } from 'crypto';
8
10
  import { z } from 'zod';
9
11
  import { relative, basename } from 'path';
10
- import { randomUUID } from 'crypto';
11
12
 
12
13
  // src/state.ts
13
14
  var LoggerStore = class {
@@ -415,13 +416,50 @@ var TelegramTransport = class extends BaseHttpTransport {
415
416
  return text.replace(/[&<>"']/g, (c) => entities[c] || c);
416
417
  }
417
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
+ }
418
455
  var CloudWatchTransport = class extends BaseHttpTransport {
419
456
  config;
420
457
  client;
421
458
  sequenceToken;
422
459
  initialized = false;
423
460
  initPromise = null;
424
- constructor(config) {
461
+ resolvedLogStreamName;
462
+ constructor(config, configHostname) {
425
463
  super({
426
464
  batchSize: config.batchSize ?? 100,
427
465
  flushInterval: config.flushInterval ?? 1e3,
@@ -429,6 +467,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
429
467
  retryDelay: config.retryDelay
430
468
  });
431
469
  this.config = config;
470
+ this.resolvedLogStreamName = resolveLogStreamName(config.logStreamName, configHostname);
432
471
  this.client = new CloudWatchLogsClient({
433
472
  region: config.region,
434
473
  credentials: {
@@ -451,7 +490,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
451
490
  logEvents.sort((a, b) => (a.timestamp ?? 0) - (b.timestamp ?? 0));
452
491
  const command = new PutLogEventsCommand({
453
492
  logGroupName: this.config.logGroupName,
454
- logStreamName: this.config.logStreamName,
493
+ logStreamName: this.resolvedLogStreamName,
455
494
  logEvents,
456
495
  sequenceToken: this.sequenceToken
457
496
  });
@@ -463,7 +502,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
463
502
  await this.fetchSequenceToken();
464
503
  const retryCommand = new PutLogEventsCommand({
465
504
  logGroupName: this.config.logGroupName,
466
- logStreamName: this.config.logStreamName,
505
+ logStreamName: this.resolvedLogStreamName,
467
506
  logEvents,
468
507
  sequenceToken: this.sequenceToken
469
508
  });
@@ -509,7 +548,7 @@ var CloudWatchTransport = class extends BaseHttpTransport {
509
548
  await this.client.send(
510
549
  new CreateLogStreamCommand({
511
550
  logGroupName: this.config.logGroupName,
512
- logStreamName: this.config.logStreamName
551
+ logStreamName: this.resolvedLogStreamName
513
552
  })
514
553
  );
515
554
  } catch (error) {
@@ -522,11 +561,11 @@ var CloudWatchTransport = class extends BaseHttpTransport {
522
561
  const response = await this.client.send(
523
562
  new DescribeLogStreamsCommand({
524
563
  logGroupName: this.config.logGroupName,
525
- logStreamNamePrefix: this.config.logStreamName,
564
+ logStreamNamePrefix: this.resolvedLogStreamName,
526
565
  limit: 1
527
566
  })
528
567
  );
529
- const stream = response.logStreams?.find((s) => s.logStreamName === this.config.logStreamName);
568
+ const stream = response.logStreams?.find((s) => s.logStreamName === this.resolvedLogStreamName);
530
569
  this.sequenceToken = stream?.uploadSequenceToken;
531
570
  }
532
571
  isResourceAlreadyExistsError(error) {
@@ -706,7 +745,7 @@ function createStreams(config, store) {
706
745
  });
707
746
  }
708
747
  for (const cloudwatchConfig of toArray(config.cloudwatch)) {
709
- const transport = new CloudWatchTransport(cloudwatchConfig);
748
+ const transport = new CloudWatchTransport(cloudwatchConfig, config.hostname);
710
749
  const cwStream = createHttpTransportStream(transport, cloudwatchConfig.level, cloudwatchConfig.rules, store);
711
750
  streams.push({
712
751
  level: "trace",
@@ -803,8 +842,7 @@ function createState(config, store) {
803
842
  level: "trace",
804
843
  // Accept all, we filter in shouldLog()
805
844
  customLevels: CUSTOM_LEVELS,
806
- base: void 0
807
- // Disable pid and hostname
845
+ base: { hostname: config.hostname ?? hostname() }
808
846
  };
809
847
  const pinoLogger = pino(options, streams);
810
848
  let callerConfig;
@@ -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"),
@@ -955,7 +1011,8 @@ var LoggerConfigSchema = z.object({
955
1011
  discord: z.union([DiscordConfigSchema, z.array(DiscordConfigSchema)]).optional(),
956
1012
  telegram: z.union([TelegramConfigSchema, z.array(TelegramConfigSchema)]).optional(),
957
1013
  cloudwatch: z.union([CloudWatchConfigSchema, z.array(CloudWatchConfigSchema)]).optional(),
958
- caller: z.union([z.boolean(), CallerConfigSchema]).optional()
1014
+ caller: z.union([z.boolean(), CallerConfigSchema]).optional(),
1015
+ hostname: z.string().optional()
959
1016
  });
960
1017
  function validateConfig(config) {
961
1018
  return LoggerConfigSchema.parse(config);
@@ -1380,6 +1437,6 @@ function formatLogfmt(data) {
1380
1437
  return Object.entries(flattened).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${key}=${formatLogfmtValue(value)}`).join(" ");
1381
1438
  }
1382
1439
 
1383
- 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 };
1384
1441
  //# sourceMappingURL=index.mjs.map
1385
1442
  //# sourceMappingURL=index.mjs.map