mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-19 01:31:34 +00:00
Updated logger.js
Manually parse date to actually print the dates in current timezone. I actually never intended it to use UTC. I wasn't really paying attention... Also during development, shortened version will be used instead, which is basically only showing hours, mins, and secs.
This commit is contained in:
parent
6a934627a2
commit
2e40124c62
30
logger.js
30
logger.js
@ -2,6 +2,31 @@ const { inspect } = require('util')
|
|||||||
|
|
||||||
const self = {}
|
const self = {}
|
||||||
|
|
||||||
|
// 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}`
|
||||||
|
}
|
||||||
|
|
||||||
const clean = item => {
|
const clean = item => {
|
||||||
if (typeof item === 'string') return item
|
if (typeof item === 'string') return item
|
||||||
const cleaned = inspect(item, { depth: 0 })
|
const cleaned = inspect(item, { depth: 0 })
|
||||||
@ -9,11 +34,8 @@ const clean = item => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const write = (content, options = {}) => {
|
const write = (content, options = {}) => {
|
||||||
const date = new Date().toISOString()
|
|
||||||
.replace(/T/, ' ')
|
|
||||||
.replace(/\..*/, '')
|
|
||||||
const stream = options.error ? process.stderr : process.stdout
|
const stream = options.error ? process.stderr : process.stdout
|
||||||
stream.write(`[${date}]: ${options.prefix || ''}${clean(content)}\n`)
|
stream.write(`[${now()}] ${options.prefix || ''}${clean(content)}\n`)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.log = write
|
self.log = write
|
||||||
|
Loading…
Reference in New Issue
Block a user