aboutsummaryrefslogtreecommitdiff
path: root/logger.ts
diff options
context:
space:
mode:
Diffstat (limited to 'logger.ts')
-rw-r--r--logger.ts22
1 files changed, 22 insertions, 0 deletions
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}`)
+};
+