aboutsummaryrefslogtreecommitdiff
path: root/logger.ts
blob: f11b0c87c59dc0875a1096fcb6adbdc582dbe693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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}`)
};