1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-06-14 19:59:17 +00:00

Added a timestamp to the logger

Signed-off-by: Florian BOUILLON <florian.bouillon@delta-wings.net>
This commit is contained in:
2021-08-19 14:14:38 +02:00
parent 692eb31e0a
commit fe73433742
4 changed files with 21631 additions and 5 deletions

View File

@ -22,6 +22,14 @@ export default class Logger implements Console {
*/
private static loggers: Array<Logger> = []
/**
*Define if you want a timestamp with your logs
*
* @static
* @memberof Logger
*/
public static timestamp = false
/**
* Console memory
* (IDK what it is)
@ -367,7 +375,16 @@ export default class Logger implements Console {
}
const res: Array<any> = [
`${this.blackOrWhite('[ ')}${spacers[0]}${blue(prefix)}${spacers[1]}${this.blackOrWhite(' ]')}:`
].concat(
]
if (Logger.timestamp) {
const now = new Date()
const h = now.getHours() >= 10 ? now.getHours().toString() : `0${now.getHours()}`
const m = now.getMinutes() >= 10 ? now.getMinutes().toString() : `0${now.getMinutes()}`
const s = now.getSeconds() >= 10 ? now.getSeconds().toString() : `0${now.getSeconds()}`
res.unshift(`${this.blackOrWhite('<')}${yellow(h)}:${yellow(m)}:${yellow(s)}${this.blackOrWhite('>')}`)
}
return res.concat(
message.map((el) => {
if (typeof el === 'object') {
return el
@ -381,7 +398,6 @@ export default class Logger implements Console {
return typeof el !== 'string' ? yellow(el.toString()) : green(el)
})
)
return res
}
/**