@rawnodes/logger 2.6.0 → 2.7.1

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
@@ -596,7 +596,13 @@ function shouldPassTransport(log, level, rules, store) {
596
596
  const logLevel = getLevelName(log.level);
597
597
  const context = log.context;
598
598
  const storeContext = store.getStore();
599
- const matchingRule = rules?.find((rule) => matchesContext(storeContext, context, rule.match));
599
+ const logMeta = {};
600
+ for (const [key, value] of Object.entries(log)) {
601
+ if (!["level", "time", "msg", "context"].includes(key)) {
602
+ logMeta[key] = value;
603
+ }
604
+ }
605
+ const matchingRule = rules?.find((rule) => matchesContext(storeContext, context, rule.match, logMeta));
600
606
  const effectiveLevel = matchingRule?.level ?? level ?? "silly";
601
607
  if (effectiveLevel === "off") return false;
602
608
  return LOG_LEVELS[logLevel] <= LOG_LEVELS[effectiveLevel];
@@ -893,8 +899,8 @@ function getEffectiveLevel(state, loggerContext) {
893
899
  }
894
900
  return state.defaultLevel;
895
901
  }
896
- function matchesContext(storeContext, loggerContext, match) {
897
- const combined = { ...storeContext, context: loggerContext };
902
+ function matchesContext(storeContext, loggerContext, match, logMeta) {
903
+ const combined = { ...logMeta, ...storeContext, context: loggerContext };
898
904
  return Object.entries(match).every(([key, value]) => combined[key] === value);
899
905
  }
900
906
  var LogLevelSchema = z.enum([
@@ -1141,6 +1147,129 @@ function registerShutdown(logger, options = {}) {
1141
1147
  return shutdown;
1142
1148
  }
1143
1149
 
1150
+ // src/utils/error.ts
1151
+ function isAxiosError(error) {
1152
+ return error !== null && typeof error === "object" && "isAxiosError" in error && error.isAxiosError === true;
1153
+ }
1154
+ function sanitizeResponseData(data, maxStringLength = 1e4) {
1155
+ if (data === null || data === void 0) {
1156
+ return data;
1157
+ }
1158
+ if (typeof data === "string") {
1159
+ return data.length > maxStringLength ? `${data.slice(0, maxStringLength)}...[truncated]` : data;
1160
+ }
1161
+ if (typeof data === "number" || typeof data === "boolean") {
1162
+ return data;
1163
+ }
1164
+ if (Array.isArray(data)) {
1165
+ return data.slice(0, 100).map((item) => sanitizeResponseData(item, maxStringLength));
1166
+ }
1167
+ if (typeof data === "object") {
1168
+ const result = {};
1169
+ const entries = Object.entries(data);
1170
+ for (const [key, value] of entries.slice(0, 50)) {
1171
+ result[key] = sanitizeResponseData(value, maxStringLength);
1172
+ }
1173
+ return result;
1174
+ }
1175
+ return String(data);
1176
+ }
1177
+ function extractAxiosHttpData(error) {
1178
+ const httpData = {};
1179
+ if (error.code) {
1180
+ httpData.code = error.code;
1181
+ }
1182
+ if (error.response) {
1183
+ httpData.status = error.response.status;
1184
+ httpData.statusText = error.response.statusText;
1185
+ if (error.response.data !== void 0) {
1186
+ httpData.responseData = sanitizeResponseData(error.response.data);
1187
+ }
1188
+ }
1189
+ if (error.config) {
1190
+ const { url, baseURL, method } = error.config;
1191
+ if (url) {
1192
+ httpData.url = baseURL && !url.startsWith("http") ? `${baseURL}${url}` : url;
1193
+ }
1194
+ if (method) {
1195
+ httpData.method = method.toUpperCase();
1196
+ }
1197
+ }
1198
+ return httpData;
1199
+ }
1200
+ function extractGenericHttpData(error) {
1201
+ const httpData = {};
1202
+ let hasData = false;
1203
+ if (typeof error.code === "string") {
1204
+ httpData.code = error.code;
1205
+ hasData = true;
1206
+ }
1207
+ const response = error.response;
1208
+ if (response && typeof response === "object") {
1209
+ if (typeof response.status === "number") {
1210
+ httpData.status = response.status;
1211
+ hasData = true;
1212
+ }
1213
+ if (typeof response.statusText === "string") {
1214
+ httpData.statusText = response.statusText;
1215
+ hasData = true;
1216
+ }
1217
+ if (response.data !== void 0) {
1218
+ httpData.responseData = sanitizeResponseData(response.data);
1219
+ hasData = true;
1220
+ }
1221
+ }
1222
+ const config = error.config;
1223
+ if (config && typeof config === "object") {
1224
+ const url = config.url;
1225
+ const baseURL = config.baseURL;
1226
+ if (typeof url === "string") {
1227
+ httpData.url = baseURL && !url.startsWith("http") ? `${baseURL}${url}` : url;
1228
+ hasData = true;
1229
+ }
1230
+ if (typeof config.method === "string") {
1231
+ httpData.method = config.method.toUpperCase();
1232
+ hasData = true;
1233
+ }
1234
+ }
1235
+ return hasData ? httpData : void 0;
1236
+ }
1237
+ function serializeError(error) {
1238
+ if (!(error instanceof Error)) {
1239
+ if (error === null || error === void 0) {
1240
+ return { errorMessage: "Unknown error" };
1241
+ }
1242
+ if (typeof error === "string") {
1243
+ return { errorMessage: error };
1244
+ }
1245
+ const httpData2 = typeof error === "object" ? extractGenericHttpData(error) : void 0;
1246
+ return {
1247
+ errorMessage: String(error),
1248
+ http: httpData2
1249
+ };
1250
+ }
1251
+ const serialized = {
1252
+ errorMessage: error.message,
1253
+ stack: error.stack
1254
+ };
1255
+ if (error.name && error.name !== "Error") {
1256
+ serialized.errorName = error.name;
1257
+ }
1258
+ if (isAxiosError(error)) {
1259
+ serialized.http = extractAxiosHttpData(error);
1260
+ return serialized;
1261
+ }
1262
+ const errWithCode = error;
1263
+ if (typeof errWithCode.code === "string") {
1264
+ serialized.code = errWithCode.code;
1265
+ }
1266
+ const httpData = extractGenericHttpData(error);
1267
+ if (httpData) {
1268
+ serialized.http = httpData;
1269
+ }
1270
+ return serialized;
1271
+ }
1272
+
1144
1273
  // src/logger.ts
1145
1274
  var Logger = class _Logger {
1146
1275
  constructor(state, context) {
@@ -1269,11 +1398,19 @@ var Logger = class _Logger {
1269
1398
  logMeta.caller = formatCallerInfo(callerInfo);
1270
1399
  }
1271
1400
  }
1272
- if (error instanceof Error) {
1273
- logMeta.errorMessage = error.message;
1274
- logMeta.stack = error.stack;
1275
- } else if (error !== void 0) {
1276
- logMeta.error = error;
1401
+ if (error !== void 0) {
1402
+ const serialized = serializeError(error);
1403
+ logMeta.errorMessage = serialized.errorMessage;
1404
+ logMeta.stack = serialized.stack;
1405
+ if (serialized.errorName) {
1406
+ logMeta.errorName = serialized.errorName;
1407
+ }
1408
+ if (serialized.code) {
1409
+ logMeta.errorCode = serialized.code;
1410
+ }
1411
+ if (serialized.http) {
1412
+ logMeta.http = serialized.http;
1413
+ }
1277
1414
  }
1278
1415
  const pinoMethod = this.getPinoMethod(level);
1279
1416
  pinoMethod.call(this.state.pino, logMeta, message);
@@ -1523,6 +1660,6 @@ function formatLogfmt(data) {
1523
1660
  return Object.entries(flattened).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${key}=${formatLogfmtValue(value)}`).join(" ");
1524
1661
  }
1525
1662
 
1526
- export { AutoShutdownConfigSchema, 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, registerShutdown, safeValidateConfig, validateConfig };
1663
+ export { AutoShutdownConfigSchema, 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, registerShutdown, safeValidateConfig, serializeError, validateConfig };
1527
1664
  //# sourceMappingURL=index.mjs.map
1528
1665
  //# sourceMappingURL=index.mjs.map