Skip to content

Commit

Permalink
refactor(docs): apply eslint rules to code snippets (#1259)
Browse files Browse the repository at this point in the history
* fix(docs): apply eslint rules to code snippets

* fix(docs): remove overrides, introduce changes to code-snippets

* fix(docs): code-snippet captureAWSAll (#1252)
  • Loading branch information
niko-achilles authored Feb 7, 2023
1 parent de02b3d commit aa14407
Show file tree
Hide file tree
Showing 56 changed files with 357 additions and 360 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
root: true,
env: {
browser: false,
es2020: true,
Expand Down Expand Up @@ -63,5 +64,5 @@ module.exports = {
'prefer-arrow-callback': 'error',
quotes: [ 'error', 'single', { allowTemplateLiterals: true } ],
semi: [ 'error', 'always' ]
},
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ jobs:
if: steps.cache-node-modules.outputs.cache-hit == 'true'
run: |
npm run build -w packages/commons
npm run build -w packages/logger & npm run build -w packages/tracer & npm run build -w packages/metrics & npm run build -w packages/parameters & npm run build -w packages/idempotency
npm run build -w packages/logger & npm run build -w packages/tracer & npm run build -w packages/metrics & npm run build -w packages/parameters & npm run build -w packages/idempotency & npm run build -w docs/snippets
- name: Run linting
run: npm run lint -w packages/commons -w packages/logger -w packages/tracer -w packages/metrics -w packages/parameters -w packages/idempotency
run: npm run lint -w packages/commons -w packages/logger -w packages/tracer -w packages/metrics -w packages/parameters -w packages/idempotency -w docs/snippets
- name: Run unit tests
run: npm t -w packages/commons -w packages/logger -w packages/tracer -w packages/metrics -w packages/parameters -w packages/idempotency
check-examples:
Expand Down
38 changes: 19 additions & 19 deletions docs/snippets/logger/appendKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Logger } from '@aws-lambda-powertools/logger';

// Add persistent log keys via the constructor
const logger = new Logger({
persistentLogAttributes: {
aws_account_id: '123456789012',
aws_region: 'eu-west-1',
logger: {
name: '@aws-lambda-powertools/logger',
version: '0.0.1',
},
extra_key: "some-value"
}
persistentLogAttributes: {
aws_account_id: '123456789012',
aws_region: 'eu-west-1',
logger: {
name: '@aws-lambda-powertools/logger',
version: '0.0.1',
},
extra_key: 'some-value'
}
});

// OR add persistent log keys to an existing Logger instance with the appendKeys method:
Expand All @@ -24,18 +24,18 @@ const logger = new Logger({
// extra_key: "some-value"
// });

export const handler = async (_event: any, _context: any): Promise<unknown> => {
export const handler = async (_event: unknown, _context: unknown): Promise<unknown> => {

// If you don't want to log the "extra_key" attribute in your logs, you can remove it
logger.removeKeys(["extra_key"])
// If you don't want to log the "extra_key" attribute in your logs, you can remove it
logger.removeKeys(['extra_key']);

// This info log will print all extra custom attributes added above
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
logger.info('This is an INFO log');
logger.info('This is another INFO log');
// This info log will print all extra custom attributes added above
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
logger.info('This is an INFO log');
logger.info('This is another INFO log');

return {
foo: 'bar'
};
return {
foo: 'bar'
};

};
2 changes: 1 addition & 1 deletion docs/snippets/logger/basicUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Logger } from '@aws-lambda-powertools/logger';
const logger = new Logger({ serviceName: 'serverlessAirline' });

export const handler = async (_event, _context): Promise<void> => {
// ...
logger.info('Hello World');
};
4 changes: 2 additions & 2 deletions docs/snippets/logger/bringYourOwnFormatterClass.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LogFormatter } from "@aws-lambda-powertools/logger";
import { LogFormatter } from '@aws-lambda-powertools/logger';
import {
LogAttributes,
UnformattedAttributes,
} from "@aws-lambda-powertools/logger/lib/types";
} from '@aws-lambda-powertools/logger/lib/types';

// Replace this line with your own type
type MyCompanyLog = LogAttributes;
Expand Down
26 changes: 13 additions & 13 deletions docs/snippets/logger/bringYourOwnFormatterHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { Logger } from '@aws-lambda-powertools/logger';
import { MyCompanyLogFormatter } from './utils/formatters/MyCompanyLogFormatter';

const logger = new Logger({
logFormatter: new MyCompanyLogFormatter(),
logLevel: 'DEBUG',
serviceName: 'serverlessAirline',
sampleRateValue: 0.5,
persistentLogAttributes: {
awsAccountId: process.env.AWS_ACCOUNT_ID,
logger: {
name: '@aws-lambda-powertools/logger',
version: '0.0.1'
}
},
logFormatter: new MyCompanyLogFormatter(),
logLevel: 'DEBUG',
serviceName: 'serverlessAirline',
sampleRateValue: 0.5,
persistentLogAttributes: {
awsAccountId: process.env.AWS_ACCOUNT_ID,
logger: {
name: '@aws-lambda-powertools/logger',
version: '0.0.1'
}
},
});

export const handler = async (event, context): Promise<void> => {

logger.addContext(context);
logger.addContext(context);

logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });

};
32 changes: 16 additions & 16 deletions docs/snippets/logger/clearStateDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import { LambdaInterface } from '@aws-lambda-powertools/commons';
// Persistent attributes added outside the handler will be
// cached across invocations
const logger = new Logger({
logLevel: 'DEBUG',
persistentLogAttributes: {
foo: "bar",
biz: "baz"
}
logLevel: 'DEBUG',
persistentLogAttributes: {
foo: 'bar',
biz: 'baz'
}
});

