@rawnodes/logger 2.1.0 → 2.3.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,6 +6,7 @@ import { createStream } from 'rotating-file-stream';
6
6
  import { EventEmitter } from 'events';
7
7
  import { CloudWatchLogsClient, PutLogEventsCommand, CreateLogGroupCommand, CreateLogStreamCommand, DescribeLogStreamsCommand } from '@aws-sdk/client-cloudwatch-logs';
8
8
  import { z } from 'zod';
9
+ import { relative, basename } from 'path';
9
10
  import { randomUUID } from 'crypto';
10
11
 
11
12
  // src/state.ts
@@ -671,7 +672,7 @@ function createStreams(config, store) {
671
672
  {
672
673
  path: fileConfig.dirname,
673
674
  size: normalizeSize(fileConfig.maxSize || "20M"),
674
- interval: fileConfig.datePattern === "YYYY-MM-DD-HH" ? "1h" : "1d",
675
+ interval: fileConfig.interval || (fileConfig.datePattern === "YYYY-MM-DD-HH" ? "1h" : "1d"),
675
676
  compress: fileConfig.zippedArchive ? "gzip" : false,
676
677
  maxFiles: parseInt(fileConfig.maxFiles?.replace("d", "") || "14")
677
678
  }
@@ -806,13 +807,20 @@ function createState(config, store) {
806
807
  // Disable pid and hostname
807
808
  };
808
809
  const pinoLogger = pino(options, streams);
810
+ let callerConfig;
811
+ if (config.caller === true) {
812
+ callerConfig = {};
813
+ } else if (config.caller && typeof config.caller === "object") {
814
+ callerConfig = config.caller;
815
+ }
809
816
  return {
810
817
  pino: pinoLogger,
811
818
  store: loggerStore,
812
819
  defaultLevel,
813
820
  levelOverrides,
814
821
  contextIndex,
815
- complexRules
822
+ complexRules,
823
+ callerConfig
816
824
  };
817
825
  }
