@leanstacks/lambda-utils 0.1.0-alpha.1 → 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 { initializeLogger, logger, LoggerConfig } from './logging/logger';
1
+ export { Logger, LoggerConfig, withRequestTracking } from './logging/logger';
package/dist/index.js CHANGED
@@ -1,75 +1,78 @@
1
1
  import pino from 'pino';
2
- import { StructuredLogFormatter, pinoLambdaDestination, CloudwatchLogFormatter } from 'pino-lambda';
2
+ import { lambdaRequestTracker, StructuredLogFormatter, CloudwatchLogFormatter, pinoLambdaDestination } from 'pino-lambda';
3
3
 
4
4
  /**
5
- * Module-level state to store logger configuration
6
- */
7
- let _loggerConfig = {
8
- enabled: true,
9
- level: 'info',
10
- format: 'json',
11
- };
12
- /**
13
- * Module-level cache for the logger instance
14
- */
15
- let _loggerInstance = null;
16
- /**
17
- * Create and return the Pino Lambda logger instance
18
- * Uses the configuration set by initializeLogger
19
- * Logger instance is cached after first creation
20
- */
21
- const _createLogger = () => {
22
- const formatter = _loggerConfig.format === 'json' ? new StructuredLogFormatter() : new CloudwatchLogFormatter();
23
- const lambdaDestination = pinoLambdaDestination({
24
- formatter,
25
- });
26
- return pino({
27
- enabled: _loggerConfig.enabled,
28
- level: _loggerConfig.level,
29
- }, lambdaDestination);
30
- };
31
- /**
32
- * Get the cached logger instance, creating it if necessary
33
- */
34
- const _getLogger = () => {
35
- if (_loggerInstance === null) {
36
- _loggerInstance = _createLogger();
37
- }
38
- return _loggerInstance;
39
- };
40
- /**
41
- * Initialize the logger with configuration
42
- * Should be called once at Lambda handler entry point
43
- * Invalidates the cached logger instance so a new one is created with the updated config
44
- *
45
- * @param config Logger configuration options
46
- * @returns void
5
+ * Logger middleware which adds AWS Lambda attributes to log messages.
47
6
  *
48
7
  * @example
49
8
  * ```typescript
50
- * import { initializeLogger } from '@utils/logging/logger';
9
+ * import { withRequestTracking } from '@leanstacks/lambda-utils';
10
+ *
11
+ * export const handler = async (event, context) => {
12
+ * withRequestTracking(event, context);
51
13
  *
52
- * initializeLogger({
53
- * enabled: true,
54
- * level: 'debug',
55
- * format: 'json',
56
- * });
14
+ * // Your Lambda handler logic here
15
+ * };
57
16
  * ```
58
17
  */
59
- const initializeLogger = (config) => {
60
- _loggerConfig = {
61
- enabled: config.enabled ?? true,
62
- level: config.level ?? 'info',
63
- format: config.format ?? 'json',
64
- };
65
- // Invalidate the cached logger instance so a new one is created with updated config
66
- _loggerInstance = null;
67
- };
18
+ const withRequestTracking = lambdaRequestTracker();
68
19
  /**
69
- * Pino logger instance
70
- * Configuration is supplied via initializeLogger() and persists across imports
71
- * Logger instance is cached after first creation for reuse
20
+ * Logger class which provides a Pino logger instance with AWS Lambda attributes.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * import { Logger } from '@leanstacks/lambda-utils';
25
+ * const logger = new Logger().instance;
26
+ *
27
+ * logger.info('Hello, world!');
28
+ * ```
72
29
  */
73
- const logger = _getLogger();
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;
75
+ }
76
+ }
74
77
 
75
- export { initializeLogger, logger };
78
+ export { Logger, withRequestTracking };
@@ -1,6 +1,21 @@
1
1
  import pino from 'pino';
2
2
  /**
3
- * Configuration options for the Pino Lambda logger
3
+ * Logger middleware which adds AWS Lambda attributes to log messages.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * import { withRequestTracking } from '@leanstacks/lambda-utils';
8
+ *
9
+ * export const handler = async (event, context) => {
10
+ * withRequestTracking(event, context);
11
+ *
12
+ * // Your Lambda handler logic here
13
+ * };
14
+ * ```
15
+ */
16
+ export declare const withRequestTracking: (event: import("pino-lambda").LambdaEvent, context: import("pino-lambda").LambdaContext) => void;
17
+ /**
18
+ * Configuration options for the Logger
4
19
  */
