Replies: 1 comment 1 reply
-
Hi @zirkelc, nice to see you here! I am not sure I understand the issue with CloudWatch and import { Logger } from '@aws-lambda-powertools/logger';
interface CustomEvent {
userId?: string;
text?: string;
}
const logger = new Logger({
logLevel: 'DEBUG',
serviceName: 'my-service',
});
export const handler = async (event: CustomEvent) => {
logger.appendKeys({
userId: event.userId || 'anonymous',
});
logger.debug('Handler invoked');
logger.debug('Handler invoked 2');
logger.debug('Handler invoked 3');
return {
statusCode: 200,
body: JSON.stringify('Hello, World!'),
};
}; and calling with payloads like and a specific Regarding the import { Logger, LogFormatter, LogItem } from '@aws-lambda-powertools/logger';
import type {
LogAttributes,
UnformattedAttributes,
} from '@aws-lambda-powertools/logger/types';
class PrefixFormatter extends LogFormatter {
readonly #prefix: string;
public constructor(prefix: string) {
super();
this.#prefix = prefix;
}
public formatAttributes(
attributes: UnformattedAttributes,
additionalLogAttributes: LogAttributes
): LogItem {
return new LogItem({
attributes: {
...attributes,
message: `${this.#prefix}${attributes.message}`,
},
}).addAttributes(additionalLogAttributes);
}
}
const logger = new Logger({
logLevel: 'INFO',
});
export const handler = async () => {
const prefixLogger = logger.createChild({
logFormatter: new PrefixFormatter('foo '),
});
prefixLogger.info('Hello world');
}; Overall with this being an opinionated JSON-structured logger having a prefix to filter/query messages sounds like an anti-pattern - unless I am completely misunderstanding the request. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
it's me again 👋 😄
I would like to suggest an option to configure an optional
prefix
which is automatically being added to all logged messages. I have a Lambda that processes items from a database in sequence and logs various information to the console. At the moment I have a local prefix field that I insert into every log statement.Pseudocode:
Why not use
logger.appendKeys()
?The AWS management console CloudWatch search when used with a keywords only returns the rows having this keyword (e.g. the user id), but there is no option to show the rows before or after the match. It misses something like
grep -A -B -C
. So I often resort to show all logs and use the browser search on CMD+F, but this only work on visible text and not the hidden JSON that is only available on expanding a row.Suggestion
The new
prefix
option should be available on the constructor and onlogger.createChild()
.Beta Was this translation helpful? Give feedback.
All reactions