@rawnodes/logger 1.8.0 → 1.9.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/README.md CHANGED
@@ -297,6 +297,39 @@ All external transports support:
297
297
  | `maxRetries` | 3 | Retry count on failure |
298
298
  | `retryDelay` | 1000 | Base retry delay in ms |
299
299
 
300
+ ### Multiple Transports
301
+
302
+ You can configure multiple instances of the same transport type using arrays:
303
+
304
+ ```typescript
305
+ const logger = Logger.create({
306
+ level: 'info',
307
+ console: { format: 'plain' },
308
+ discord: [
309
+ {
310
+ webhookUrl: 'https://discord.com/api/webhooks/payments/xxx',
311
+ level: 'off',
312
+ rules: [{ match: { context: 'payments' }, level: 'error' }],
313
+ },
314
+ {
315
+ webhookUrl: 'https://discord.com/api/webhooks/auth/yyy',
316
+ level: 'off',
317
+ rules: [{ match: { context: 'auth' }, level: 'error' }],
318
+ },
319
+ ],
320
+ telegram: [
321
+ { botToken: 'token1', chatId: 'errors-chat', level: 'error' },
322
+ { botToken: 'token2', chatId: 'alerts-chat', level: 'warn' },
323
+ ],
324
+ });
325
+
326
+ // Payment errors go to payments Discord channel
327
+ logger.for('payments').error('Payment failed');
328
+
329
+ // Auth errors go to auth Discord channel
330
+ logger.for('auth').error('Login failed');
331
+ ```
332
+
300
333
  ## Singleton Pattern
301
334
 
302
335
  For app-wide logging:
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import TransportStream from 'winston-transport';
2
+ import { z } from 'zod';
2
3
 