5
20
  export interface LoggerConfig {
6
21
  /** Whether logging is enabled */
@@ -11,28 +26,34 @@ export interface LoggerConfig {
11
26
  format?: 'json' | 'text';
12
27
  }
13
28
  /**
14
- * Initialize the logger with configuration
15
- * Should be called once at Lambda handler entry point
16
- * Invalidates the cached logger instance so a new one is created with the updated config
17
- *
18
- * @param config Logger configuration options
19
- * @returns void
29
+ * Logger class which provides a Pino logger instance with AWS Lambda attributes.
20
30
  *
21
31
  * @example
22
32
  * ```typescript
23
- * import { initializeLogger } from '@utils/logging/logger';
33
+ * import { Logger } from '@leanstacks/lambda-utils';
34
+ * const logger = new Logger().instance;
24
35
  *
25
- * initializeLogger({
26
- * enabled: true,
27
- * level: 'debug',
28
- * format: 'json',
29
- * });
36
+ * logger.info('Hello, world!');
30
37
  * ```
31
38
  */
32
- export declare const initializeLogger: (config: LoggerConfig) => void;
33
- /**
34
- * Pino logger instance
35
- * Configuration is supplied via initializeLogger() and persists across imports
36
- * Logger instance is cached after first creation for reuse
37
- */
38
- export declare const logger: pino.Logger<never, boolean>;
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.1",
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 { initializeLogger, logger, LoggerConfig } from './logging/logger';
1
+ export { Logger, LoggerConfig, withRequestTracking } from './logging/logger';
@@ -1,8 +1,29 @@
1
1
  import pino from 'pino';
2
- import { CloudwatchLogFormatter, pinoLambdaDestination, StructuredLogFormatter } from 'pino-lambda';
2
+ import {
3
+ CloudwatchLogFormatter,
4
+ lambdaRequestTracker,
5
+ pinoLambdaDestination,
6
+ StructuredLogFormatter,
7
+ } from 'pino-lambda';
3
8
 
4
9
  /**
5
- * Configuration options for the Pino Lambda logger
10
+ * Logger middleware which adds AWS Lambda attributes to log messages.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { withRequestTracking } from '@leanstacks/lambda-utils';
15
+ *
16
+ * export const handler = async (event, context) => {
17
+ * withRequestTracking(event, context);
18
+ *
19
+ * // Your Lambda handler logic here
20
+ * };
21
+ * ```
22
+ */
23
+ export const withRequestTracking = lambdaRequestTracker();
24
+
25
+ /**
26
+ * Configuration options for the Logger
6
27
  */
7
28
  export interface LoggerConfig {
8
29
  /** Whether logging is enabled */
@@ -14,82 +35,70 @@ export interface LoggerConfig {
14
35
  }
15
36
 
16
37
  /**
17
- * Module-level state to store logger configuration
18
- */
19
- let _loggerConfig: LoggerConfig = {
20
- enabled: true,
21
- level: 'info',
22
- format: 'json',
23
- };
24
-
25
- /**
26
- * Module-level cache for the logger instance
27
- */
28
- let _loggerInstance: pino.Logger | null = null;
29
-
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 = (): pino.Logger => {
36
- const formatter = _loggerConfig.format === 'json' ? new StructuredLogFormatter() : new CloudwatchLogFormatter();
37
-
38
- const lambdaDestination = pinoLambdaDestination({
39
- formatter,
40
- });
41
-
42
- return pino(
43
- {
44
- enabled: _loggerConfig.enabled,
45
- level: _loggerConfig.level,
46
- },
47
- lambdaDestination,
48
- );
49
- };
50
-
51
- /**
52
- * Get the cached logger instance, creating it if necessary
53
- */
54
- const _getLogger = (): pino.Logger => {
55
- if (_loggerInstance === null) {
56
- _loggerInstance = _createLogger();
57
- }
58
- return _loggerInstance;
59
- };
60
-
61
- /**
62
- * Initialize the logger with configuration
63
- * Should be called once at Lambda handler entry point
64
- * Invalidates the cached logger instance so a new one is created with the updated config
65
- *
66
- * @param config Logger configuration options
67
- * @returns void
38
+ * Logger class which provides a Pino logger instance with AWS Lambda attributes.
68
39
  *
69
40
  * @example
70
41
  * ```typescript
71
- * import { initializeLogger } from '@utils/logging/logger';
42
+ * import { Logger } from '@leanstacks/lambda-utils';
43
+ * const logger = new Logger().instance;
72
44
  *
73
- * initializeLogger({
74
- * enabled: true,
75
- * level: 'debug',
76
- * format: 'json',
77
- * });
45
+ * logger.info('Hello, world!');
78
46
  * ```
79
47
  */
80
- export const initializeLogger = (config: LoggerConfig): void => {
81
- _loggerConfig = {
82
- enabled: config.enabled ?? true,
83
- level: config.level ?? 'info',
84
- format: config.format ?? 'json',
48
+ export class Logger {
49
+ private _loggerConfig: LoggerConfig = {
50
+ enabled: true,
51
+ level: 'info',
52
+ format: 'json',
85
53
  };
86
- // Invalidate the cached logger instance so a new one is created with updated config
87
- _loggerInstance = null;
88
- };
89
54
 
90
- /**
91
- * Pino logger instance
92
- * Configuration is supplied via initializeLogger() and persists across imports
93
- * Logger instance is cached after first creation for reuse
94
- */
95
- export const logger = _getLogger();
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
+ }
65
+ }
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
+ }