|
1 | 1 | import { injectLambdaContext, Logger } from '../../src'; |
2 | 2 | import { Context, APIGatewayAuthorizerResult } from 'aws-lambda'; |
| 3 | +import { TestEvent, TestOutput } from '../helpers/types'; |
3 | 4 | import middy from '@middy/core'; |
4 | 5 |
|
5 | | -const PERSISTENT_KEY = process.env.PERSISTENT_KEY; |
6 | | -const PERSISTENT_KEY_FIRST_INVOCATION_ONLY = process.env.PERSISTENT_KEY_FIRST_INVOCATION_ONLY; |
7 | | -const PERSISTENT_VALUE = process.env.PERSISTENT_VALUE; |
8 | | -const REMOVABLE_KEY = process.env.REMOVABLE_KEY; |
9 | | -const REMOVABLE_VALUE = process.env.REMOVABLE_VALUE; |
| 6 | +const PERSISTENT_KEY = process.env.PERSISTENT_KEY || 'persistentKey'; |
| 7 | +const PERSISTENT_VALUE = process.env.PERSISTENT_VALUE || 'persistentValue'; |
| 8 | +const REMOVABLE_KEY = process.env.REMOVABLE_KEY || 'removableKey'; |
| 9 | +const REMOVABLE_VALUE = process.env.REMOVABLE_VALUE || 'remvovableValue'; |
10 | 10 | const ERROR_MSG = process.env.ERROR_MSG || 'error'; |
11 | | -const SINGLE_LOG_ITEM_KEY = process.env.SINGLE_LOG_ITEM_KEY; |
12 | | -const SINGLE_LOG_ITEM_VALUE = process.env.SINGLE_LOG_ITEM_VALUE; |
13 | | -const ARBITRARY_OBJECT_KEY = process.env.ARBITRARY_OBJECT_KEY; |
14 | | -const ARBITRARY_OBJECT_DATA = process.env.ARBITRARY_OBJECT_DATA; |
15 | | - |
16 | | -type LambdaEvent = { |
17 | | - invocation: number |
18 | | -}; |
| 11 | +const RUNTIME_ADDED_KEY = process.env.RUNTIME_ADDED_KEY || 'runtimeAddedKey'; |
| 12 | +const SINGLE_LOG_ITEM_KEY = process.env.SINGLE_LOG_ITEM_KEY || 'keyForSingleLogItem'; |
| 13 | +const SINGLE_LOG_ITEM_VALUE = process.env.SINGLE_LOG_ITEM_VALUE || 'valueForSingleLogItem'; |
| 14 | +const ARBITRARY_OBJECT_KEY = process.env.ARBITRARY_OBJECT_KEY || 'keyForArbitraryObject'; |
| 15 | +const ARBITRARY_OBJECT_DATA = process.env.ARBITRARY_OBJECT_DATA || 'arbitrary object data'; |
19 | 16 |
|
20 | 17 | const logger = new Logger({ |
21 | 18 | persistentLogAttributes: { |
22 | | - [PERSISTENT_KEY]: PERSISTENT_VALUE, |
23 | | - [REMOVABLE_KEY]: REMOVABLE_VALUE, |
| 19 | + [PERSISTENT_KEY]: PERSISTENT_VALUE, // This key-value pair will be added to every log |
| 20 | + [REMOVABLE_KEY]: REMOVABLE_VALUE, // This other one will be removed at runtime and not displayed in any log |
24 | 21 | }, |
25 | 22 | }); |
26 | 23 |
|
27 | | -const testFunction = async (event: LambdaEvent, context: Context): Promise<{requestId: string}> => { |
28 | | - // Test feature 1: Log level filtering |
29 | | - // Test feature 2: Context data |
30 | | - // Test feature 3: Add and remove persistent additional log keys and value |
31 | | - // Test feature 4: X-Ray Trace ID injection |
32 | | - logger.removeKeys([REMOVABLE_KEY]); |
33 | | - |
34 | | - const specialValue = event.invocation; |
35 | | - if (specialValue === 0) { |
36 | | - logger.appendKeys({ |
37 | | - [PERSISTENT_KEY_FIRST_INVOCATION_ONLY]: specialValue |
38 | | - }); |
39 | | - } |
40 | | - |
| 24 | +const testFunction = async (event: TestEvent, context: Context): TestOutput => { |
| 25 | + // Test feature 1: Context data injection (all logs should have the same context data) |
| 26 | + // Test feature 2: Event log (this log should have the event data) |
| 27 | + // Test feature 3: Log level filtering (log level is set to INFO) |
41 | 28 | logger.debug('##### This should not appear'); |
42 | | - logger.info('This is an INFO log with context and persistent key'); |
| 29 | + |
| 30 | + // Test feature 4: Add and remove persistent additional log keys and value |
| 31 | + logger.removeKeys([REMOVABLE_KEY]); // This key should not appear in any log (except the event log) |
| 32 | + logger.appendKeys({ // This key-value pair should appear in every log (except the event log) |
| 33 | + [RUNTIME_ADDED_KEY]: event.invocation, |
| 34 | + }); |
43 | 35 |
|
44 | 36 | // Test feature 5: One-time additional log keys and values |
45 | 37 | logger.info('This is an one-time log with an additional key-value', { |
46 | 38 | [SINGLE_LOG_ITEM_KEY]: SINGLE_LOG_ITEM_VALUE, |
47 | 39 | }); |
48 | 40 |
|
49 | | - // Test feature 6: Logging an error object |
| 41 | + // Test feature 6: Error logging |
50 | 42 | try { |
51 | 43 | throw new Error(ERROR_MSG); |
52 | 44 | } catch (e) { |
53 | 45 | logger.error(ERROR_MSG, e as Error); |
54 | 46 | } |
55 | 47 |
|
56 | | - // Test feature 7: Logging an arbitrary object |
| 48 | + // Test feature 7: Arbitrary object logging |
57 | 49 | const obj: APIGatewayAuthorizerResult = { |
58 | 50 | principalId: ARBITRARY_OBJECT_DATA, |
59 | 51 | policyDocument: { |
60 | | - Version: 'Version' + ARBITRARY_OBJECT_DATA, |
| 52 | + Version: 'Version 1', |
61 | 53 | Statement: [{ |
62 | | - Effect: 'Effect' + ARBITRARY_OBJECT_DATA, |
63 | | - Action: 'Action' + ARBITRARY_OBJECT_DATA, |
64 | | - Resource: 'Resource' + ARBITRARY_OBJECT_DATA |
| 54 | + Effect: 'Allow', |
| 55 | + Action: 'geo:*', |
| 56 | + Resource: '*', |
65 | 57 | }] |
66 | 58 | } |
67 | 59 | }; |
68 | | - |
69 | 60 | logger.info('A log entry with an object', { [ARBITRARY_OBJECT_KEY]: obj }); |
70 | 61 |
|
| 62 | + // Test feature 8: X-Ray Trace ID injection (all logs should have the same X-Ray Trace ID) |
| 63 | + |
71 | 64 | return { |
72 | 65 | requestId: context.awsRequestId, |
73 | 66 | }; |
|
0 commit comments