@geekmidas/logger 0.0.1 → 0.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.
Files changed (40) hide show
  1. package/README.md +114 -7
  2. package/dist/console.cjs +75 -17
  3. package/dist/console.cjs.map +1 -1
  4. package/dist/console.d.cts +31 -7
  5. package/dist/console.d.mts +31 -7
  6. package/dist/console.mjs +76 -18
  7. package/dist/console.mjs.map +1 -1
  8. package/dist/index.d.cts +2 -2
  9. package/dist/index.d.mts +2 -2
  10. package/dist/pino.cjs +42 -1
  11. package/dist/pino.cjs.map +1 -1
  12. package/dist/pino.d.cts +23 -3
  13. package/dist/pino.d.mts +23 -3
  14. package/dist/pino.mjs +42 -2
  15. package/dist/pino.mjs.map +1 -1
  16. package/dist/redact-paths-Br-tI2GZ.d.cts +18 -0
  17. package/dist/redact-paths-CIsuxHH7.d.mts +18 -0
  18. package/dist/redact-paths-D0m0DIuQ.cjs +73 -0
  19. package/dist/redact-paths-D0m0DIuQ.cjs.map +1 -0
  20. package/dist/redact-paths-DQoIXhkS.mjs +67 -0
  21. package/dist/redact-paths-DQoIXhkS.mjs.map +1 -0
  22. package/dist/redact-paths.cjs +3 -0
  23. package/dist/redact-paths.d.cts +2 -0
  24. package/dist/redact-paths.d.mts +2 -0
  25. package/dist/redact-paths.mjs +3 -0
  26. package/dist/{types-C1RfRbo6.d.mts → types-Bga8WDuP.d.mts} +86 -2
  27. package/dist/{types-DXdmn7h5.d.cts → types-JxCFymH0.d.cts} +86 -2
  28. package/dist/types-ag_0Cvbg.cjs.map +1 -1
  29. package/dist/types-yQ6XOihF.mjs.map +1 -1
  30. package/dist/types.d.cts +2 -2
  31. package/dist/types.d.mts +2 -2
  32. package/package.json +10 -1
  33. package/src/__tests__/console.spec.ts +277 -140
  34. package/src/__tests__/pino-redaction.integration.spec.ts +307 -0
  35. package/src/__tests__/pino.spec.ts +199 -0
  36. package/src/console.ts +95 -23
  37. package/src/index.ts +1 -0
  38. package/src/pino.ts +105 -2
  39. package/src/redact-paths.ts +71 -0
  40. package/src/types.ts +87 -0
package/src/pino.ts CHANGED
@@ -1,7 +1,105 @@
1
+ /**
2
+ * Pino logger with built-in redaction support for sensitive data.
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * import { createLogger, DEFAULT_REDACT_PATHS } from '@geekmidas/logger/pino';
7
+ *
8
+ * // Enable redaction with sensible defaults
9
+ * const logger = createLogger({ redact: true });
10
+ *
11
+ * // Sensitive data is automatically masked
12
+ * logger.info({ password: 'secret123', user: 'john' }, 'Login');
13
+ * // Output: { password: '[Redacted]', user: 'john' } Login
14
+ *
15
+ * // Add custom paths (merged with defaults)
16
+ * const logger2 = createLogger({ redact: ['user.ssn'] });
17
+ *
18
+ * // Override defaults for full control
19
+ * const logger3 = createLogger({
20
+ * redact: {
21
+ * paths: ['onlyThis'],
22
+ * resolution: 'override',
23
+ * }
24
+ * });
25
+ * ```
26
+ *
27
+ * @module
28
+ */
1
29
  import { pino } from 'pino';
2
- import type { CreateLoggerOptions } from './types';
30
+ import { DEFAULT_REDACT_PATHS } from './redact-paths';
31
+ import type { CreateLoggerOptions, RedactOptions } from './types';
3
32
 
