@rawnodes/logger 2.1.0 → 2.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/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
@@ -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) {
@@ -935,13 +943,18 @@ var LevelConfigSchema = z.union([
935
943
  LogLevelSchema,
936
944
  LevelConfigObjectSchema
937
945
  ]);
946
+ var CallerConfigSchema = z.object({
947
+ depth: z.number().int().nonnegative().optional(),
948
+ fullPath: z.boolean().optional()
949
+ });
938
950
  var LoggerConfigSchema = z.object({
939
951
  level: LevelConfigSchema,
940
952
  console: ConsoleConfigSchema,
941
953
  file: FileConfigSchema.optional(),
942
954
  discord: z.union([DiscordConfigSchema, z.array(DiscordConfigSchema)]).optional(),
943
955
  telegram: z.union([TelegramConfigSchema, z.array(TelegramConfigSchema)]).optional(),
944
- cloudwatch: z.union([CloudWatchConfigSchema, z.array(CloudWatchConfigSchema)]).optional()
956
+ cloudwatch: z.union([CloudWatchConfigSchema, z.array(CloudWatchConfigSchema)]).optional(),
957
+ caller: z.union([z.boolean(), CallerConfigSchema]).optional()
945
958
  });
946
959
  function validateConfig(config) {
947
960
  return LoggerConfigSchema.parse(config);
@@ -949,6 +962,66 @@ function validateConfig(config) {
949
962
  function safeValidateConfig(config) {
950
963
  return LoggerConfigSchema.safeParse(config);
951
964
  }
965
+ var BASE_STACK_OFFSET = 4;
966
+ var STACK_LINE_REGEX = /^\s*at\s+(?:(.+?)\s+\()?(.+):(\d+):(\d+)\)?$/;
967
+ function getCallerInfo(config, additionalOffset = 0) {
968
+ const depth = config.depth ?? 0;
969
+ const targetFrame = BASE_STACK_OFFSET + depth + additionalOffset;
970
+ const originalPrepare = Error.prepareStackTrace;
971
+ const originalLimit = Error.stackTraceLimit;
972
+ try {
973
+ Error.stackTraceLimit = targetFrame + 5;
974
+ const err = new Error();
975
+ const stack = err.stack;
976
+ if (!stack) {
977
+ return void 0;
978
+ }
979
+ const lines = stack.split("\n");
980
+ const targetLine = lines[targetFrame];
981
+ if (!targetLine) {
982
+ return void 0;
983
+ }
984
+ const match = targetLine.match(STACK_LINE_REGEX);
985
+ if (!match) {
986
+ return void 0;
987
+ }
988
+ const [, fnName, filePath, lineStr, colStr] = match;
989
+ const line = parseInt(lineStr, 10);
990
+ const column = parseInt(colStr, 10);
991
+ let file = filePath;
992
+ if (file.startsWith("file://")) {
993
+ file = file.slice(7);
994
+ }
995
+ if (!config.fullPath) {
996
+ try {
997
+ const relativePath = relative(process.cwd(), file);
998
+ if (relativePath.startsWith("..") && relativePath.split("/").filter((p) => p === "..").length > 2) {
999
+ file = basename(file);
1000
+ } else {
1001
+ file = relativePath;
1002
+ }
1003
+ } catch {
1004
+ file = basename(file);
1005
+ }
1006
+ }
1007
+ return {
1008
+ file,
1009
+ line,
1010
+ column: isNaN(column) ? void 0 : column,
1011
+ function: fnName && fnName !== "<anonymous>" ? fnName : void 0
1012
+ };
1013
+ } finally {
1014
+ Error.prepareStackTrace = originalPrepare;
1015
+ Error.stackTraceLimit = originalLimit;
1016
+ }
1017
+ }
1018
+ function formatCallerInfo(info) {
1019
+ const location = `${info.file}:${info.line}`;
1020
+ if (info.function) {
1021
+ return `${info.function} (${location})`;
1022
+ }
1023
+ return location;
1024
+ }
952
1025
 
953
1026
  // src/logger.ts
954
1027
  var Logger = class _Logger {
@@ -1051,13 +1124,19 @@ var Logger = class _Logger {
1051
1124
  this.log("silly", message, meta);
1052
1125
  }
1053
1126
  // Private
1054
- log(level, message, meta, error) {
1127
+ log(level, message, meta, error, callerOffset = 0) {
1055
1128
  const resolved = typeof meta === "function" ? meta() : meta;
1056
1129
  const logMeta = { context: this.context, ...resolved };
1057
1130
  const storeContext = this.state.store.getStore();
1058
1131
  if (storeContext) {
1059
1132
  Object.assign(logMeta, storeContext);
1060
1133
  }
1134
+ if (this.state.callerConfig) {
1135
+ const callerInfo = getCallerInfo(this.state.callerConfig, callerOffset);
1136
+ if (callerInfo) {
1137
+ logMeta.caller = formatCallerInfo(callerInfo);
1138
+ }
1139
+ }
1061
1140
  if (error instanceof Error) {
1062
1141
  logMeta.errorMessage = error.message;
1063
1142
  logMeta.stack = error.stack;
@@ -1300,6 +1379,6 @@ function formatLogfmt(data) {
1300
1379
  return Object.entries(flattened).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${key}=${formatLogfmtValue(value)}`).join(" ");
1301
1380
  }
1302
1381
 
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 };
1382
+ 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
1383
  //# sourceMappingURL=index.mjs.map
1305
1384
  //# sourceMappingURL=index.mjs.map