Skip to content

Commit 7e6b4a9

Browse files
docs(logger): extract Logger code snippets in separate files (#1230)
1 parent 4307a7c commit 7e6b4a9

18 files changed

+375
-374
lines changed

docs/core/logger.md

Lines changed: 17 additions & 374 deletions
Large diffs are not rendered by default.

docs/snippets/logger/appendKeys.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
3+
// Add persistent log keys via the constructor
4+
const logger = new Logger({
5+
persistentLogAttributes: {
6+
aws_account_id: '123456789012',
7+
aws_region: 'eu-west-1',
8+
logger: {
9+
name: '@aws-lambda-powertools/logger',
10+
version: '0.0.1',
11+
},
12+
extra_key: "some-value"
13+
}
14+
});
15+
16+
// OR add persistent log keys to an existing Logger instance with the appendKeys method:
17+
// logger.appendKeys({
18+
// aws_account_id: '123456789012',
19+
// aws_region: 'eu-west-1',
20+
// logger: {
21+
// name: '@aws-lambda-powertools/logger',
22+
// version: '0.0.1',
23+
// },
24+
// extra_key: "some-value"
25+
// });
26+
27+
export const handler = async (_event: any, _context: any): Promise<unknown> => {
28+
29+
// If you don't want to log the "extra_key" attribute in your logs, you can remove it
30+
logger.removeKeys(["extra_key"])
31+
32+
// This info log will print all extra custom attributes added above
33+
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
34+
logger.info('This is an INFO log');
35+
logger.info('This is another INFO log');
36+
37+
return {
38+
foo: 'bar'
39+
};
40+
41+
};

docs/snippets/logger/basicUsage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
3+
const logger = new Logger({ serviceName: 'serverlessAirline' });
4+
5+
export const handler = async (_event, _context): Promise<void> => {
6+
// ...
7+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { MyCompanyLogFormatter } from './utils/formatters/MyCompanyLogFormatter';
3+
4+
const logger = new Logger({
5+
logFormatter: new MyCompanyLogFormatter(),
6+
logLevel: 'DEBUG',
7+
serviceName: 'serverlessAirline',
8+
sampleRateValue: 0.5,
9+
persistentLogAttributes: {
10+
awsAccountId: process.env.AWS_ACCOUNT_ID,
11+
logger: {
12+
name: '@aws-lambda-powertools/logger',
13+
version: '0.0.1'
14+
}
15+
},
16+
});
17+
18+
export const handler = async (event, context): Promise<void> => {
19+
20+
logger.addContext(context);
21+
22+
logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
23+
24+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { MyCompanyLogFormatter } from './utils/formatters/MyCompanyLogFormatter';
3+
4+
const logger = new Logger({
5+
logFormatter: new MyCompanyLogFormatter(),
6+
logLevel: 'DEBUG',
7+
serviceName: 'serverlessAirline',
8+
sampleRateValue: 0.5,
9+
persistentLogAttributes: {
10+
awsAccountId: process.env.AWS_ACCOUNT_ID,
11+
logger: {
12+
name: '@aws-lambda-powertools/logger',
13+
version: '0.0.1'
14+
}
15+
},
16+
});
17+
18+
export const handler = async (event, context): Promise<void> => {
19+
20+
logger.addContext(context);
21+
22+
logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
23+
24+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { LambdaInterface } from '@aws-lambda-powertools/commons';
3+
4+
// Persistent attributes added outside the handler will be
5+
// cached across invocations
6+
const logger = new Logger({
7+
logLevel: 'DEBUG',
8+
persistentLogAttributes: {
9+
foo: "bar",
10+
biz: "baz"
11+
}
12+
});
13+
14+
class Lambda implements LambdaInterface {
15+
// Enable the clear state flag
16+
@logger.injectLambdaContext({ clearState: true })
17+
public async handler(_event: any, _context: any): Promise<void> {
18+
// Persistent attributes added inside the handler will NOT be cached
19+
// across invocations
20+
if (event['special_key'] === '123456'){
21+
logger.appendKeys({
22+
details: { special_key: '123456' }
23+
});
24+
}
25+
logger.debug('This is a DEBUG log');
26+
}
27+
28+
}
29+
30+
const myFunction = new Lambda();
31+
export const handler = myFunction.handler.bind(myFunction); // (1)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger';
2+
import middy from '@middy/core';
3+
4+
// Persistent attributes added outside the handler will be
5+
// cached across invocations
6+
const logger = new Logger({
7+
logLevel: 'DEBUG',
8+
persistentLogAttributes: {
9+
foo: "bar",
10+
biz: "baz"
11+
}
12+
});
13+
14+
const lambdaHandler = async (event: { special_key: string }, _context: any): Promise<void> => {
15+
// Persistent attributes added inside the handler will NOT be cached
16+
// across invocations
17+
if (event['special_key'] === '123456') {
18+
logger.appendKeys({
19+
details: { special_key: event['special_key'] }
20+
});
21+
}
22+
logger.debug('This is a DEBUG log');
23+
};
24+
25+
// Enable the clear state flag
26+
export const handler = middy(lambdaHandler)
27+
.use(injectLambdaContext(logger, { clearState: true }));

docs/snippets/logger/createChild.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
3+
// With this logger, all the INFO logs will be printed
4+
const logger = new Logger({
5+
logLevel: 'INFO'
6+
});
7+
8+
// With this logger, only the ERROR logs will be printed
9+
const childLogger = logger.createChild({
10+
logLevel: 'ERROR'
11+
});
12+
13+
export const handler = async (_event: any, _context: any): Promise<void> => {
14+
15+
logger.info('This is an INFO log, from the parent logger');
16+
logger.error('This is an ERROR log, from the parent logger');
17+
18+
childLogger.info('This is an INFO log, from the child logger');
19+
childLogger.error('This is an ERROR log, from the child logger');
20+
21+
};

docs/snippets/logger/decorator.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { LambdaInterface } from '@aws-lambda-powertools/commons';
3+
4+
const logger = new Logger();
5+
6+
class Lambda implements LambdaInterface {
7+
// Decorate your handler class method
8+
@logger.injectLambdaContext()
9+
public async handler(_event: any, _context: any): Promise<void> {
10+
logger.info('This is an INFO log with some context');
11+
}
12+
13+
}
14+
15+
const myFunction = new Lambda();
16+
export const handler = myFunction.handler.bind(myFunction); // (1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { LambdaInterface } from '@aws-lambda-powertools/commons';
3+
4+
const logger = new Logger();
5+
6+
class Lambda implements LambdaInterface {
7+
// Set the log event flag to true
8+
@logger.injectLambdaContext({ logEvent: true })
9+
public async handler(_event: any, _context: any): Promise<void> {
10+
logger.info('This is an INFO log with some context');
11+
}
12+
13+
}
14+
15+
const myFunction = new Lambda();
16+
export const handler = myFunction.handler.bind(myFunction); // (1)

0 commit comments

Comments
 (0)