Skip to content

Commit 277e93d

Browse files
erikayao93dreamorosi
authored andcommitted
feat(logger): Support for external observability providers (#1511)
* Updated formatAttributes for additional parameters and LogItem return type * Updated the unit tests to pass with new formatter * Updated Powertool named objects to Powertools * Updated tests to match new naming consistency * Updated for tests for new naming consistency * Updated formatter for new design decisions * Update Logger for ephemeral attributes * Update bringYourOwnFormatter documentation to match new formatter --------- Co-authored-by: erikayao93 <[email protected]>
1 parent b9ce4ab commit 277e93d

File tree

6 files changed

+581
-2
lines changed

6 files changed

+581
-2
lines changed

Diff for: packages/logger/src/Logger.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ class Logger extends Utility implements LoggerInterface {
640640
item instanceof Error
641641
? { error: item }
642642
: typeof item === 'string'
643-
? { extra: item }
644-
: item;
643+
? { extra: item }
644+
: item;
645645

646646
additionalLogAttributes = merge(additionalLogAttributes, attributes);
647647
});
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { LogAttributes, UnformattedAttributes } from '../types';
2+
import { LogItem } from '../log';
3+
4+
/**
5+
* @interface
6+
*/
7+
interface LogFormatterInterface {
8+
/**
9+
* It formats key-value pairs of log attributes.
10+
*
11+
* @param {UnformattedAttributes} attributes
12+
* @param {LogAttributes} additionalLogAttributes
13+
* @returns {LogItem}
14+
*/
15+
formatAttributes(
16+
attributes: UnformattedAttributes,
17+
additionalLogAttributes: LogAttributes
18+
): LogItem;
19+
20+
/**
21+
* It formats a given Error parameter.
22+
*
23+
* @param {Error} error
24+
* @returns {LogAttributes}
25+
*/
26+
formatError(error: Error): LogAttributes;
27+
}
28+
29+
export { LogFormatterInterface };

Diff for: packages/logger/src/formatter/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './LogFormatter';
2+
export * from './LogFormatterInterface';
3+
export * from './PowertoolsLogFormatter';

Diff for: packages/logger/src/types/formats/PowertoolsLog.ts

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import type { LogAttributes, LogLevel } from '..';
2+
3+
type PowertoolsLog = LogAttributes & {
4+
/**
5+
* timestamp
6+
*
7+
* Description: Timestamp of actual log statement.
8+
* Example: "2020-05-24 18:17:33,774"
9+
*/
10+
timestamp?: string;
11+
12+
/**
13+
* level
14+
*
15+
* Description: Logging level
16+
* Example: "INFO"
17+
*/
18+
level?: LogLevel;
19+
20+
/**
21+
* service
22+
*
23+
* Description: Service name defined.
24+
* Example: "payment"
25+
*/
26+
service: string;
27+
28+
/**
29+
* sampling_rate
30+
*
31+
* Description: The value of the logging sampling rate in percentage.
32+
* Example: 0.1
33+
*/
34+
sampling_rate?: number;
35+
36+
/**
37+
* message
38+
*
39+
* Description: Log statement value. Unserializable JSON values will be cast to string.
40+
* Example: "Collecting payment"
41+
*/
42+
message?: string;
43+
44+
/**
45+
* xray_trace_id
46+
*
47+
* Description: X-Ray Trace ID when Lambda function has enabled Tracing.
48+
* Example: "1-5759e988-bd862e3fe1be46a994272793"
49+
*/
50+
xray_trace_id?: string;
51+
52+
/**
53+
* cold_start
54+
*
55+
* Description: Indicates whether the current execution experienced a cold start.
56+
* Example: false
57+
*/
58+
cold_start?: boolean;
59+
60+
/**
61+
* lambda_function_name
62+
*
63+
* Description: The name of the Lambda function.
64+
* Example: "example-powertools-HelloWorldFunction-1P1Z6B39FLU73"
65+
*/
66+
lambda_function_name?: string;
67+
68+
/**
69+
* lambda_function_memory_size
70+
*
71+
* Description: The memory size of the Lambda function.
72+
* Example: 128
73+
*/
74+
lambda_function_memory_size?: number;
75+
76+
/**
77+
* lambda_function_arn
78+
*
79+
* Description: The ARN of the Lambda function.
80+
* Example: "arn:aws:lambda:eu-west-1:012345678910:function:example-powertools-HelloWorldFunction-1P1Z6B39FLU73"
81+
*/
82+
lambda_function_arn?: string;
83+
84+
/**
85+
* lambda_request_id
86+
*
87+
* Description: The request ID of the current invocation.
88+
* Example: "899856cb-83d1-40d7-8611-9e78f15f32f4"
89+
*/
90+
lambda_request_id?: string;
91+
};
92+
93+
export type { PowertoolsLog };

Diff for: packages/logger/src/types/formats/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './PowertoolsLog';

0 commit comments

Comments
 (0)