818
826
  function rebuildIndexes(state) {
@@ -870,6 +878,7 @@ var FileConfigSchema = z.object({
870
878
  dirname: z.string().min(1, "dirname is required"),
871
879
  filename: z.string().min(1, "filename is required"),
872
880
  datePattern: z.string().optional(),
881
+ interval: z.string().regex(/^\d+[Mdhms]$/).optional(),
873
882
  zippedArchive: z.boolean().optional(),
874
883
  maxSize: z.string().optional(),
875
884
  maxFiles: z.string().optional()
@@ -935,13 +944,18 @@ var LevelConfigSchema = z.union([
935
944
  LogLevelSchema,
936
945
  LevelConfigObjectSchema
937
946
  ]);
947
+ var CallerConfigSchema = z.object({
948
+ depth: z.number().int().nonnegative().optional(),
949
+ fullPath: z.boolean().optional()
950
+ });
938
951
  var LoggerConfigSchema = z.object({
939
952
  level: LevelConfigSchema,
940
953
  console: ConsoleConfigSchema,
941
954
  file: FileConfigSchema.optional(),
942
955
  discord: z.union([DiscordConfigSchema, z.array(DiscordConfigSchema)]).optional(),
943
956
  telegram: z.union([TelegramConfigSchema, z.array(TelegramConfigSchema)]).optional(),
944
- cloudwatch: z.union([CloudWatchConfigSchema, z.array(CloudWatchConfigSchema)]).optional()
957
+ cloudwatch: z.union([CloudWatchConfigSchema, z.array(CloudWatchConfigSchema)]).optional(),
958
+ caller: z.union([z.boolean(), CallerConfigSchema]).optional()
945
959
  });
946
960
  function validateConfig(config) {
947
961
  return LoggerConfigSchema.parse(config);
@@ -949,6 +963,66 @@ function validateConfig(config) {
949
963
  function safeValidateConfig(config) {
950
964
  return LoggerConfigSchema.safeParse(config);
951
965
  }
966
+ var BASE_STACK_OFFSET = 4;
967
+ var STACK_LINE_REGEX = /^\s*at\s+(?:(.+?)\s+\()?(.+):(\d+):(\d+)\)?$/;
968
+ function getCallerInfo(config, additionalOffset = 0) {
969
+ const depth = config.depth ?? 0;
970
+ const targetFrame = BASE_STACK_OFFSET + depth + additionalOffset;
971
+ const originalPrepare = Error.prepareStackTrace;
972
+ const originalLimit = Error.stackTraceLimit;
973
+ try {
974
+ Error.stackTraceLimit = targetFrame + 5;
975
+ const err = new Error();
976
+ const stack = err.stack;
977
+ if (!stack) {
978
+ return void 0;
979
+ }
980
+ const lines = stack.split("\n");
981
+ const targetLine = lines[targetFrame];
982
+ if (!targetLine) {
983
+ return void 0;
984
+ }
985
+ const match = targetLine.match(STACK_LINE_REGEX);
986
+ if (!match) {
987
+ return void 0;
988
+ }
989
+ const [, fnName, filePath, lineStr, colStr] = match;
990
+ const line = parseInt(lineStr, 10);
991
+ const column = parseInt(colStr, 10);
992
+ let file = filePath;
993
+ if (file.startsWith("file://")) {
994
+ file = file.slice(7);
995
+ }
996
+ if (!config.fullPath) {
997
+ try {
998
+ const relativePath = relative(process.cwd(), file);
999
+ if (relativePath.startsWith("..") && relativePath.split("/").filter((p) => p === "..").length > 2) {
1000
+ file = basename(file);
1001
+ } else {
1002
+ file = relativePath;
1003
+ }
1004
+ } catch {
1005
+ file = basename(file);
1006
+ }
1007
+ }
1008
+ return {
1009
+ file,
1010
+ line,
1011
+ column: isNaN(column) ? void 0 : column,
1012
+ function: fnName && fnName !== "<anonymous>" ? fnName : void 0
1013
+ };
1014
+ } finally {
1015
+ Error.prepareStackTrace = originalPrepare;
1016
+ Error.stackTraceLimit = originalLimit;
1017
+ }
1018
+ }
1019
+ function formatCallerInfo(info) {
1020
+ const location = `${info.file}:${info.line}`;
1021
+ if (info.function) {
1022
+ return `${info.function} (${location})`;
1023
+ }
1024
+ return location;
1025
+ }
952
1026
 
953
1027
  // src/logger.ts
954
1028
  var Logger = class _Logger {
@@ -1051,13 +1125,19 @@ var Logger = class _Logger {
1051
1125
  this.log("silly", message, meta);
1052
1126
  }
1053
1127
  // Private
1054
- log(level, message, meta, error) {
1128
+ log(level, message, meta, error, callerOffset = 0) {
1055
1129
  const resolved = typeof meta === "function" ? meta() : meta;
1056
1130
  const logMeta = { context: this.context, ...resolved };
1057
1131
  const storeContext = this.state.store.getStore();
1058
1132
  if (storeContext) {
1059
1133
  Object.assign(logMeta, storeContext);
1060
1134
  }
1135
+ if (this.state.callerConfig) {
1136
+ const callerInfo = getCallerInfo(this.state.callerConfig, callerOffset);
1137
+ if (callerInfo) {
1138
+ logMeta.caller = formatCallerInfo(callerInfo);
1139
+ }
1140
+ }
1061
1141
  if (error instanceof Error) {
1062
1142
  logMeta.errorMessage = error.message;
1063
1143
  logMeta.stack = error.stack;
@@ -1300,6 +1380,6 @@ function formatLogfmt(data) {
1300
1380
  return Object.entries(flattened).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${key}=${formatLogfmtValue(value)}`).join(" ");
1301
1381
  }
1302
1382
 
1303
- export { BaseHttpTransport, 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, formatLogfmt, formatLogfmtValue, generateRequestId, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync, safeValidateConfig, validateConfig };
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 };
1304
1384
  //# sourceMappingURL=index.mjs.map
1305
1385
  //# sourceMappingURL=index.mjs.map