-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Change powertools lambda context capture log key order #3091
Comments
Hi @WtfJoke, thanks for opening the issue. As you pointed out in the "Alternative Solutions" section of the issue, in the last release we added the ability to specify the order of the keys when initializing the Logger. I think however that we could have done a better job in the documentation for the feature. After re-reading it, if I understand correctly your feature request, I think it's not accurate. The docs say this:
Technically speaking, the context keys are included in the In practice, you can specify any key that Powertools manages - which includes the standard keys and the context keys - but also any other key that your code adds. For example, if you want import { Logger } from '@aws-lambda-powertools/logger';
import type { Handler } from 'aws-lambda';
const logger = new Logger({
logRecordOrder: ['message', 'timestamp', 'level'],
});
export const handler: Handler = async (_, context) => {
logger.addContext(context);
logger.info('Hello, World!');
logger.info('Hello, World!');
logger.info('Hello, World!');
logger.info('Hello, World!');
return {
statusCode: 200,
body: JSON.stringify('Hello, World!'),
};
}; Which would result in these logs on CloudWatch: The types should also help you with choosing the keys in the IDE: If instead you want full control of how the log is structured and ordered, you can also bring your own custom log formatter and pass it to the Logger when initializing it, below an example of this that I have used in another application: import type { Handler } from 'aws-lambda';
import { Logger, LogFormatter, LogItem } from '@aws-lambda-powertools/logger';
import type {
LogAttributes,
UnformattedAttributes,
} from '@aws-lambda-powertools/logger/types';
/**
* Custom log formatter to reorganize the log attributes.
*/
class CustomLogFormatter extends LogFormatter {
public formatAttributes(
attributes: UnformattedAttributes,
additionalLogAttributes: LogAttributes
): LogItem {
const {
feature,
area,
appVersion,
eventId,
eventType,
installationId,
...otherAttributes
} = additionalLogAttributes;
return new LogItem({
attributes: {
message: attributes.message,
timestamp: this.formatTimestamp(attributes.timestamp),
level: attributes.logLevel,
logMetadata: {
feature,
area,
},
service: {
environment: attributes.environment,
name: attributes.serviceName,
version: appVersion,
},
correlationIds: {
awsRequestId: attributes.lambdaContext?.awsRequestId,
xrayTraceId: attributes.xRayTraceId,
eventId,
eventType,
installationId,
},
functionMetadata: {
memorySize: attributes.lambdaContext?.memoryLimitInMB,
name: attributes.lambdaContext?.functionName,
version: attributes.lambdaContext?.functionVersion,
arn: attributes.lambdaContext?.invokedFunctionArn,
},
},
}).addAttributes(otherAttributes);
}
}
const logger = new Logger({
logFormatter: new CustomLogFormatter(),
}); Please let me know if this helps, and also if I understood your feature request correctly. If yes, please leave the issue open so we can clarify the docs. |
Use case
We were in the process of enabling the capturing of the lambda context in our logger.
Although we like the additional context, we didnt enable it because the actual logged message is only visible anymore when expanding the log message which makes it almost impossible to skim through log-message and spot the one which looks interesting because they all look the same in the x-ray-tracing-view and I would be forced to scroll sideways.
See this screenshot for reference:
Solution/User Experience
Is it possible for you to change the order of the logging keys exposed from capturing the lambda context and put it to the end by default (or after the message)?
Alternative solutions
Since v2.8.0 its also possible to define log key order on their own, see here: https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/#reordering-log-keys-position
Acknowledgment
Future readers
Please react with 👍 and your use case to help us understand customer demand.
The text was updated successfully, but these errors were encountered: