2019-08-26 17:02:06 +00:00
|
|
|
const { inspect } = require('util')
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const self = {}
|
2019-08-26 17:02:06 +00:00
|
|
|
|
2019-09-21 07:29:08 +00:00
|
|
|
// Only show shortened version (time) during development
|
|
|
|
const short = process.env.NODE_ENV === 'development'
|
|
|
|
|
|
|
|
const now = () => {
|
|
|
|
const time = new Date()
|
|
|
|
const parsed = {
|
|
|
|
hours: time.getHours(),
|
|
|
|
minutes: time.getMinutes(),
|
|
|
|
seconds: time.getSeconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!short) {
|
|
|
|
parsed.month = time.getMonth() + 1
|
|
|
|
parsed.date = time.getDate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add leading zeroes and slice
|
|
|
|
Object.keys(parsed).forEach(key => {
|
|
|
|
parsed[key] = ('0' + parsed[key]).slice(-2)
|
|
|
|
})
|
|
|
|
|
|
|
|
return (!short ? `${time.getFullYear()}-${parsed.month}-${parsed.date} ` : '') +
|
|
|
|
`${parsed.hours}:${parsed.minutes}:${parsed.seconds}`
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const clean = item => {
|
2019-08-26 17:02:06 +00:00
|
|
|
if (typeof item === 'string') return item
|
|
|
|
const cleaned = inspect(item, { depth: 0 })
|
|
|
|
return cleaned
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const write = (content, options = {}) => {
|
2019-08-26 17:02:06 +00:00
|
|
|
const stream = options.error ? process.stderr : process.stdout
|
2019-09-21 07:29:08 +00:00
|
|
|
stream.write(`[${now()}] ${options.prefix || ''}${clean(content)}\n`)
|
2019-08-26 17:02:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.log = write
|
2019-08-26 17:02:06 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.error = (content, options = {}) => {
|
2019-08-26 17:02:06 +00:00
|
|
|
options.error = true
|
2019-09-08 01:56:29 +00:00
|
|
|
write(content, options)
|
2019-08-26 17:02:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 18:19:20 +00:00
|
|
|
self.debug = (...args) => {
|
|
|
|
for (const arg of args)
|
|
|
|
console.log(inspect(arg, { depth: Infinity }))
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
module.exports = self
|