diff options
Diffstat (limited to 'logger.ts')
-rw-r--r-- | logger.ts | 22 |
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}`) +}; + |