better log

This commit is contained in:
Qiming Zhao 2022-04-02 05:45:18 +08:00
parent 9d7ee9da1f
commit b22c59c072
No known key found for this signature in database
GPG key ID: 9722CD0E8D4DCB8C

View file

@ -51,12 +51,24 @@ export default class Logger {
this.logLevel('Error', message, data) this.logLevel('Error', message, data)
} }
private now(): string {
const now = new Date()
return padLeft(now.getUTCHours() + '', 2, '0')
+ ':' + padLeft(now.getMinutes() + '', 2, '0')
+ ':' + padLeft(now.getUTCSeconds() + '', 2, '0') + '.' + now.getMilliseconds()
}
public logLevel(level: string, message: string, data?: any): void { public logLevel(level: string, message: string, data?: any): void {
this.output.appendLine( this.output.appendLine(
`[${level} - ${new Date().toLocaleTimeString()}] ${message}` `[${level} - ${this.now()}] ${message}`
) )
if (data) { if (data) {
this.output.appendLine(this.data2String(data)) this.output.appendLine(this.data2String(data))
} }
} }
} }
function padLeft(s: string, n: number, pad = ' ') {
return pad.repeat(Math.max(0, n - s.length)) + s
}