@rawnodes/logger 2.7.2 → 2.9.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
@@ -494,6 +494,112 @@ var TelegramTransport = class extends BaseHttpTransport {
494
494
  return text.replace(/[&<>"']/g, (c) => entities[c] || c);
495
495
  }
496
496
  };
497
+
498
+ // src/transports/zoho-cliq.ts
499
+ var LEVEL_EMOJI3 = {
500
+ off: "",
501
+ error: "\u{1F534}",
502
+ warn: "\u{1F7E1}",
503
+ info: "\u{1F7E2}",
504
+ http: "\u{1F535}",
505
+ verbose: "\u{1F7E3}",
506
+ debug: "\u26AA",
507
+ silly: "\u26AB"
508
+ };
509
+ var DEFAULT_REQUEST_TIMEOUT_MS3 = 1e4;
510
+ var MAX_MESSAGE_LENGTH = 9500;
511
+ var ZohoCliqTransport = class extends BaseHttpTransport {
512
+ config;
513
+ endpoint;
514
+ requestTimeout;
515
+ constructor(config) {
516
+ super({
517
+ batchSize: config.batchSize ?? 20,
518
+ flushInterval: config.flushInterval ?? 2e3,
519
+ maxRetries: config.maxRetries,
520
+ retryDelay: config.retryDelay,
521
+ maxQueueSize: config.maxQueueSize,
522
+ dropPolicy: config.dropPolicy,
523
+ onDrop: config.onDrop,
524
+ onError: config.onError
525
+ });
526
+ this.config = config;
527
+ this.requestTimeout = config.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT_MS3;
528
+ this.endpoint = this.buildEndpoint();
529
+ }
530
+ buildEndpoint() {
531
+ if (this.config.webhookUrl) {
532
+ return this.config.webhookUrl;
533
+ }
534
+ const region = this.config.region ?? "eu";
535
+ const companyId = this.config.companyId;
536
+ const channel = encodeURIComponent(this.config.channel);
537
+ return `https://cliq.zoho.${region}/company/${companyId}/api/v2/channelsbyname/${channel}/message`;
538
+ }
539
+ async sendBatch(messages) {
540
+ const text = messages.map((msg) => this.formatMessage(msg)).join("\n\n---\n\n");
541
+ for (const chunk of this.splitContent(text, MAX_MESSAGE_LENGTH)) {
542
+ await this.post(chunk);
543
+ }
544
+ }
545
+ formatMessage(msg) {
546
+ const emoji = LEVEL_EMOJI3[msg.level];
547
+ const level = msg.level.toUpperCase();
548
+ const context = msg.context || "APP";
549
+ const timestamp = this.config.includeTimestamp !== false ? ` ${msg.timestamp.toISOString()}` : "";
550
+ let text = `${emoji} *${level}* [${context}]${timestamp}
551
+ ${msg.message}`;
552
+ if (this.config.includeMeta !== false && msg.meta && Object.keys(msg.meta).length > 0) {
553
+ text += "\n```\n" + JSON.stringify(msg.meta, null, 2) + "\n```";
554
+ }
555
+ return text;
556
+ }
557
+ splitContent(content, maxLength) {
558
+ if (content.length <= maxLength) return [content];
559
+ const chunks = [];
560
+ let current = content;
561
+ while (current.length > 0) {
562
+ if (current.length <= maxLength) {
563
+ chunks.push(current);
564
+ break;
565
+ }
566
+ let splitAt = current.lastIndexOf("\n", maxLength);
567
+ if (splitAt === -1 || splitAt < maxLength / 2) {
568
+ splitAt = current.lastIndexOf(" ", maxLength);
569
+ }
570
+ if (splitAt === -1 || splitAt < maxLength / 2) {
571
+ splitAt = maxLength;
572
+ }
573
+ chunks.push(current.slice(0, splitAt));
574
+ current = current.slice(splitAt).trimStart();
575
+ }
576
+ return chunks;
577
+ }
578
+ async post(text) {
579
+ const url = new URL(this.endpoint);
580
+ url.searchParams.set("zapikey", this.config.apiKey);
581
+ if (this.config.bot?.name) {
582
+ url.searchParams.set("bot_unique_name", this.config.bot.name);
583
+ }
584
+ const body = {
585
+ text,
586
+ broadcast: this.config.broadcast ?? true
587
+ };
588
+ if (this.config.bot) {
589
+ body.bot = this.config.bot;
590
+ }
591
+ const response = await fetch(url.toString(), {
592
+ method: "POST",
593
+ headers: { "Content-Type": "application/json" },
594
+ body: JSON.stringify(body),
595
+ signal: AbortSignal.timeout(this.requestTimeout)
596
+ });
597
+ if (!response.ok && response.status !== 204) {
598
+ const respText = await response.text().catch(() => "");
599
+ throw new Error(`Zoho Cliq API failed: ${response.status} ${respText}`);
600
+ }
601
+ }
602
+ };
497
603
  var instanceUuid = randomUUID().slice(0, 8);
498
604
  function formatDate() {
499
605
  return (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
@@ -677,13 +783,27 @@ function getLevelName(levelNum) {
677
783
  if (levelNum >= 20) return "debug";
678
784
  return "silly";
679
785
  }
680
- function shouldPassTransport(log, level, rules, state) {
786
+ var DEFAULT_RESPECTS_OVERRIDES = {
787
+ console: true,
788
+ file: true,
789
+ cloudwatch: true,
790
+ relay: true,
791
+ discord: false,
792
+ telegram: false,
793
+ zohoCliq: false
794
+ };
795
+ function resolveRespectsOverrides(kind, configFlag) {
796
+ return configFlag ?? DEFAULT_RESPECTS_OVERRIDES[kind];
797
+ }
798
+ function shouldPassTransport(log, level, rules, state, respectOverrides) {
681
799
  const logLevel = getLevelName(log.level);
682
- const embeddedOverride = log.__or;
683
- if (typeof embeddedOverride === "string") {
684
- if (embeddedOverride === "off") return false;
685
- const overrideLevel = embeddedOverride;
686
- return LOG_LEVELS[logLevel] <= LOG_LEVELS[overrideLevel];
800
+ if (respectOverrides) {
801
+ const embeddedOverride = log.__or;
802
+ if (typeof embeddedOverride === "string") {
803
+ if (embeddedOverride === "off") return false;
804
+ const overrideLevel = embeddedOverride;
805
+ return LOG_LEVELS[logLevel] <= LOG_LEVELS[overrideLevel];
806
+ }
687
807
  }
688
808
  const context = log.context;
689
809
  if (rules && rules.length > 0) {
@@ -762,7 +882,7 @@ function formatLog(log, format, store) {
762
882
  return `[${timestamp}] ${levelName}: ${message}
763
883
  `;
764
884
  }
765
- function createFormattedFilterStream(format, level, rules, state, destination) {
885
+ function createFormattedFilterStream(format, level, rules, state, destination, respectOverrides) {
766
886
  return new Transform({
767
887
  transform(chunk, _encoding, callback) {
768
888
  const line = chunk.toString().trim();
@@ -775,7 +895,7 @@ function createFormattedFilterStream(format, level, rules, state, destination) {
775
895
  callback();
776
896
  return;
777
897
  }
778
- if (!shouldPassTransport(log, level, rules, state)) {
898
+ if (!shouldPassTransport(log, level, rules, state, respectOverrides)) {
779
899
  callback();
780
900
  return;
781
901
  }
@@ -793,7 +913,8 @@ function createStreams(config, state) {
793
913
  config.console.level,
794
914
  config.console.rules,
795
915
  state,
796
- process.stdout
916
+ process.stdout,
917
+ resolveRespectsOverrides("console", config.console.respectRuntimeOverrides)
797
918
  );
798
919
  streams.push({
799
920
  level: "trace",
@@ -826,7 +947,8 @@ function createStreams(config, state) {
826
947
  fileConfig.level,
827
948
  fileConfig.rules,
828
949
  state,
829
- rotatingStream
950
+ rotatingStream,
951
+ resolveRespectsOverrides("file", fileConfig.respectRuntimeOverrides)
830
952
  );
831
953
  streams.push({
832
954
  level: "trace",
@@ -836,7 +958,13 @@ function createStreams(config, state) {
836
958
  for (const discordConfig of toArray(config.discord)) {
837
959
  const transport = new DiscordTransport(discordConfig);
838
960
  transports.push(transport);
839
- const discordStream = createHttpTransportStream(transport, discordConfig.level, discordConfig.rules, state);
961
+ const discordStream = createHttpTransportStream(
962
+ transport,
963
+ discordConfig.level,
964
+ discordConfig.rules,
965
+ state,
966
+ resolveRespectsOverrides("discord", discordConfig.respectRuntimeOverrides)
967
+ );
840
968
  streams.push({
841
969
  level: "trace",
842
970
  stream: discordStream
@@ -845,16 +973,43 @@ function createStreams(config, state) {
845
973
  for (const telegramConfig of toArray(config.telegram)) {
846
974
  const transport = new TelegramTransport(telegramConfig);
847
975
  transports.push(transport);
848
- const telegramStream = createHttpTransportStream(transport, telegramConfig.level, telegramConfig.rules, state);
976
+ const telegramStream = createHttpTransportStream(
977
+ transport,
978
+ telegramConfig.level,
979
+ telegramConfig.rules,
980
+ state,
981
+ resolveRespectsOverrides("telegram", telegramConfig.respectRuntimeOverrides)
982
+ );
849
983
  streams.push({
850
984
  level: "trace",
851
985
  stream: telegramStream
852
986
  });
853
987
  }
988
+ for (const zohoCliqConfig of toArray(config.zohoCliq)) {
989
+ const transport = new ZohoCliqTransport(zohoCliqConfig);
990
+ transports.push(transport);
991
+ const zohoStream = createHttpTransportStream(
992
+ transport,
993
+ zohoCliqConfig.level,
994
+ zohoCliqConfig.rules,
995
+ state,
996
+ resolveRespectsOverrides("zohoCliq", zohoCliqConfig.respectRuntimeOverrides)
997
+ );
998
+ streams.push({
999
+ level: "trace",
1000
+ stream: zohoStream
1001
+ });
1002
+ }
854
1003
  for (const cloudwatchConfig of toArray(config.cloudwatch)) {
855
1004
  const transport = new CloudWatchTransport(cloudwatchConfig, config.hostname);
856
1005
  transports.push(transport);
857
- const cwStream = createHttpTransportStream(transport, cloudwatchConfig.level, cloudwatchConfig.rules, state);
1006
+ const cwStream = createHttpTransportStream(
1007
+ transport,
1008
+ cloudwatchConfig.level,
1009
+ cloudwatchConfig.rules,
1010
+ state,
1011
+ resolveRespectsOverrides("cloudwatch", cloudwatchConfig.respectRuntimeOverrides)
1012
+ );
858
1013
  streams.push({
859
1014
  level: "trace",
860
1015
  stream: cwStream
@@ -879,7 +1034,7 @@ function toArray(value) {
879
1034
  if (!value) return [];
880
1035
  return Array.isArray(value) ? value : [value];
881
1036
  }
882
- function createHttpTransportStream(transport, level, rules, state) {
1037
+ function createHttpTransportStream(transport, level, rules, state, respectOverrides) {
883
1038
  return new Writable({
884
1039
  write(chunk, _encoding, callback) {
885
1040
  const line = chunk.toString().trim();
@@ -892,7 +1047,7 @@ function createHttpTransportStream(transport, level, rules, state) {
892
1047
  callback();
893
1048
  return;
894
1049
  }
895
- if (!shouldPassTransport(log, level, rules, state)) {
1050
+ if (!shouldPassTransport(log, level, rules, state, respectOverrides)) {
896
1051
  callback();
897
1052
  return;
898
1053
  }
@@ -1051,12 +1206,14 @@ var LevelRuleSchema = z.object({
1051
1206
  var ConsoleConfigSchema = z.object({
1052
1207
  format: LogFormatSchema,
1053
1208
  level: LogLevelSchema.optional(),
1054
- rules: z.array(LevelRuleSchema).optional()
1209
+ rules: z.array(LevelRuleSchema).optional(),
1210
+ respectRuntimeOverrides: z.boolean().optional()
1055
1211
  });
1056
1212
  var FileConfigSchema = z.object({
1057
1213
  format: LogFormatSchema,
1058
1214
  level: LogLevelSchema.optional(),
1059
1215
  rules: z.array(LevelRuleSchema).optional(),
1216
+ respectRuntimeOverrides: z.boolean().optional(),
1060
1217
  dirname: z.string().min(1, "dirname is required"),
1061
1218
  filename: z.string().min(1, "filename is required"),
1062
1219
  datePattern: z.string().optional(),
@@ -1068,6 +1225,7 @@ var FileConfigSchema = z.object({
1068
1225
  var HttpTransportBaseConfigSchema = z.object({
1069
1226
  level: LogLevelSchema.optional(),
1070
1227
  rules: z.array(LevelRuleSchema).optional(),
1228
+ respectRuntimeOverrides: z.boolean().optional(),
1071
1229
  batchSize: z.number().int().positive().optional(),
1072
1230
  flushInterval: z.number().int().positive().optional(),
1073
1231
  maxRetries: z.number().int().nonnegative().optional(),
@@ -1081,6 +1239,7 @@ var HttpTransportBaseConfigSchema = z.object({
1081
1239
  var DiscordConfigSchema = z.object({
1082
1240
  level: LogLevelSchema.optional(),
1083
1241
  rules: z.array(LevelRuleSchema).optional(),
1242
+ respectRuntimeOverrides: z.boolean().optional(),
1084
1243
  batchSize: z.number().int().positive().optional(),
1085
1244
  flushInterval: z.number().int().positive().optional(),
1086
1245
  maxRetries: z.number().int().nonnegative().optional(),
@@ -1102,6 +1261,7 @@ var DiscordConfigSchema = z.object({
1102
1261
  var TelegramConfigSchema = z.object({
1103
1262
  level: LogLevelSchema.optional(),
1104
1263
  rules: z.array(LevelRuleSchema).optional(),
1264
+ respectRuntimeOverrides: z.boolean().optional(),
1105
1265
  batchSize: z.number().int().positive().optional(),
1106
1266
  flushInterval: z.number().int().positive().optional(),
1107
1267
  maxRetries: z.number().int().nonnegative().optional(),
@@ -1139,6 +1299,7 @@ var LogStreamNameSchema = z.union([
1139
1299
  var CloudWatchConfigSchema = z.object({
1140
1300
  level: LogLevelSchema.optional(),
1141
1301
  rules: z.array(LevelRuleSchema).optional(),
1302
+ respectRuntimeOverrides: z.boolean().optional(),
1142
1303
  batchSize: z.number().int().positive().optional(),
1143
1304
  flushInterval: z.number().int().positive().optional(),
1144
1305
  maxRetries: z.number().int().nonnegative().optional(),
@@ -1156,13 +1317,44 @@ var CloudWatchConfigSchema = z.object({
1156
1317
  createLogGroup: z.boolean().optional(),
1157
1318
  createLogStream: z.boolean().optional()
1158
1319
  });
1320
+ var ZohoCliqConfigBaseSchema = z.object({
1321
+ level: LogLevelSchema.optional(),
1322
+ rules: z.array(LevelRuleSchema).optional(),
1323
+ respectRuntimeOverrides: z.boolean().optional(),
1324
+ batchSize: z.number().int().positive().optional(),
1325
+ flushInterval: z.number().int().positive().optional(),
1326
+ maxRetries: z.number().int().nonnegative().optional(),
1327
+ retryDelay: z.number().int().positive().optional(),
1328
+ onError: z.function().optional(),
1329
+ onDrop: z.function().optional(),
1330
+ maxQueueSize: z.number().int().positive().optional(),
1331
+ dropPolicy: z.enum(["drop-oldest", "drop-newest"]).optional(),
1332
+ requestTimeout: z.number().int().positive().optional(),
1333
+ webhookUrl: z.string().url().optional(),
1334
+ companyId: z.string().min(1).optional(),
1335
+ channel: z.string().min(1).optional(),
1336
+ region: z.string().min(1).optional(),
1337
+ apiKey: z.string().min(1, "apiKey is required"),
1338
+ broadcast: z.boolean().optional(),
1339
+ bot: z.object({
1340
+ name: z.string().min(1),
1341
+ image: z.string().url().optional()
1342
+ }).optional(),
1343
+ includeTimestamp: z.boolean().optional(),
1344
+ includeMeta: z.boolean().optional()
1345
+ });
1346
+ var ZohoCliqConfigSchema = ZohoCliqConfigBaseSchema.refine(
1347
+ (cfg) => Boolean(cfg.webhookUrl) || Boolean(cfg.companyId) && Boolean(cfg.channel),
1348
+ { message: "Either webhookUrl or both companyId and channel must be provided" }
1349
+ );
1159
1350
  var RelayConfigSchema = z.object({
1160
1351
  apiUrl: z.string().url("apiUrl must be a valid URL"),
1161
1352
  token: z.string().min(1, "token is required"),
1162
1353
  pollInterval: z.number().int().positive().optional(),
1163
1354
  bufferSize: z.number().int().positive().optional(),
1164
1355
  reconnectDelay: z.number().int().positive().optional(),
1165
- maxReconnectDelay: z.number().int().positive().optional()
1356
+ maxReconnectDelay: z.number().int().positive().optional(),
1357
+ respectRuntimeOverrides: z.boolean().optional()
1166
1358
  });
1167
1359
  var LevelConfigObjectSchema = z.object({
1168
1360
  default: LogLevelSchema,
@@ -1187,6 +1379,7 @@ var LoggerConfigSchema = z.object({
1187
1379
  discord: z.union([DiscordConfigSchema, z.array(DiscordConfigSchema)]).optional(),
1188
1380
  telegram: z.union([TelegramConfigSchema, z.array(TelegramConfigSchema)]).optional(),
1189
1381
  cloudwatch: z.union([CloudWatchConfigSchema, z.array(CloudWatchConfigSchema)]).optional(),
1382
+ zohoCliq: z.union([ZohoCliqConfigSchema, z.array(ZohoCliqConfigSchema)]).optional(),
1190
1383
  relay: RelayConfigSchema.optional(),
1191
1384
  caller: z.union([z.boolean(), CallerConfigSchema]).optional(),
1192
1385
  hostname: z.string().optional(),
@@ -1848,6 +2041,6 @@ function formatLogfmt(data) {
1848
2041
  return Object.entries(flattened).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${key}=${formatLogfmtValue(value)}`).join(" ");
1849
2042
  }
1850
2043
 
1851
- 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 };
2044
+ 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, RelayConfigSchema, TelegramConfigSchema, TelegramTransport, ZohoCliqConfigSchema, ZohoCliqTransport, assertLogLevel, createMasker, createSingletonLogger, extractRequestId, flattenObject, formatCallerInfo, formatLogfmt, formatLogfmtValue, generateRequestId, getCallerInfo, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync, registerShutdown, safeValidateConfig, serializeError, validateConfig };
1852
2045
  //# sourceMappingURL=index.mjs.map
1853
2046
  //# sourceMappingURL=index.mjs.map