3
4
  declare const LOG_LEVELS: {
4
5
  readonly off: -1;
@@ -79,9 +80,9 @@ interface LoggerConfig {
79
80
  level: LevelConfig;
80
81
  console: ConsoleConfig;
81
82
  file?: FileConfig;
82
- discord?: DiscordConfig;
83
- telegram?: TelegramConfig;
84
- cloudwatch?: CloudWatchConfig;
83
+ discord?: DiscordConfig | DiscordConfig[];
84
+ telegram?: TelegramConfig | TelegramConfig[];
85
+ cloudwatch?: CloudWatchConfig | CloudWatchConfig[];
85
86
  }
86
87
  type LoggerContext = Record<string, unknown>;
87
88
  type LevelOverrideMatch<TContext extends LoggerContext> = Partial<TContext> & {
@@ -260,4 +261,19 @@ declare function flattenObject(obj: Record<string, unknown>, prefix?: string): R
260
261
  declare function formatLogfmtValue(value: unknown): string;
261
262
  declare function formatLogfmt(data: Record<string, unknown>): string;
262
263
 
263
- export { BaseHttpTransport, type BaseHttpTransportOptions, type BufferOptions, type BufferedMessage, type CloudWatchConfig, CloudWatchTransport, type ConsoleConfig, type DiscordConfig, DiscordTransport, type FileConfig, type HttpTransportBaseConfig, LOG_LEVELS, type LevelConfig, type LevelConfigObject, type LevelOverride, type LevelOverrideMatch, type LevelRule, type LogFormat, type LogLevel, Logger, type LoggerConfig, type LoggerContext, LoggerStore, type MaskSecretsOptions, MessageBuffer, type Meta, type RequestIdOptions, type SingletonLogger, type TelegramConfig, TelegramTransport, type TimingResult, assertLogLevel, createMasker, createSingletonLogger, extractRequestId, flattenObject, formatLogfmt, formatLogfmtValue, generateRequestId, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync };
264
+ declare const LogLevelSchema: z.ZodType<LogLevel>;
265
+ declare const LogFormatSchema: z.ZodType<LogFormat>;
266
+ declare const LevelRuleSchema: z.ZodType<LevelRule>;
267
+ declare const ConsoleConfigSchema: z.ZodType<ConsoleConfig>;
268
+ declare const FileConfigSchema: z.ZodType<FileConfig>;
269
+ declare const HttpTransportBaseConfigSchema: z.ZodType<HttpTransportBaseConfig>;
270
+ declare const DiscordConfigSchema: z.ZodType<DiscordConfig>;
271
+ declare const TelegramConfigSchema: z.ZodType<TelegramConfig>;
272
+ declare const CloudWatchConfigSchema: z.ZodType<CloudWatchConfig>;
273
+ declare const LevelConfigObjectSchema: z.ZodType<LevelConfigObject>;
274
+ declare const LevelConfigSchema: z.ZodType<LevelConfig>;
275
+ declare const LoggerConfigSchema: z.ZodType<LoggerConfig>;
276
+ declare function validateConfig(config: unknown): z.infer<typeof LoggerConfigSchema>;
277
+ declare function safeValidateConfig(config: unknown): z.ZodSafeParseResult<LoggerConfig>;
278
+
279
+ export { BaseHttpTransport, type BaseHttpTransportOptions, type BufferOptions, type BufferedMessage, type CloudWatchConfig, CloudWatchConfigSchema, CloudWatchTransport, type ConsoleConfig, ConsoleConfigSchema, type DiscordConfig, DiscordConfigSchema, DiscordTransport, type FileConfig, FileConfigSchema, type HttpTransportBaseConfig, HttpTransportBaseConfigSchema, LOG_LEVELS, type LevelConfig, type LevelConfigObject, LevelConfigObjectSchema, LevelConfigSchema, type LevelOverride, type LevelOverrideMatch, type LevelRule, LevelRuleSchema, type LogFormat, LogFormatSchema, type LogLevel, LogLevelSchema, Logger, type LoggerConfig, LoggerConfigSchema, type LoggerContext, LoggerStore, type MaskSecretsOptions, MessageBuffer, type Meta, type RequestIdOptions, type SingletonLogger, type TelegramConfig, TelegramConfigSchema, TelegramTransport, type TimingResult, assertLogLevel, createMasker, createSingletonLogger, extractRequestId, flattenObject, formatLogfmt, formatLogfmtValue, generateRequestId, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync, safeValidateConfig, validateConfig };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import TransportStream from 'winston-transport';
2
+ import { z } from 'zod';
2
3
 
3
4
  declare const LOG_LEVELS: {
4
5
  readonly off: -1;
@@ -79,9 +80,9 @@ interface LoggerConfig {
79
80
  level: LevelConfig;
80
81
  console: ConsoleConfig;
81
82
  file?: FileConfig;
82
- discord?: DiscordConfig;
83
- telegram?: TelegramConfig;
84
- cloudwatch?: CloudWatchConfig;
83
+ discord?: DiscordConfig | DiscordConfig[];
84
+ telegram?: TelegramConfig | TelegramConfig[];
85
+ cloudwatch?: CloudWatchConfig | CloudWatchConfig[];
85
86
  }
86
87
  type LoggerContext = Record<string, unknown>;
87
88
  type LevelOverrideMatch<TContext extends LoggerContext> = Partial<TContext> & {
@@ -260,4 +261,19 @@ declare function flattenObject(obj: Record<string, unknown>, prefix?: string): R
260
261
  declare function formatLogfmtValue(value: unknown): string;
261
262
  declare function formatLogfmt(data: Record<string, unknown>): string;
262
263
 
263
- export { BaseHttpTransport, type BaseHttpTransportOptions, type BufferOptions, type BufferedMessage, type CloudWatchConfig, CloudWatchTransport, type ConsoleConfig, type DiscordConfig, DiscordTransport, type FileConfig, type HttpTransportBaseConfig, LOG_LEVELS, type LevelConfig, type LevelConfigObject, type LevelOverride, type LevelOverrideMatch, type LevelRule, type LogFormat, type LogLevel, Logger, type LoggerConfig, type LoggerContext, LoggerStore, type MaskSecretsOptions, MessageBuffer, type Meta, type RequestIdOptions, type SingletonLogger, type TelegramConfig, TelegramTransport, type TimingResult, assertLogLevel, createMasker, createSingletonLogger, extractRequestId, flattenObject, formatLogfmt, formatLogfmtValue, generateRequestId, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync };
264
+ declare const LogLevelSchema: z.ZodType<LogLevel>;
265
+ declare const LogFormatSchema: z.ZodType<LogFormat>;
266
+ declare const LevelRuleSchema: z.ZodType<LevelRule>;
267
+ declare const ConsoleConfigSchema: z.ZodType<ConsoleConfig>;
268
+ declare const FileConfigSchema: z.ZodType<FileConfig>;
269
+ declare const HttpTransportBaseConfigSchema: z.ZodType<HttpTransportBaseConfig>;
270
+ declare const DiscordConfigSchema: z.ZodType<DiscordConfig>;
271
+ declare const TelegramConfigSchema: z.ZodType<TelegramConfig>;
272
+ declare const CloudWatchConfigSchema: z.ZodType<CloudWatchConfig>;
273
+ declare const LevelConfigObjectSchema: z.ZodType<LevelConfigObject>;
274
+ declare const LevelConfigSchema: z.ZodType<LevelConfig>;
275
+ declare const LoggerConfigSchema: z.ZodType<LoggerConfig>;
276
+ declare function validateConfig(config: unknown): z.infer<typeof LoggerConfigSchema>;
277
+ declare function safeValidateConfig(config: unknown): z.ZodSafeParseResult<LoggerConfig>;
278
+
279
+ export { BaseHttpTransport, type BaseHttpTransportOptions, type BufferOptions, type BufferedMessage, type CloudWatchConfig, CloudWatchConfigSchema, CloudWatchTransport, type ConsoleConfig, ConsoleConfigSchema, type DiscordConfig, DiscordConfigSchema, DiscordTransport, type FileConfig, FileConfigSchema, type HttpTransportBaseConfig, HttpTransportBaseConfigSchema, LOG_LEVELS, type LevelConfig, type LevelConfigObject, LevelConfigObjectSchema, LevelConfigSchema, type LevelOverride, type LevelOverrideMatch, type LevelRule, LevelRuleSchema, type LogFormat, LogFormatSchema, type LogLevel, LogLevelSchema, Logger, type LoggerConfig, LoggerConfigSchema, type LoggerContext, LoggerStore, type MaskSecretsOptions, MessageBuffer, type Meta, type RequestIdOptions, type SingletonLogger, type TelegramConfig, TelegramConfigSchema, TelegramTransport, type TimingResult, assertLogLevel, createMasker, createSingletonLogger, extractRequestId, flattenObject, formatLogfmt, formatLogfmtValue, generateRequestId, getOrGenerateRequestId, isValidLogLevel, maskSecrets, matchesContext, measureAsync, measureSync, safeValidateConfig, validateConfig };
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ var DailyRotateFile = require('winston-daily-rotate-file');
6
6
  var util = require('util');
7
7
  var TransportStream = require('winston-transport');
8
8
  var clientCloudwatchLogs = require('@aws-sdk/client-cloudwatch-logs');
9
+ var zod = require('zod');
9
10
  var crypto = require('crypto');
10
11
 
11
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -711,6 +712,10 @@ var CloudWatchTransport = class extends BaseHttpTransport {
711
712
  };
712
713
 
713
714
  // src/transports.ts
715
+ function toArray(value) {
716
+ if (!value) return [];
717
+ return Array.isArray(value) ? value : [value];
718
+ }
714
719
  function createTransports(config, store) {
715
720
  const result = [
716
721
  new winston.transports.Console({
@@ -736,26 +741,26 @@ function createTransports(config, store) {
736
741
  })
737
742
  );
738
743
  }
739
- if (config.discord) {
740
- const discord = new DiscordTransport(config.discord);
744
+ for (const discordConfig of toArray(config.discord)) {
745
+ const discord = new DiscordTransport(discordConfig);
741
746
  discord.format = winston.format.combine(
742
- createFilterFormat(config.discord.level, config.discord.rules, store),
747
+ createFilterFormat(discordConfig.level, discordConfig.rules, store),
743
748
  winston.format.timestamp()
744
749
  );
745
750
  result.push(discord);
746
751
  }
747
- if (config.telegram) {
748
- const telegram = new TelegramTransport(config.telegram);
752
+ for (const telegramConfig of toArray(config.telegram)) {
753
+ const telegram = new TelegramTransport(telegramConfig);
749
754
  telegram.format = winston.format.combine(
750
- createFilterFormat(config.telegram.level, config.telegram.rules, store),
755
+ createFilterFormat(telegramConfig.level, telegramConfig.rules, store),
751
756
  winston.format.timestamp()
752
757
  );
753
758
  result.push(telegram);
754
759
  }
755
- if (config.cloudwatch) {
756
- const cloudwatch = new CloudWatchTransport(config.cloudwatch);
760
+ for (const cloudwatchConfig of toArray(config.cloudwatch)) {
761
+ const cloudwatch = new CloudWatchTransport(cloudwatchConfig);
757
762
  cloudwatch.format = winston.format.combine(
758
- createFilterFormat(config.cloudwatch.level, config.cloudwatch.rules, store),
763
+ createFilterFormat(cloudwatchConfig.level, cloudwatchConfig.rules, store),
759
764
  winston.format.timestamp()
760
765
  );
761
766
  result.push(cloudwatch);
@@ -838,6 +843,111 @@ function matchesContext(storeContext, loggerContext, match) {
838
843
  const combined = { ...storeContext, context: loggerContext };
839
844
  return Object.entries(match).every(([key, value]) => combined[key] === value);
840
845
  }
846
+ var LogLevelSchema = zod.z.enum([
847
+ "off",
848
+ "error",
849
+ "warn",
850
+ "info",
851
+ "http",
852
+ "verbose",
853
+ "debug",
854
+ "silly"
855
+ ]);
856
+ var LogFormatSchema = zod.z.enum(["json", "plain", "logfmt", "simple"]);
857
+ var LevelRuleSchema = zod.z.object({
858
+ match: zod.z.record(zod.z.string(), zod.z.unknown()),
859
+ level: LogLevelSchema
860
+ });
861
+ var ConsoleConfigSchema = zod.z.object({
862
+ format: LogFormatSchema,
863
+ level: LogLevelSchema.optional(),
864
+ rules: zod.z.array(LevelRuleSchema).optional()
865
+ });
866
+ var FileConfigSchema = zod.z.object({
867
+ format: LogFormatSchema,
868
+ level: LogLevelSchema.optional(),
869
+ rules: zod.z.array(LevelRuleSchema).optional(),
870
+ dirname: zod.z.string().min(1, "dirname is required"),
871
+ filename: zod.z.string().min(1, "filename is required"),
872
+ datePattern: zod.z.string().optional(),
873
+ zippedArchive: zod.z.boolean().optional(),
874
+ maxSize: zod.z.string().optional(),
875
+ maxFiles: zod.z.string().optional()
876
+ });
877
+ var HttpTransportBaseConfigSchema = zod.z.object({
878
+ level: LogLevelSchema.optional(),
879
+ rules: zod.z.array(LevelRuleSchema).optional(),
880
+ batchSize: zod.z.number().int().positive().optional(),
881
+ flushInterval: zod.z.number().int().positive().optional(),
882
+ maxRetries: zod.z.number().int().nonnegative().optional(),
883
+ retryDelay: zod.z.number().int().positive().optional()
884
+ });
885
+ var DiscordConfigSchema = zod.z.object({
886
+ level: LogLevelSchema.optional(),
887
+ rules: zod.z.array(LevelRuleSchema).optional(),
888
+ batchSize: zod.z.number().int().positive().optional(),
889
+ flushInterval: zod.z.number().int().positive().optional(),
890
+ maxRetries: zod.z.number().int().nonnegative().optional(),
891
+ retryDelay: zod.z.number().int().positive().optional(),
892
+ webhookUrl: zod.z.string().url("webhookUrl must be a valid URL"),
893
+ username: zod.z.string().optional(),
894
+ avatarUrl: zod.z.string().url().optional(),
895
+ embedColors: zod.z.record(zod.z.string(), zod.z.number().int()).optional(),
896
+ includeTimestamp: zod.z.boolean().optional(),
897
+ includeMeta: zod.z.boolean().optional(),
898
+ maxEmbedFields: zod.z.number().int().min(1).max(25).optional()
899
+ });
900
+ var TelegramConfigSchema = zod.z.object({
901
+ level: LogLevelSchema.optional(),
902
+ rules: zod.z.array(LevelRuleSchema).optional(),
903
+ batchSize: zod.z.number().int().positive().optional(),
904
+ flushInterval: zod.z.number().int().positive().optional(),
905
+ maxRetries: zod.z.number().int().nonnegative().optional(),
906
+ retryDelay: zod.z.number().int().positive().optional(),
907
+ botToken: zod.z.string().min(1, "botToken is required"),
908
+ chatId: zod.z.union([zod.z.string(), zod.z.number()]),
909
+ parseMode: zod.z.enum(["Markdown", "MarkdownV2", "HTML"]).optional(),
910
+ disableNotification: zod.z.boolean().optional(),
911
+ threadId: zod.z.number().int().optional(),
912
+ replyToMessageId: zod.z.number().int().optional()
913
+ });
914
+ var CloudWatchConfigSchema = zod.z.object({
915
+ level: LogLevelSchema.optional(),
916
+ rules: zod.z.array(LevelRuleSchema).optional(),
917
+ batchSize: zod.z.number().int().positive().optional(),
918
+ flushInterval: zod.z.number().int().positive().optional(),
919
+ maxRetries: zod.z.number().int().nonnegative().optional(),
920
+ retryDelay: zod.z.number().int().positive().optional(),
921
+ logGroupName: zod.z.string().min(1, "logGroupName is required"),
922
+ logStreamName: zod.z.string().min(1, "logStreamName is required"),
923
+ region: zod.z.string().min(1, "region is required"),
924
+ accessKeyId: zod.z.string().min(1, "accessKeyId is required"),
925
+ secretAccessKey: zod.z.string().min(1, "secretAccessKey is required"),
926
+ createLogGroup: zod.z.boolean().optional(),
927
+ createLogStream: zod.z.boolean().optional()
928
+ });
929
+ var LevelConfigObjectSchema = zod.z.object({
930
+ default: LogLevelSchema,
931
+ rules: zod.z.array(LevelRuleSchema).optional()
932
+ });
933
+ var LevelConfigSchema = zod.z.union([
934
+ LogLevelSchema,
935
+ LevelConfigObjectSchema
936
+ ]);
937
+ var LoggerConfigSchema = zod.z.object({
938
+ level: LevelConfigSchema,
939
+ console: ConsoleConfigSchema,
940
+ file: FileConfigSchema.optional(),
941
+ discord: zod.z.union([DiscordConfigSchema, zod.z.array(DiscordConfigSchema)]).optional(),
942
+ telegram: zod.z.union([TelegramConfigSchema, zod.z.array(TelegramConfigSchema)]).optional(),
943
+ cloudwatch: zod.z.union([CloudWatchConfigSchema, zod.z.array(CloudWatchConfigSchema)]).optional()
944
+ });
945
+ function validateConfig(config) {
946
+ return LoggerConfigSchema.parse(config);
947
+ }
948
+ function safeValidateConfig(config) {
949
+ return LoggerConfigSchema.safeParse(config);
950
+ }
841
951
 
842
952
  // src/logger.ts
843
953
  var Logger = class _Logger {
@@ -846,7 +956,8 @@ var Logger = class _Logger {
846
956
  this.context = context;
847
957
  }
848
958
  static create(config, store) {
849
- const state = createState(config, store);
959
+ const validatedConfig = validateConfig(config);
960
+ const state = createState(validatedConfig, store);
850
961
  return new _Logger(state, "APP");
851
962
  }
852
963
  for(context) {
@@ -1027,12 +1138,24 @@ function getOrGenerateRequestId(headers, options = {}) {
1027
1138
  }
1028
1139
 
1029
1140
  exports.BaseHttpTransport = BaseHttpTransport;
1141
+ exports.CloudWatchConfigSchema = CloudWatchConfigSchema;
1030
1142
  exports.CloudWatchTransport = CloudWatchTransport;
1143
+ exports.ConsoleConfigSchema = ConsoleConfigSchema;
1144
+ exports.DiscordConfigSchema = DiscordConfigSchema;
1031
1145
  exports.DiscordTransport = DiscordTransport;
1146
+ exports.FileConfigSchema = FileConfigSchema;
1147
+ exports.HttpTransportBaseConfigSchema = HttpTransportBaseConfigSchema;
1032
1148
  exports.LOG_LEVELS = LOG_LEVELS;
1149
+ exports.LevelConfigObjectSchema = LevelConfigObjectSchema;
1150
+ exports.LevelConfigSchema = LevelConfigSchema;
1151
+ exports.LevelRuleSchema = LevelRuleSchema;
1152
+ exports.LogFormatSchema = LogFormatSchema;
1153
+ exports.LogLevelSchema = LogLevelSchema;
1033
1154
  exports.Logger = Logger;
1155
+ exports.LoggerConfigSchema = LoggerConfigSchema;
1034
1156
  exports.LoggerStore = LoggerStore;
1035
1157
  exports.MessageBuffer = MessageBuffer;
1158
+ exports.TelegramConfigSchema = TelegramConfigSchema;
1036
1159
  exports.TelegramTransport = TelegramTransport;
1037
1160
  exports.assertLogLevel = assertLogLevel;
1038
1161
  exports.createMasker = createMasker;
@@ -1048,5 +1171,7 @@ exports.maskSecrets = maskSecrets;
1048
1171
  exports.matchesContext = matchesContext;
1049
1172
  exports.measureAsync = measureAsync;
1050
1173
  exports.measureSync = measureSync;
1174
+ exports.safeValidateConfig = safeValidateConfig;
1175
+ exports.validateConfig = validateConfig;
1051
1176
  //# sourceMappingURL=index.js.map
1052
1177
  //# sourceMappingURL=index.js.map