@leanstacks/lambda-utils 0.1.0-alpha.2 → 0.1.0-alpha.3

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.d.ts CHANGED
@@ -1 +1 @@
1
- export { getLogger, initializeLogger, LoggerConfig, withRequestTracking } from './logging/logger';
1
+ export { Logger, LoggerConfig, withRequestTracking } from './logging/logger';
package/dist/index.js CHANGED
@@ -2,10 +2,11 @@ import pino from 'pino';
2
2
  import { lambdaRequestTracker, StructuredLogFormatter, CloudwatchLogFormatter, pinoLambdaDestination } from 'pino-lambda';
3
3
 
4
4
  /**
5
- * Middleware to track AWS Lambda requests
5
+ * Logger middleware which adds AWS Lambda attributes to log messages.
6
+ *
6
7
  * @example
7
8
  * ```typescript
8
- * import { withRequestTracking } from '@utils/logging/logger';
9
+ * import { withRequestTracking } from '@leanstacks/lambda-utils';
9
10
  *
10
11
  * export const handler = async (event, context) => {
11
12
  * withRequestTracking(event, context);
@@ -16,75 +17,62 @@ import { lambdaRequestTracker, StructuredLogFormatter, CloudwatchLogFormatter, p
16
17
  */
17
18
  const withRequestTracking = lambdaRequestTracker();
18
19
  /**
19
- * Module-level state to store logger configuration
20
- */
21
- let _loggerConfig = {
22
- enabled: true,
23
- level: 'info',
24
- format: 'json',
25
- };
26
- /**
27
- * Module-level cache for the logger instance
28
- */
29
- let _loggerInstance = null;
30
- /**
31
- * Create and return the Pino Lambda logger instance
32
- * Uses the configuration set by initializeLogger
33
- * Logger instance is cached after first creation
34
- */
35
- const _createLogger = () => {
36
- const formatter = _loggerConfig.format === 'json' ? new StructuredLogFormatter() : new CloudwatchLogFormatter();
37
- const lambdaDestination = pinoLambdaDestination({
38
- formatter,
39
- });
40
- return pino({
41
- enabled: _loggerConfig.enabled,
42
- level: _loggerConfig.level,
43
- }, lambdaDestination);
44
- };
45
- /**
46
- * Initialize the logger with configuration
47
- * Should be called once at Lambda handler entry point
48
- * Invalidates the cached logger instance so a new one is created with the updated config
49
- *
50
- * @param config Logger configuration options
51
- * @returns void
52
- *
53
- * @example
54
- * ```typescript
55
- * import { initializeLogger } from '@utils/logging/logger';
56
- *
57
- * initializeLogger({
58
- * enabled: true,
59
- * level: 'debug',
60
- * format: 'json',
61
- * });
62
- * ```
63
- */
64
- const initializeLogger = (config) => {
65
- _loggerConfig = {
66
- enabled: config.enabled ?? true,
67
- level: config.level ?? 'info',
68
- format: config.format ?? 'json',
69
- };
70
- // Invalidate the cached logger instance so a new one is created with updated config
71
- _loggerInstance = null;
72
- };
73
- /**
74
- * Get the logger instance
20
+ * Logger class which provides a Pino logger instance with AWS Lambda attributes.
75
21
  *
76
22
  * @example
77
23
  * ```typescript
78
- * import { logger } from '@utils/logging/logger';
24
+ * import { Logger } from '@leanstacks/lambda-utils';
25
+ * const logger = new Logger().instance;
79
26
  *
80
27
  * logger.info('Hello, world!');
81
28
  * ```
82
29
  */
