From 6bb5fedc695ea0f46839c5de776eaed94468a1ac Mon Sep 17 00:00:00 2001 From: Mark Tiedemann Date: Thu, 19 Nov 2020 12:58:53 +0100 Subject: [PATCH] feat(std/log): Log error stack (#8401) --- std/log/logger.ts | 2 ++ std/log/logger_test.ts | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/std/log/logger.ts b/std/log/logger.ts index c30517c59d..7ef23977a3 100644 --- a/std/log/logger.ts +++ b/std/log/logger.ts @@ -131,6 +131,8 @@ export class Logger { typeof data === "symbol" ) { return String(data); + } else if (data instanceof Error) { + return data.stack!; } else if (typeof data === "object") { return JSON.stringify(data); } diff --git a/std/log/logger_test.ts b/std/log/logger_test.ts index 4ab50809f8..b01d1dc5d5 100644 --- a/std/log/logger_test.ts +++ b/std/log/logger_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "../testing/asserts.ts"; +import { assert, assertEquals, assertMatch } from "../testing/asserts.ts"; import { Logger, LogRecord } from "./logger.ts"; import { LevelName, LogLevels } from "./levels.ts"; import { BaseHandler } from "./handlers.ts"; @@ -243,5 +243,13 @@ Deno.test( }); assertEquals(handler.messages[16], 'ERROR {"payload":"data","other":123}'); assertEquals(handler.messages[17], 'ERROR {"payload":"data","other":123}'); + + // error + const error = new RangeError("Uh-oh!"); + const data19: RangeError = logger.error(error); + assertEquals(data19, error); + const messages19 = handler.messages[18].split("\n"); + assertEquals(messages19[0], `ERROR ${error.name}: ${error.message}`); + assertMatch(messages19[1], /^\s+at file:.*\d+:\d+$/); }, );