class Lambda implements LambdaInterface {
// Enable the clear state flag
@logger.injectLambdaContext({ clearState: true })
public async handler(_event: any, _context: any): Promise<void> {
// Persistent attributes added inside the handler will NOT be cached
// across invocations
if (event['special_key'] === '123456'){
logger.appendKeys({
details: { special_key: '123456' }
});
}
logger.debug('This is a DEBUG log');
// Enable the clear state flag
@logger.injectLambdaContext({ clearState: true })
public async handler(event: unknown, _context: unknown): Promise<void> {
// Persistent attributes added inside the handler will NOT be cached
// across invocations
if (event['special_key'] === '123456'){
logger.appendKeys({
details: { special_key: '123456' }
});
}
logger.debug('This is a DEBUG log');
}

}

Expand Down
30 changes: 15 additions & 15 deletions docs/snippets/logger/clearStateMiddy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import middy from '@middy/core';
// Persistent attributes added outside the handler will be
// cached across invocations
const logger = new Logger({
logLevel: 'DEBUG',
persistentLogAttributes: {
foo: "bar",
biz: "baz"
}
logLevel: 'DEBUG',
persistentLogAttributes: {
foo: 'bar',
biz: 'baz'
}
});

const lambdaHandler = async (event: { special_key: string }, _context: any): Promise<void> => {
// Persistent attributes added inside the handler will NOT be cached
// across invocations
if (event['special_key'] === '123456') {
logger.appendKeys({
details: { special_key: event['special_key'] }
});
}
logger.debug('This is a DEBUG log');
const lambdaHandler = async (event: { special_key: string }, _context: unknown): Promise<void> => {
// Persistent attributes added inside the handler will NOT be cached
// across invocations
if (event['special_key'] === '123456') {
logger.appendKeys({
details: { special_key: event['special_key'] }
});
}
logger.debug('This is a DEBUG log');
};

// Enable the clear state flag
export const handler = middy(lambdaHandler)
.use(injectLambdaContext(logger, { clearState: true }));
.use(injectLambdaContext(logger, { clearState: true }));
14 changes: 7 additions & 7 deletions docs/snippets/logger/createChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { Logger } from '@aws-lambda-powertools/logger';

// With this logger, all the INFO logs will be printed
const logger = new Logger({
logLevel: 'INFO'
logLevel: 'INFO'
});

// With this logger, only the ERROR logs will be printed
const childLogger = logger.createChild({
logLevel: 'ERROR'
logLevel: 'ERROR'
});

export const handler = async (_event: any, _context: any): Promise<void> => {
export const handler = async (_event: unknown, _context: unknown): Promise<void> => {

logger.info('This is an INFO log, from the parent logger');
logger.error('This is an ERROR log, from the parent logger');
logger.info('This is an INFO log, from the parent logger');
logger.error('This is an ERROR log, from the parent logger');

childLogger.info('This is an INFO log, from the child logger');
childLogger.error('This is an ERROR log, from the child logger');
childLogger.info('This is an INFO log, from the child logger');
childLogger.error('This is an ERROR log, from the child logger');

};
10 changes: 5 additions & 5 deletions docs/snippets/logger/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { LambdaInterface } from '@aws-lambda-powertools/commons';
const logger = new Logger();

class Lambda implements LambdaInterface {
// Decorate your handler class method
@logger.injectLambdaContext()
public async handler(_event: any, _context: any): Promise<void> {
logger.info('This is an INFO log with some context');
}
// Decorate your handler class method
@logger.injectLambdaContext()
public async handler(_event: unknown, _context: unknown): Promise<void> {
logger.info('This is an INFO log with some context');
}

}

Expand Down
10 changes: 5 additions & 5 deletions docs/snippets/logger/eventDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { LambdaInterface } from '@aws-lambda-powertools/commons';
const logger = new Logger();

class Lambda implements LambdaInterface {
// Set the log event flag to true
@logger.injectLambdaContext({ logEvent: true })
public async handler(_event: any, _context: any): Promise<void> {
logger.info('This is an INFO log with some context');
}
// Set the log event flag to true
@logger.injectLambdaContext({ logEvent: true })
public async handler(_event: unknown, _context: unknown): Promise<void> {
logger.info('This is an INFO log with some context');
}

}

Expand Down
6 changes: 3 additions & 3 deletions docs/snippets/logger/eventMiddy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import middy from '@middy/core';

const logger = new Logger();

const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
logger.info('This is an INFO log with some context');
const lambdaHandler = async (_event: unknown, _context: unknown): Promise<void> => {
logger.info('This is an INFO log with some context');
};

export const handler = middy(lambdaHandler)
.use(injectLambdaContext(logger, { logEvent: true }));
.use(injectLambdaContext(logger, { logEvent: true }));
48 changes: 24 additions & 24 deletions docs/snippets/logger/extraData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

export const handler = async (event: any, _context: any): Promise<unknown> => {
export const handler = async (event: unknown, _context: unknown): Promise<unknown> => {

const myImportantVariable = {
foo: 'bar'
};
const myImportantVariable = {
foo: 'bar'
};

// Log additional data in single log items
// Log additional data in single log items

// As second parameter
logger.info('This is a log with an extra variable', { data: myImportantVariable });
// As second parameter
logger.info('This is a log with an extra variable', { data: myImportantVariable });

// You can also pass multiple parameters containing arbitrary objects
logger.info('This is a log with 3 extra objects',
{ data: myImportantVariable },
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } },
{ lambdaEvent: event }
);
// You can also pass multiple parameters containing arbitrary objects
logger.info('This is a log with 3 extra objects',
{ data: myImportantVariable },
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } },
{ lambdaEvent: event }
);

// Simply pass a string for logging additional data
logger.info('This is a log with additional string value', 'string value');
// Simply pass a string for logging additional data
logger.info('This is a log with additional string value', 'string value');

// Directly passing an object containing both the message and the additional info
const logObject = {
message: 'This is a log message',
additionalValue: 42
};
// Directly passing an object containing both the message and the additional info
const logObject = {
message: 'This is a log message',
additionalValue: 42
};

logger.info(logObject);
logger.info(logObject);

return {
foo: 'bar'
};
return {
foo: 'bar'
};

};
26 changes: 13 additions & 13 deletions docs/snippets/logger/logError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

export const handler = async (_event: any, _context: any): Promise<void> => {
export const handler = async (_event: unknown, _context: unknown): Promise<void> => {

try {
throw new Error('Unexpected error #1');
} catch (error) {
// Log information about the error using the default "error" key
logger.error('This is the first error', error as Error);
}
try {
throw new Error('Unexpected error #1');
} catch (error) {
// Log information about the error using the default "error" key
logger.error('This is the first error', error as Error);
}

try {
throw new Error('Unexpected error #2');
} catch (error) {
// Log information about the error using a custom "myCustomErrorKey" key
logger.error('This is the second error', { myCustomErrorKey: error as Error } );
}
try {
throw new Error('Unexpected error #2');
} catch (error) {
// Log information about the error using a custom "myCustomErrorKey" key
logger.error('This is the second error', { myCustomErrorKey: error as Error } );
}

};
Loading

0 comments on commit aa14407

Please sign in to comment.