From fd484a217c77dba42c29fae1cfdb2390422da847 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 21:25:18 +0300 Subject: feat: add logger and handle eveyrthing nicely --- logger.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 logger.ts (limited to 'logger.ts') diff --git a/logger.ts b/logger.ts new file mode 100644 index 0000000..f11b0c8 --- /dev/null +++ b/logger.ts @@ -0,0 +1,22 @@ +type Stream = 'log' | 'error' | 'debug'; + +interface ErrorWithCode extends Error { + code?: number +} + +const dateOpts = { timeStyle: 'medium', dateStyle: 'short' }; +// eslint-disable-next-line +// @ts-ignore +const timestamp = (): string => new Date().toLocaleString('en', dateOpts); + + +const logWithTimestamp = (stream: Stream, message: string): void => { + console[stream](`[${timestamp()}] ${message}`); +}; + +export default { + log: (message: string): void => logWithTimestamp('log', message), + debug: (message: string): void => logWithTimestamp('debug', message), + error: (err: ErrorWithCode): void => logWithTimestamp('error', `${err.code || ''} ${err.name}: ${err.message}`) +}; + -- cgit v1.2.3