83
- const getLogger = () => {
84
- if (_loggerInstance === null) {
85
- _loggerInstance = _createLogger();
30
+ class Logger {
31
+ constructor(config) {
32
+ this._loggerConfig = {
33
+ enabled: true,
34
+ level: 'info',
35
+ format: 'json',
36
+ };
37
+ this._instance = null;
38
+ /**
39
+ * Creates a new, fully configured Pino logger instance.
40
+ */
41
+ this._createLogger = () => {
42
+ const formatter = this._loggerConfig.format === 'json' ? new StructuredLogFormatter() : new CloudwatchLogFormatter();
43
+ const lambdaDestination = pinoLambdaDestination({
44
+ formatter,
45
+ });
46
+ return pino({
47
+ enabled: this._loggerConfig.enabled,
48
+ level: this._loggerConfig.level,
49
+ }, lambdaDestination);
50
+ };
51
+ if (config) {
52
+ this._loggerConfig = {
53
+ enabled: config.enabled ?? true,
54
+ level: config.level ?? 'info',
55
+ format: config.format ?? 'json',
56
+ };
57
+ }
58
+ }
59
+ /**
60
+ * Get the logger instance.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * import { Logger } from '@leanstacks/lambda-utils';
65
+ * const logger = new Logger().instance;
66
+ *
67
+ * logger.info('Hello, world!');
68
+ * ```
69
+ */
70
+ get instance() {
71
+ if (this._instance === null) {
72
+ this._instance = this._createLogger();
73
+ }
74
+ return this._instance;
86
75
  }
87
- return _loggerInstance;
88
- };
76
+ }
89
77
 
90
- export { getLogger, initializeLogger, withRequestTracking };
78
+ export { Logger, withRequestTracking };
@@ -1,9 +1,10 @@
1
1
  import pino from 'pino';
2
2
  /**
3
- * Middleware to track AWS Lambda requests
3
+ * Logger middleware which adds AWS Lambda attributes to log messages.
4
+ *
4
5
  * @example
5
6
  * ```typescript
6
- * import { withRequestTracking } from '@utils/logging/logger';
7
+ * import { withRequestTracking } from '@leanstacks/lambda-utils';
7
8
  *
8
9
  * export const handler = async (event, context) => {
9
10
  * withRequestTracking(event, context);
@@ -14,7 +15,7 @@ import pino from 'pino';
14
15
  */
15
16
  export declare const withRequestTracking: (event: import("pino-lambda").LambdaEvent, context: import("pino-lambda").LambdaContext) => void;
16
17
  /**
17
- * Configuration options for the Pino Lambda logger
18
+ * Configuration options for the Logger
18
19
  */
19
20
  export interface LoggerConfig {
20
21
  /** Whether logging is enabled */
@@ -25,33 +26,34 @@ export interface LoggerConfig {
25
26
  format?: 'json' | 'text';
26
27
  }
27
28
  /**
28
- * Initialize the logger with configuration
29
- * Should be called once at Lambda handler entry point
30
- * Invalidates the cached logger instance so a new one is created with the updated config
31
- *
32
- * @param config Logger configuration options
33
- * @returns void
34
- *
35
- * @example
36
- * ```typescript
37
- * import { initializeLogger } from '@utils/logging/logger';
38
- *
39
- * initializeLogger({
40
- * enabled: true,
41
- * level: 'debug',
42
- * format: 'json',
43
- * });
44
- * ```
45
- */
46
- export declare const initializeLogger: (config: LoggerConfig) => void;
47
- /**
48
- * Get the logger instance
29
+ * Logger class which provides a Pino logger instance with AWS Lambda attributes.
49
30
  *
50
31
  * @example
51
32
  * ```typescript
52
- * import { logger } from '@utils/logging/logger';
33
+ * import { Logger } from '@leanstacks/lambda-utils';
34
+ * const logger = new Logger().instance;
53
35
  *
54
36
  * logger.info('Hello, world!');
55
37
  * ```
56
38
  */
57
- export declare const getLogger: () => pino.Logger;
39
+ export declare class Logger {
40
+ private _loggerConfig;
41
+ private _instance;
42
+ constructor(config?: LoggerConfig);
43
+ /**
44
+ * Creates a new, fully configured Pino logger instance.
45
+ */
46
+ private _createLogger;
47
+ /**
48
+ * Get the logger instance.
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import { Logger } from '@leanstacks/lambda-utils';
53
+ * const logger = new Logger().instance;
54
+ *
55
+ * logger.info('Hello, world!');
56
+ * ```
57
+ */
58
+ get instance(): pino.Logger;
59
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leanstacks/lambda-utils",
3
- "version": "0.1.0-alpha.2",
3
+ "version": "0.1.0-alpha.3",
4
4
  "description": "A collection of utilities and helper functions designed to streamline the development of AWS Lambda functions using TypeScript.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export { getLogger, initializeLogger, LoggerConfig, withRequestTracking } from './logging/logger';
1
+ export { Logger, LoggerConfig, withRequestTracking } from './logging/logger';
@@ -7,10 +7,11 @@ import {
7
7
  } from 'pino-lambda';
8
8
 
9
9
  /**
10
- * Middleware to track AWS Lambda requests
10
+ * Logger middleware which adds AWS Lambda attributes to log messages.
11
+ *
11
12
  * @example
12
13
  * ```typescript
13
- * import { withRequestTracking } from '@utils/logging/logger';
14
+ * import { withRequestTracking } from '@leanstacks/lambda-utils';
14
15
  *
15
16
  * export const handler = async (event, context) => {
16
17
  * withRequestTracking(event, context);
@@ -22,7 +23,7 @@ import {
22
23
  export const withRequestTracking = lambdaRequestTracker();
23
24
 
24
25
  /**
25
- * Configuration options for the Pino Lambda logger
26
+ * Configuration options for the Logger
26
27
  */
27
28
  export interface LoggerConfig {
28
29
  /** Whether logging is enabled */
@@ -34,82 +35,70 @@ export interface LoggerConfig {
34
35
  }
35
36
 
36
37
  /**
37
- * Module-level state to store logger configuration
38
- */
39
- let _loggerConfig: LoggerConfig = {
40
- enabled: true,
41
- level: 'info',
42
- format: 'json',
43
- };
44
-
45
- /**
46
- * Module-level cache for the logger instance
47
- */
48
- let _loggerInstance: pino.Logger | null = null;
49
-
50
- /**
51
- * Create and return the Pino Lambda logger instance
52
- * Uses the configuration set by initializeLogger
53
- * Logger instance is cached after first creation
54
- */
55
- const _createLogger = (): pino.Logger => {
56
- const formatter = _loggerConfig.format === 'json' ? new StructuredLogFormatter() : new CloudwatchLogFormatter();
57
-
58
- const lambdaDestination = pinoLambdaDestination({
59
- formatter,
60
- });
61
-
62
- return pino(
63
- {
64
- enabled: _loggerConfig.enabled,
65
- level: _loggerConfig.level,
66
- },
67
- lambdaDestination,
68
- );
69
- };
70
-
71
- /**
72
- * Initialize the logger with configuration
73
- * Should be called once at Lambda handler entry point
74
- * Invalidates the cached logger instance so a new one is created with the updated config
75
- *
76
- * @param config Logger configuration options
77
- * @returns void
38
+ * Logger class which provides a Pino logger instance with AWS Lambda attributes.
78
39
  *
79
40
  * @example
80
41
  * ```typescript
81
- * import { initializeLogger } from '@utils/logging/logger';
42
+ * import { Logger } from '@leanstacks/lambda-utils';
43
+ * const logger = new Logger().instance;
82
44
  *
83
- * initializeLogger({
84
- * enabled: true,
85
- * level: 'debug',
86
- * format: 'json',
87
- * });
45
+ * logger.info('Hello, world!');
88
46
  * ```
89
47
  */
90
- export const initializeLogger = (config: LoggerConfig): void => {
91
- _loggerConfig = {
92
- enabled: config.enabled ?? true,
93
- level: config.level ?? 'info',
94
- format: config.format ?? 'json',
48
+ export class Logger {
49
+ private _loggerConfig: LoggerConfig = {
50
+ enabled: true,
51
+ level: 'info',
52
+ format: 'json',
95
53
  };
96
- // Invalidate the cached logger instance so a new one is created with updated config
97
- _loggerInstance = null;
98
- };
99
54
 
100
- /**
101
- * Get the logger instance
102
- *
103
- * @example
104
- * ```typescript
105
- * import { logger } from '@utils/logging/logger';
106
- *
107
- * logger.info('Hello, world!');
108
- * ```
109
- */
110
- export const getLogger = (): pino.Logger => {
111
- if (_loggerInstance === null) {
112
- _loggerInstance = _createLogger();
55
+ private _instance: pino.Logger | null = null;
56
+
57
+ constructor(config?: LoggerConfig) {
58
+ if (config) {
59
+ this._loggerConfig = {
60
+ enabled: config.enabled ?? true,
61
+ level: config.level ?? 'info',
62
+ format: config.format ?? 'json',
63
+ };
64
+ }
113
65
  }
114
- return _loggerInstance;
115
- };
66
+
67
+ /**
68
+ * Creates a new, fully configured Pino logger instance.
69
+ */
70
+ private _createLogger = (): pino.Logger => {
71
+ const formatter =
72
+ this._loggerConfig.format === 'json' ? new StructuredLogFormatter() : new CloudwatchLogFormatter();
73
+
74
+ const lambdaDestination = pinoLambdaDestination({
75
+ formatter,
76
+ });
77
+
78
+ return pino(
79
+ {
80
+ enabled: this._loggerConfig.enabled,
81
+ level: this._loggerConfig.level,
82
+ },
83
+ lambdaDestination,
84
+ );
85
+ };
86
+
87
+ /**
88
+ * Get the logger instance.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * import { Logger } from '@leanstacks/lambda-utils';
93
+ * const logger = new Logger().instance;
94
+ *
95
+ * logger.info('Hello, world!');
96
+ * ```
97
+ */
98
+ get instance(): pino.Logger {
99
+ if (this._instance === null) {
100
+ this._instance = this._createLogger();
101
+ }
102
+ return this._instance;
103
+ }
104
+ }