@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.js CHANGED
@@ -500,6 +500,112 @@ var TelegramTransport = class extends BaseHttpTransport {
500
500
  return text.replace(/[&<>"']/g, (c) => entities[c] || c);
501
501
  }
502
502
  };
503
+
504
+ // src/transports/zoho-cliq.ts
505
+ var LEVEL_EMOJI3 = {
506
+ off: "",
507
+ error: "\u{1F534}",
508
+ warn: "\u{1F7E1}",
509
+ info: "\u{1F7E2}",
510
+ http: "\u{1F535}",
511
+ verbose: "\u{1F7E3}",
512
+ debug: "\u26AA",
513
+ silly: "\u26AB"
514
+ };
515
+ var DEFAULT_REQUEST_TIMEOUT_MS3 = 1e4;
516
+ var MAX_MESSAGE_LENGTH = 9500;
517
+ var ZohoCliqTransport = class extends BaseHttpTransport {
518
+ config;
519
+ endpoint;
520
+ requestTimeout;
521
+ constructor(config) {
522
+ super({
523
+ batchSize: config.batchSize ?? 20,
524
+ flushInterval: config.flushInterval ?? 2e3,
525
+ maxRetries: config.maxRetries,
526
+ retryDelay: config.retryDelay,
527
+ maxQueueSize: config.maxQueueSize,
528
+ dropPolicy: config.dropPolicy,
529
+ onDrop: config.onDrop,
530
+ onError: config.onError
531
+ });
532
+ this.config = config;
533
+ this.requestTimeout = config.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT_MS3;
534
+ this.endpoint = this.buildEndpoint();
535
+ }
536
+ buildEndpoint() {
537
+ if (this.config.webhookUrl) {
538
+ return this.config.webhookUrl;
539
+ }
540
+ const region = this.config.region ?? "eu";
541
+ const companyId = this.config.companyId;
542
+ const channel = encodeURIComponent(this.config.channel);
543
+ return `https://cliq.zoho.${region}/company/${companyId}/api/v2/channelsbyname/${channel}/message`;
544
+ }
545
+ async sendBatch(messages) {
546
+ const text = messages.map((msg) => this.formatMessage(msg)).join("\n\n---\n\n");
547
+ for (const chunk of this.splitContent(text, MAX_MESSAGE_LENGTH)) {
548
+ await this.post(chunk);
549
+ }
550
+ }
551
+ formatMessage(msg) {
552
+ const emoji = LEVEL_EMOJI3[msg.level];
553
+ const level = msg.level.toUpperCase();
554
+ const context = msg.context || "APP";
555
+ const timestamp = this.config.includeTimestamp !== false ? ` ${msg.timestamp.toISOString()}` : "";
556
+ let text = `${emoji} *${level}* [${context}]${timestamp}
557
+ ${msg.message}`;
558
+ if (this.config.includeMeta !== false && msg.meta && Object.keys(msg.meta).length > 0) {
559
+ text += "\n```\n" + JSON.stringify(msg.meta, null, 2) + "\n```";
560
+ }
561
+ return text;
562
+ }
563
+ splitContent(content, maxLength) {
564
+ if (content.length <= maxLength) return [content];
565
+ const chunks = [];
566
+ let current = content;
567
+ while (current.length > 0) {
568
+ if (current.length <= maxLength) {
569
+ chunks.push(current);
570
+ break;
571
+ }
572
+ let splitAt = current.lastIndexOf("\n", maxLength);
573
+ if (splitAt === -1 || splitAt < maxLength / 2) {
574
+ splitAt = current.lastIndexOf(" ", maxLength);
575
+ }
576
+ if (splitAt === -1 || splitAt < maxLength / 2) {
577
+ splitAt = maxLength;
578
+ }
579
+ chunks.push(current.slice(0, splitAt));
580
+ current = current.slice(splitAt).trimStart();
581
+ }
582
+ return chunks;
583
+ }
584
+ async post(text) {
585
+ const url = new URL(this.endpoint);
586
+ url.searchParams.set("zapikey", this.config.apiKey);
587
+ if (this.config.bot?.name) {
588
+ url.searchParams.set("bot_unique_name", this.config.bot.name);
589
+ }
590
+ const body = {
591
+ text,
592
+ broadcast: this.config.broadcast ?? true
593
+ };
594
+ if (this.config.bot) {
595
+ body.bot = this.config.bot;
596
+ }
597
+ const response = await fetch(url.toString(), {
598
+ method: "POST",
599
+ headers: { "Content-Type": "application/json" },
600
+ body: JSON.stringify(body),
601
+ signal: AbortSignal.timeout(this.requestTimeout)
602
+ });
603
+ if (!response.ok && response.status !== 204) {
604
+ const respText = await response.text().catch(() => "");
605
+ throw new Error(`Zoho Cliq API failed: ${response.status} ${respText}`);
606
+ }
607
+ }
608
+ };
503
609
  var instanceUuid = crypto.randomUUID().slice(0, 8);
504
610
  function formatDate() {
505
611
  return (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
@@ -683,13 +789,27 @@ function getLevelName(levelNum) {
683
789
  if (levelNum >= 20) return "debug";
684
790
  return "silly";
685
791
  }
686
- function shouldPassTransport(log, level, rules, state) {
792
+ var DEFAULT_RESPECTS_OVERRIDES = {
793
+ console: true,
794
+ file: true,
795
+ cloudwatch: true,
796
+ relay: true,
797
+ discord: false,
798
+ telegram: false,
799
+ zohoCliq: false
800
+ };
801
+ function resolveRespectsOverrides(kind, configFlag) {
802
+ return configFlag ?? DEFAULT_RESPECTS_OVERRIDES[kind];
803
+ }
804
+ function shouldPassTransport(log, level, rules, state, respectOverrides) {
687
805
  const logLevel = getLevelName(log.level);
688
- const embeddedOverride = log.__or;
689
- if (typeof embeddedOverride === "string") {
690
- if (embeddedOverride === "off") return false;
691
- const overrideLevel = embeddedOverride;
692
- return LOG_LEVELS[logLevel] <= LOG_LEVELS[overrideLevel];
806
+ if (respectOverrides) {
807
+ const embeddedOverride = log.__or;
808
+ if (typeof embeddedOverride === "string") {
809
+ if (embeddedOverride === "off") return false;
810
+ const overrideLevel = embeddedOverride;
811
+ return LOG_LEVELS[logLevel] <= LOG_LEVELS[overrideLevel];
812
+ }
693
813
  }
694
814
  const context = log.context;
695
815
  if (rules && rules.length > 0) {
@@ -768,7 +888,7 @@ function formatLog(log, format, store) {
768
888
  return `[${timestamp}] ${levelName}: ${message}
769
889
  `;
770
890
  }
771
- function createFormattedFilterStream(format, level, rules, state, destination) {
891
+ function createFormattedFilterStream(format, level, rules, state, destination, respectOverrides) {
772
892
  return new stream.Transform({
773
893
  transform(chunk, _encoding, callback) {
774
894
  const line = chunk.toString().trim();
@@ -781,7 +901,7 @@ function createFormattedFilterStream(format, level, rules, state, destination) {
781
901
  callback();
782
902
  return;
783
903
  }
784
- if (!shouldPassTransport(log, level, rules, state)) {
904
+ if (!shouldPassTransport(log, level, rules, state, respectOverrides)) {
785
905
  callback();
786
906
  return;
787
907
  }
@@ -799,7 +919,8 @@ function createStreams(config, state) {
799
919
  config.console.level,
800
920
  config.console.rules,
801
921
  state,
802
- process.stdout
922
+ process.stdout,
923
+ resolveRespectsOverrides("console", config.console.respectRuntimeOverrides)
803
924
  );
804
925
  streams.push({
805
926
  level: "trace",
@@ -832,7 +953,8 @@ function createStreams(config, state) {
832
953
  fileConfig.level,
833
954
  fileConfig.rules,
834
955
  state,
835
- rotatingStream
956
+ rotatingStream,
957
+ resolveRespectsOverrides("file", fileConfig.respectRuntimeOverrides)
836
958
  );
837
959
  streams.push({
838
960
  level: "trace",
@@ -842,7 +964,13 @@ function createStreams(config, state) {
842
964
  for (const discordConfig of toArray(config.discord)) {
843
965
  const transport = new DiscordTransport(discordConfig);
844
966
  transports.push(transport);
845
- const discordStream = createHttpTransportStream(transport, discordConfig.level, discordConfig.rules, state);
967
+ const discordStream = createHttpTransportStream(
968
+ transport,
969
+ discordConfig.level,
970
+ discordConfig.rules,
971
+ state,
972
+ resolveRespectsOverrides("discord", discordConfig.respectRuntimeOverrides)
973
+ );
846
974
  streams.push({
847
975
  level: "trace",
848
976
  stream: discordStream
@@ -851,16 +979,43 @@ function createStreams(config, state) {
851
979
  for (const telegramConfig of toArray(config.telegram)) {
852
980
  const transport = new TelegramTransport(telegramConfig);
853
981
  transports.push(transport);
854
- const telegramStream = createHttpTransportStream(transport, telegramConfig.level, telegramConfig.rules, state);
982
+ const telegramStream = createHttpTransportStream(
983
+ transport,
984
+ telegramConfig.level,
985
+ telegramConfig.rules,
986
+ state,
987
+ resolveRespectsOverrides("telegram", telegramConfig.respectRuntimeOverrides)
988
+ );
855
989
  streams.push({
856
990
  level: "trace",
857
991
  stream: telegramStream
858
992
  });
859
993
  }
994
+ for (const zohoCliqConfig of toArray(config.zohoCliq)) {
995
+ const transport = new ZohoCliqTransport(zohoCliqConfig);
996
+ transports.push(transport);
997
+ const zohoStream = createHttpTransportStream(
998
+ transport,
999
+ zohoCliqConfig.level,
1000
+ zohoCliqConfig.rules,
1001
+ state,
1002
+ resolveRespectsOverrides("zohoCliq", zohoCliqConfig.respectRuntimeOverrides)
1003
+ );
1004
+ streams.push({
1005
+ level: "trace",
1006
+ stream: zohoStream
1007
+ });
1008
+ }
860
1009
  for (const cloudwatchConfig of toArray(config.cloudwatch)) {
861
1010
  const transport = new CloudWatchTransport(cloudwatchConfig, config.hostname);
862
1011
  transports.push(transport);
863
- const cwStream = createHttpTransportStream(transport, cloudwatchConfig.level, cloudwatchConfig.rules, state);
1012
+ const cwStream = createHttpTransportStream(
1013
+ transport,
1014
+ cloudwatchConfig.level,
1015
+ cloudwatchConfig.rules,
1016
+ state,
1017
+ resolveRespectsOverrides("cloudwatch", cloudwatchConfig.respectRuntimeOverrides)
1018
+ );
864
1019
  streams.push({
865
1020
  level: "trace",
866
1021
  stream: cwStream
@@ -885,7 +1040,7 @@ function toArray(value) {
885
1040
  if (!value) return [];
886
1041
  return Array.isArray(value) ? value : [value];
887
1042
  }
888
- function createHttpTransportStream(transport, level, rules, state) {
1043
+ function createHttpTransportStream(transport, level, rules, state, respectOverrides) {
889
1044
  return new stream.Writable({
890
1045
  write(chunk, _encoding, callback) {
891
1046
  const line = chunk.toString().trim();
@@ -898,7 +1053,7 @@ function createHttpTransportStream(transport, level, rules, state) {
898
1053
  callback();
899
1054
  return;
900
1055
  }
901
- if (!shouldPassTransport(log, level, rules, state)) {
1056
+ if (!shouldPassTransport(log, level, rules, state, respectOverrides)) {
902
1057
  callback();
903
1058
  return;
904
1059
  }
@@ -1057,12 +1212,14 @@ var LevelRuleSchema = zod.z.object({
1057
1212
  var ConsoleConfigSchema = zod.z.object({
1058
1213
  format: LogFormatSchema,
1059
1214
  level: LogLevelSchema.optional(),
1060
- rules: zod.z.array(LevelRuleSchema).optional()
1215
+ rules: zod.z.array(LevelRuleSchema).optional(),
1216
+ respectRuntimeOverrides: zod.z.boolean().optional()
1061
1217
  });
1062
1218
  var FileConfigSchema = zod.z.object({
1063
1219
  format: LogFormatSchema,
1064
1220
  level: LogLevelSchema.optional(),
1065
1221
  rules: zod.z.array(LevelRuleSchema).optional(),
1222
+ respectRuntimeOverrides: zod.z.boolean().optional(),
1066
1223
  dirname: zod.z.string().min(1, "dirname is required"),
1067
1224
  filename: zod.z.string().min(1, "filename is required"),
1068
1225
  datePattern: zod.z.string().optional(),
@@ -1074,6 +1231,7 @@ var FileConfigSchema = zod.z.object({
1074
1231
  var HttpTransportBaseConfigSchema = zod.z.object({
1075
1232
  level: LogLevelSchema.optional(),
1076
1233
  rules: zod.z.array(LevelRuleSchema).optional(),
1234
+ respectRuntimeOverrides: zod.z.boolean().optional(),
1077
1235
  batchSize: zod.z.number().int().positive().optional(),
1078
1236
  flushInterval: zod.z.number().int().positive().optional(),
1079
1237
  maxRetries: zod.z.number().int().nonnegative().optional(),
@@ -1087,6 +1245,7 @@ var HttpTransportBaseConfigSchema = zod.z.object({
1087
1245
  var DiscordConfigSchema = zod.z.object({
1088
1246
  level: LogLevelSchema.optional(),
1089
1247
  rules: zod.z.array(LevelRuleSchema).optional(),
1248
+ respectRuntimeOverrides: zod.z.boolean().optional(),
1090
1249
  batchSize: zod.z.number().int().positive().optional(),
1091
1250
  flushInterval: zod.z.number().int().positive().optional(),
1092
1251
  maxRetries: zod.z.number().int().nonnegative().optional(),
@@ -1108,6 +1267,7 @@ var DiscordConfigSchema = zod.z.object({
1108
1267
  var TelegramConfigSchema = zod.z.object({
1109
1268
  level: LogLevelSchema.optional(),
1110
1269
  rules: zod.z.array(LevelRuleSchema).optional(),
1270
+ respectRuntimeOverrides: zod.z.boolean().optional(),
1111
1271
  batchSize: zod.z.number().int().positive().optional(),
1112
1272
  flushInterval: zod.z.number().int().positive().optional(),
1113
1273
  maxRetries: zod.z.number().int().nonnegative().optional(),
@@ -1145,6 +1305,7 @@ var LogStreamNameSchema = zod.z.union([
1145
1305
  var CloudWatchConfigSchema = zod.z.object({
1146
1306
  level: LogLevelSchema.optional(),
1147
1307
  rules: zod.z.array(LevelRuleSchema).optional(),
1308
+ respectRuntimeOverrides: zod.z.boolean().optional(),
1148
1309
  batchSize: zod.z.number().int().positive().optional(),
1149
1310
  flushInterval: zod.z.number().int().positive().optional(),
1150
1311
  maxRetries: zod.z.number().int().nonnegative().optional(),
@@ -1162,13 +1323,44 @@ var CloudWatchConfigSchema = zod.z.object({
1162
1323
  createLogGroup: zod.z.boolean().optional(),
1163
1324
  createLogStream: zod.z.boolean().optional()
1164
1325
  });
1326
+ var ZohoCliqConfigBaseSchema = zod.z.object({
1327
+ level: LogLevelSchema.optional(),
1328
+ rules: zod.z.array(LevelRuleSchema).optional(),
1329
+ respectRuntimeOverrides: zod.z.boolean().optional(),
1330
+ batchSize: zod.z.number().int().positive().optional(),
1331
+ flushInterval: zod.z.number().int().positive().optional(),
1332
+ maxRetries: zod.z.number().int().nonnegative().optional(),
1333
+ retryDelay: zod.z.number().int().positive().optional(),
1334
+ onError: zod.z.function().optional(),
1335
+ onDrop: zod.z.function().optional(),
1336
+ maxQueueSize: zod.z.number().int().positive().optional(),
1337
+ dropPolicy: zod.z.enum(["drop-oldest", "drop-newest"]).optional(),
1338
+ requestTimeout: zod.z.number().int().positive().optional(),
1339
+ webhookUrl: zod.z.string().url().optional(),
1340
+ companyId: zod.z.string().min(1).optional(),
1341
+ channel: zod.z.string().min(1).optional(),
1342
+ region: zod.z.string().min(1).optional(),
1343
+ apiKey: zod.z.string().min(1, "apiKey is required"),
1344
+ broadcast: zod.z.boolean().optional(),
1345
+ bot: zod.z.object({
1346
+ name: zod.z.string().min(1),
1347
+ image: zod.z.string().url().optional()
1348
+ }).optional(),
1349
+ includeTimestamp: zod.z.boolean().optional(),
1350
+ includeMeta: zod.z.boolean().optional()
1351
+ });
1352
+ var ZohoCliqConfigSchema = ZohoCliqConfigBaseSchema.refine(
1353
+ (cfg) => Boolean(cfg.webhookUrl) || Boolean(cfg.companyId) && Boolean(cfg.channel),
1354
+ { message: "Either webhookUrl or both companyId and channel must be provided" }
1355
+ );
1165
1356
  var RelayConfigSchema = zod.z.object({
1166
1357
  apiUrl: zod.z.string().url("apiUrl must be a valid URL"),
1167
1358
  token: zod.z.string().min(1, "token is required"),
1168
1359
  pollInterval: zod.z.number().int().positive().optional(),
1169
1360
  bufferSize: zod.z.number().int().positive().optional(),
1170
1361
  reconnectDelay: zod.z.number().int().positive().optional(),
1171
- maxReconnectDelay: zod.z.number().int().positive().optional()
1362
+ maxReconnectDelay: zod.z.number().int().positive().optional(),
1363
+ respectRuntimeOverrides: zod.z.boolean().optional()
1172
1364
  });
1173
1365
  var LevelConfigObjectSchema = zod.z.object({
1174
1366
  default: LogLevelSchema,
@@ -1193,6 +1385,7 @@ var LoggerConfigSchema = zod.z.object({
1193
1385
  discord: zod.z.union([DiscordConfigSchema, zod.z.array(DiscordConfigSchema)]).optional(),
1194
1386
  telegram: zod.z.union([TelegramConfigSchema, zod.z.array(TelegramConfigSchema)]).optional(),
1195
1387
  cloudwatch: zod.z.union([CloudWatchConfigSchema, zod.z.array(CloudWatchConfigSchema)]).optional(),
1388
+ zohoCliq: zod.z.union([ZohoCliqConfigSchema, zod.z.array(ZohoCliqConfigSchema)]).optional(),
1196
1389
  relay: RelayConfigSchema.optional(),
1197
1390
  caller: zod.z.union([zod.z.boolean(), CallerConfigSchema]).optional(),
1198
1391
  hostname: zod.z.string().optional(),
@@ -1878,8 +2071,11 @@ exports.Logger = Logger;
1878
2071
  exports.LoggerConfigSchema = LoggerConfigSchema;
1879
2072
  exports.LoggerStore = LoggerStore;
1880
2073
  exports.MessageBuffer = MessageBuffer;
2074
+ exports.RelayConfigSchema = RelayConfigSchema;
1881
2075
  exports.TelegramConfigSchema = TelegramConfigSchema;
1882
2076
  exports.TelegramTransport = TelegramTransport;
2077
+ exports.ZohoCliqConfigSchema = ZohoCliqConfigSchema;
2078
+ exports.ZohoCliqTransport = ZohoCliqTransport;
1883
2079
  exports.assertLogLevel = assertLogLevel;
1884
2080
  exports.createMasker = createMasker;
1885
2081
  exports.createSingletonLogger = createSingletonLogger;