Skip to content

Commit d5b1c51

Browse files
author
Dmitry Balabanov
committed
fix(logger): fix handling of additional log keys
1 parent 4793360 commit d5b1c51

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

packages/logger/examples/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ const lambdaHandler: Handler = async () => {
1717
try {
1818
throw new Error('Unexpected error #1');
1919
} catch (error) {
20-
logger.error('This is an ERROR log #1', error);
20+
logger.error('This is an ERROR log #1', error as Error);
2121
}
2222

2323
try {
2424
throw new Error('Unexpected error #2');
2525
} catch (error) {
26-
logger.error('This is an ERROR log #2', { myCustomErrorKey: error } );
26+
logger.error('This is an ERROR log #2', { myCustomErrorKey: error as Error } );
2727
}
2828

2929
return {

packages/logger/src/Logger.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class Logger extends Utility implements ClassThatLogs {
200200
* It prints a log item with level DEBUG.
201201
*
202202
* @param {LogItemMessage} input
203-
* @param {Error | LogAttributes | unknown} extraInput
203+
* @param {Error | LogAttributes | string} extraInput
204204
* @returns {void}
205205
*/
206206
public debug(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
@@ -211,7 +211,7 @@ class Logger extends Utility implements ClassThatLogs {
211211
* It prints a log item with level ERROR.
212212
*
213213
* @param {LogItemMessage} input
214-
* @param {Error | LogAttributes | unknown} extraInput
214+
* @param {Error | LogAttributes | string} extraInput
215215
* @returns {void}
216216
*/
217217
public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
@@ -231,7 +231,7 @@ class Logger extends Utility implements ClassThatLogs {
231231
* It prints a log item with level INFO.
232232
*
233233
* @param {LogItemMessage} input
234-
* @param {Error | LogAttributes | unknown} extraInput
234+
* @param {Error | LogAttributes | string} extraInput
235235
* @returns {void}
236236
*/
237237
public info(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
@@ -288,7 +288,7 @@ class Logger extends Utility implements ClassThatLogs {
288288
* It prints a log item with level WARN.
289289
*
290290
* @param {LogItemMessage} input
291-
* @param {Error | LogAttributes | unknown} extraInput
291+
* @param {Error | LogAttributes | string} extraInput
292292
* @returns {void}
293293
*/
294294
public warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
@@ -336,9 +336,13 @@ class Logger extends Utility implements ClassThatLogs {
336336
if (typeof input !== 'string') {
337337
logItem.addAttributes(input);
338338
}
339-
extraInput.forEach((item: Error | LogAttributes | unknown) => {
340-
const attributes = item instanceof Error ? { error: item } : item;
341-
logItem.addAttributes(<LogAttributes>attributes);
339+
extraInput.forEach((item: Error | LogAttributes | string) => {
340+
const attributes: LogAttributes =
341+
item instanceof Error ? { error: item } :
342+
typeof item === 'string' ? { extra: item } :
343+
item;
344+
345+
logItem.addAttributes(attributes);
342346
});
343347

344348
return logItem;

packages/logger/src/types/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type UnformattedAttributes = {
5050
};
5151

5252
type LogItemMessage = string | LogAttributesWithMessage;
53-
type LogItemExtraInput = Array<Error | LogAttributes | unknown>;
53+
type LogItemExtraInput = [Error | string] | LogAttributes[];
5454

5555
type HandlerMethodDecorator = (
5656
target: LambdaInterface,

packages/logger/tests/e2e/basicFeatures.middy.test.FunctionCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const testFunction = async (event: APIGatewayProxyEvent, context: Context): Prom
3030
try {
3131
throw new Error(ERROR_MSG);
3232
} catch (e) {
33-
logger.error(ERROR_MSG, e);
33+
logger.error(ERROR_MSG, e as Error);
3434
}
3535

3636
return {

0 commit comments

Comments
 (0)