-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
33 lines (28 loc) · 928 Bytes
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// TODO: Use proper logging like winston or bunyan
const format = require('date-fns/format')
const Logger = function () {}
/**
* Write an info to the console
* @param logText
*/
Logger.prototype.info = function (logText) {
console.log(`[${format(new Date(), 'dd-MM-yyyy HH:mm:ss')}] Info: ${logText}`)
}
/**
* Write an error to the console. If the NODE_ENV is not production, it will print the error stack too.
* @param logText
*/
Logger.prototype.error = function (logText) {
logText = process.env.NODE_ENV !== 'production' ? logText.stack : logText.message
console.log(
`[${format(new Date(), 'dd-MM-yyyy HH:mm:ss')}] Error: ${
logText instanceof Error ? logText.message : logText
}`
)
}
Logger.prototype.errorWithId = function (logText, errorId) {
console.log(
`[${format(new Date(), 'dd-MM-yyyy HH:mm:ss')}] Error: ${logText}. Error ID: ${errorId}`
)
}
module.exports = new Logger()