4
- export function createLogger(options: CreateLoggerOptions) {
33
+ // Re-export for backwards compatibility
34
+ export { DEFAULT_REDACT_PATHS } from './redact-paths';
35
+
36
+ /**
37
+ * Type for the resolved pino redact config (without our custom resolution field).
38
+ */
39
+ type PinoRedactConfig =
40
+ | string[]
41
+ | {
42
+ paths: string[];
43
+ censor?: string | ((value: unknown, path: string[]) => unknown);
44
+ remove?: boolean;
45
+ };
46
+
47
+ /**
48
+ * Resolves redaction configuration from options.
49
+ * Returns undefined if redaction is disabled, or a pino-compatible redact config.
50
+ *
51
+ * By default (resolution: 'merge'), custom paths are merged with DEFAULT_REDACT_PATHS.
52
+ * With resolution: 'override', only the custom paths are used.
53
+ */
54
+ function resolveRedactConfig(
55
+ redact: boolean | RedactOptions | undefined,
56
+ ): PinoRedactConfig | undefined {
57
+ if (redact === undefined || redact === false) {
58
+ return undefined;
59
+ }
60
+
61
+ if (redact === true) {
62
+ return DEFAULT_REDACT_PATHS;
63
+ }
64
+
65
+ // Array syntax - merge with defaults
66
+ if (Array.isArray(redact)) {
67
+ return [...DEFAULT_REDACT_PATHS, ...redact];
68
+ }
69
+
70
+ // Object syntax - check resolution mode
71
+ const { resolution = 'merge', paths, censor, remove } = redact;
72
+
73
+ const resolvedPaths =
74
+ resolution === 'override' ? paths : [...DEFAULT_REDACT_PATHS, ...paths];
75
+
76
+ // Return clean pino config without our resolution field
77
+ const config: PinoRedactConfig = { paths: resolvedPaths };
78
+ if (censor !== undefined) config.censor = censor;
79
+ if (remove !== undefined) config.remove = remove;
80
+
81
+ return config;
82
+ }
83
+
84
+ /**
85
+ * Creates a pino logger instance with optional redaction support.
86
+ *
87
+ * @param options - Logger configuration options
88
+ * @returns A configured pino logger instance
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * // Basic logger
93
+ * const logger = createLogger({ level: 'debug' });
94
+ *
95
+ * // With redaction enabled
96
+ * const secureLogger = createLogger({ redact: true });
97
+ *
98
+ * // Pretty printing in development
99
+ * const devLogger = createLogger({ pretty: true, redact: true });
100
+ * ```
101
+ */
102
+ export function createLogger(options: CreateLoggerOptions = {}) {
5
103
  // @ts-ignore
6
104
  const pretty = options?.pretty && process.NODE_ENV !== 'production';
7
105
  const baseOptions = pretty
@@ -12,8 +110,13 @@ export function createLogger(options: CreateLoggerOptions) {
12
110
  },
13
111
  }
14
112
  : {};
113
+
114
+ const redact = resolveRedactConfig(options.redact);
115
+
15
116
  return pino({
16
117
  ...baseOptions,
118
+ ...(options.level && { level: options.level }),
119
+ ...(redact && { redact }),
17
120
  formatters: {
18
121
  bindings() {
19
122
  return { nodeVersion: process.version };
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Default sensitive field paths for redaction.
3
+ *
4
+ * These paths are automatically used when `redact: true` is set,
5
+ * and merged with custom paths unless `resolution: 'override'` is specified.
6
+ *
7
+ * Includes:
8
+ * - Authentication: password, token, apiKey, authorization, credentials
9
+ * - Headers: authorization, cookie, x-api-key, x-auth-token
10
+ * - Personal data: ssn, creditCard, cvv, pin
11
+ * - Secrets: secret, connectionString, databaseUrl
12
+ * - Wildcards: *.password, *.secret, *.token (catches nested fields)
13
+ */
14
+ export const DEFAULT_REDACT_PATHS: string[] = [
15
+ // Authentication & authorization
16
+ 'password',
17
+ 'pass',
18
+ 'passwd',
19
+ 'secret',
20
+ 'token',
21
+ 'accessToken',
22
+ 'refreshToken',
23
+ 'idToken',
24
+ 'apiKey',
25
+ 'api_key',
26
+ 'apikey',
27
+ 'auth',
28
+ 'authorization',
29
+ 'credential',
30
+ 'credentials',
31
+
32
+ // Common nested patterns (headers, body, etc.)
33
+ '*.password',
34
+ '*.secret',
35
+ '*.token',
36
+ '*.apiKey',
37
+ '*.api_key',
38
+ '*.authorization',
39
+ '*.accessToken',
40
+ '*.refreshToken',
41
+
42
+ // HTTP headers (case variations)
43
+ 'headers.authorization',
44
+ 'headers.Authorization',
45
+ 'headers["authorization"]',
46
+ 'headers["Authorization"]',
47
+ 'headers.cookie',
48
+ 'headers.Cookie',
49
+ 'headers["x-api-key"]',
50
+ 'headers["X-Api-Key"]',
51
+ 'headers["x-auth-token"]',
52
+ 'headers["X-Auth-Token"]',
53
+
54
+ // Common sensitive data fields
55
+ 'ssn',
56
+ 'socialSecurityNumber',
57
+ 'social_security_number',
58
+ 'creditCard',
59
+ 'credit_card',
60
+ 'cardNumber',
61
+ 'card_number',
62
+ 'cvv',
63
+ 'cvc',
64
+ 'pin',
65
+
66
+ // Database & connection strings
67
+ 'connectionString',
68
+ 'connection_string',
69
+ 'databaseUrl',
70
+ 'database_url',
71
+ ];
package/src/types.ts CHANGED
@@ -74,7 +74,94 @@ export enum LogLevel {
74
74
  Silent = 'silent',
75
75
  }
76
76
 
77
+ /**
78
+ * Redaction configuration for masking sensitive data in logs.
79
+ * Uses pino's fast-redact library under the hood.
80
+ *
81
+ * By default, custom paths are merged with the default sensitive paths.
82
+ * Use `resolution: 'override'` to use only your custom paths.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * // Simple path array (merges with defaults)
87
+ * redact: ['user.ssn', 'custom.field']
88
+ *
89
+ * // Override defaults completely
90
+ * redact: {
91
+ * paths: ['only.these.paths'],
92
+ * resolution: 'override',
93
+ * }
94
+ *
95
+ * // With custom censor
96
+ * redact: {
97
+ * paths: ['extra.secret'],
98
+ * censor: '***',
99
+ * }
100
+ *
101
+ * // Remove fields entirely
102
+ * redact: {
103
+ * paths: ['temporary.data'],
104
+ * remove: true,
105
+ * }
106
+ * ```
107
+ */
108
+ export type RedactOptions =
109
+ | string[]
110
+ | {
111
+ /** Paths to redact using dot notation or bracket notation for special chars */
112
+ paths: string[];
113
+ /** Custom replacement text (default: '[REDACTED]') */
114
+ censor?: string | ((value: unknown, path: string[]) => unknown);
115
+ /** Remove the field entirely instead of replacing (default: false) */
116
+ remove?: boolean;
117
+ /**
118
+ * How to combine custom paths with default sensitive paths.
119
+ * - 'merge': Custom paths are added to default paths (default)
120
+ * - 'override': Only custom paths are used, defaults are ignored
121
+ */
122
+ resolution?: 'merge' | 'override';
123
+ };
124
+
77
125
  export type CreateLoggerOptions = {
126
+ /** Enable pretty printing with colors (disabled in production) */
78
127
  pretty?: boolean;
128
+ /** Minimum log level to output */
79
129
  level?: LogLevel;
130
+ /**
131
+ * Redaction configuration for masking sensitive data.
132
+ *
133
+ * - `true`: Uses default sensitive paths (password, token, secret, etc.)
134
+ * - `false` or `undefined`: No redaction applied
135
+ * - `string[]`: Custom paths merged with defaults
136
+ * - `object`: Advanced config with paths, censor, remove, and resolution options
137
+ *
138
+ * By default, custom paths are **merged** with the default sensitive paths.
139
+ * Use `resolution: 'override'` to disable defaults and use only your paths.
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * // Use defaults only
144
+ * createLogger({ redact: true });
145
+ *
146
+ * // Add custom paths (merged with defaults)
147
+ * createLogger({ redact: ['user.ssn', 'custom.field'] });
148
+ *
149
+ * // Override defaults completely
150
+ * createLogger({
151
+ * redact: {
152
+ * paths: ['only.these.paths'],
153
+ * resolution: 'override',
154
+ * }
155
+ * });
156
+ *
157
+ * // Merge with custom censor
158
+ * createLogger({
159
+ * redact: {
160
+ * paths: ['extra.secret'],
161
+ * censor: '***',
162
+ * }
163
+ * });
164
+ * ```
165
+ */
166
+ redact?: boolean | RedactOptions;
80
167
  };