Skip to content

Commit 48f04cd

Browse files
committed
fix(logger): environment attributes, polishing
1 parent c359e54 commit 48f04cd

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

packages/logger/examples/custom-logger-options.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,35 @@ const lambdaHandler: Handler = async (event, context) => {
4242
};
4343

4444
lambdaHandler(dummyEvent, dummyContext, () => {});
45+
46+
/**
47+
* Sample output:
48+
*
49+
* {
50+
* message: 'This is an ERROR log',
51+
* service: 'foo-bar',
52+
* environment: 'prod',
53+
* awsRegion: 'eu-central-1',
54+
* correlationIds: {
55+
* awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
56+
* xRayTraceId: 'abcdef123456abcdef123456abcdef123456',
57+
* myCustomCorrelationId: 'foo-bar-baz'
58+
* },
59+
* lambdaFunction: {
60+
* name: 'foo-bar-function',
61+
* arn: 'arn:aws:lambda:eu-central-1:123456789012:function:Example',
62+
* memoryLimitInMB: 128,
63+
* version: '$LATEST',
64+
* coldStart: undefined
65+
* },
66+
* logLevel: 'ERROR',
67+
* timestamp: '2021-03-10T18:47:04.724Z',
68+
* logger: {
69+
* name: '@aws-lambda-powertools/logger',
70+
* version: '0.0.0',
71+
* level: 'ERROR',
72+
* sampleRateValue: 0.00001
73+
* }
74+
* }
75+
*
76+
**/

packages/logger/examples/formatters/CustomLogFormatter.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable sort-keys */
1+
import * as powertool from '../../package.json';
22
import { LogFormatter } from '../../src/formatter';
33
import { LogAttributes, UnformattedAttributes } from '../../types';
44

@@ -9,15 +9,9 @@ class CustomLogFormatter extends LogFormatter {
99
public format(attributes: UnformattedAttributes): MyCompanyLog {
1010
return {
1111
message: attributes.message,
12-
timestamp: this.formatTimestamp(attributes.timestamp),
13-
logLevel: attributes.logLevel,
1412
service: attributes.serviceName,
15-
env: attributes.environment,
13+
environment: attributes.environment,
1614
awsRegion: attributes.awsRegion,
17-
logger: {
18-
level: attributes.logLevel,
19-
sampleRateValue: attributes.sampleRateValue,
20-
},
2115
correlationIds: {
2216
awsRequestId: attributes.awsRequestId,
2317
xRayTraceId: attributes.xRayTraceId
@@ -29,6 +23,14 @@ class CustomLogFormatter extends LogFormatter {
2923
version: attributes.functionVersion,
3024
coldStart: attributes.coldStart,
3125
},
26+
logLevel: attributes.logLevel,
27+
timestamp: this.formatTimestamp(attributes.timestamp),
28+
logger: {
29+
name: powertool.name,
30+
version: powertool.version,
31+
level: attributes.logLevel,
32+
sampleRateValue: attributes.sampleRateValue,
33+
},
3234
};
3335
}
3436

packages/logger/src/Logger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
LogAttributes,
1111
LoggerOptions,
1212
LogLevel,
13-
LogLevelThresholds,
13+
LogLevelThresholds, UnformattedAttributes,
1414
} from '../types';
1515
import { LogFormatterInterface, PowertoolLogFormatter } from './formatter';
1616

@@ -107,7 +107,7 @@ class Logger implements LoggerInterface {
107107
private createLogItem(logLevel: LogLevel, message: string, customAttributes: LogAttributes = {}): LogItem {
108108
this.addToDefaultLoggerAttributes({ logLevel, message, timestamp: new Date() });
109109

110-
return new LogItem().addAttributes(this.getLogFormatter().format(this.getDefaultLoggerAttributes()))
110+
return new LogItem().addAttributes(this.getLogFormatter().format(this.getDefaultLoggerAttributes() as UnformattedAttributes))
111111
.addAttributes(customAttributes);
112112
}
113113

@@ -150,7 +150,7 @@ class Logger implements LoggerInterface {
150150
private populateDefaultLoggerAttributes(serviceName?: string, environment?: Environment, customAttributes: LogAttributes = {}): void {
151151
this.addToDefaultLoggerAttributes({
152152
awsRegion: this.getEnvVarsService().getAwsRegion(),
153-
env: environment || this.getCustomConfigService()?.getCurrentEnvironment() || this.getEnvVarsService().getCurrentEnvironment(),
153+
environment: environment || this.getCustomConfigService()?.getCurrentEnvironment() || this.getEnvVarsService().getCurrentEnvironment(),
154154
functionName: this.getEnvVarsService().getFunctionName(),
155155
functionVersion: this.getEnvVarsService().getFunctionVersion(),
156156
logLevel: this.getLogLevel(),

packages/logger/types/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type DefaultLoggerAttributes = LogAttributes | {[key in keyof Context]: Context[
3030
message?: string
3131
};
3232

33-
type UnformattedAttributes = DefaultLoggerAttributes & {
33+
type UnformattedAttributes = LogAttributes & DefaultLoggerAttributes & {
3434
awsRequestId?: string
3535
environment?: Environment
3636
serviceName: string

0 commit comments

Comments
 (0)