Kaydet (Commit) 71879178 authored tarafından Ali's avatar Ali

Debug function changed. setMessage added, setFormat added and getDateWithTime…

Debug function changed. setMessage added, setFormat added and getDateWithTime added. Logger class now works with singleton pattern
üst dd89f5db
...@@ -11,13 +11,21 @@ class Logger { ...@@ -11,13 +11,21 @@ class Logger {
private isFormatted: boolean private isFormatted: boolean
private formattedString: string private formattedString: string
/**
* Singleton instance
*/
private instance: Logger
private config: any = { private config: any = {
platform: 'web',
server: {
useServer: false, useServer: false,
endPoint: '', endPoint: '',
method: 'POST', method: 'POST',
headers: {}, headers: {},
body: {} body: {}
} }
}
private server: LogServer private server: LogServer
...@@ -26,12 +34,42 @@ class Logger { ...@@ -26,12 +34,42 @@ class Logger {
this.server = new LogServer(this.config.server) this.server = new LogServer(this.config.server)
if (!this.instance) {
this.instance = this
}
return this
}
/**
* @description This helps to get date with time
* @function getDateWithTime
* @example this.getDateWithTime() // output: 2019-05-16 12:03:48
*/
private getDateWithTime() {
const date = (new Date()).toISOString().split('T')[0]
const time = (new Date()).toISOString().split('T')[1].slice(0, 8)
return `${date} ${time}`
} }
public setFormat(formatStr: string) { public setFormat(formatStr: string) {
this.isFormatted = true this.isFormatted = true
} }
private setMessage(details: any) {
const logDate = this.getDateWithTime()
let logString = `[${logDate}] - ${details.foregroundColor}${details.backgroundColor} ${details.levelName} ${details.reset} - `
logString += `${details.message}`
logString = logString.trim()
return logString
}
/** /**
* @description Debug Level Message * @description Debug Level Message
* @function debug * @function debug
...@@ -43,14 +81,19 @@ class Logger { ...@@ -43,14 +81,19 @@ class Logger {
debug(message: string, context: any) { debug(message: string, context: any) {
const logCode = LogTypes.DEBUG const logCode = LogTypes.DEBUG
const levelName = Levels.GET(logCode) const levelName = Levels.GET(logCode)
const colors = Colors.Node //Colors.GET('node') const colors = this.config.platform == "web" ? Colors["Web"] : Colors["Node"]
const messageData = {
foregroundColor: colors.FgWhite,
backgroundColor: colors.BgRed,
levelName: levelName,
message: message,
reset: colors.Reset
}
const messageOutput = this.setMessage(messageData)
/** console.log(messageOutput)
* @todo This section will change. This needs formatted output
*/
console.log(logCode, levelName)
console.log(`${colors.FgWhite}${colors.BgRed}%s`, `${levelName}`, colors.Reset, '-', message);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment