aboutsummaryrefslogtreecommitdiff
path: root/logger.ts
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-06-25 22:19:15 +0300
committerGitHub <noreply@github.com>2020-06-25 22:19:15 +0300
commit2156cb4cb0d84d7f3905d97ef789c3f9fde22548 (patch)
treeb11c527bbc0a268f0361a4d56f6aff7011960f98 /logger.ts
parent343a975413d8d3ce8194507017fb4ca01a4faf54 (diff)
parentfd484a217c77dba42c29fae1cfdb2390422da847 (diff)
downloadwhich-api-2156cb4cb0d84d7f3905d97ef789c3f9fde22548.tar.gz
Merge pull request #12 from which-ecosystem/logging
Logging and exception handling
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}`)
+};
+