-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from arshad-yaseen/improve-logger
Improved Logger
- Loading branch information
Showing
6 changed files
with
59 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,41 @@ | ||
/** | ||
* Logger class for consistent logging across the application. | ||
*/ | ||
class Logger { | ||
private static readonly instance: Logger = new Logger(); | ||
private static readonly RED = '\x1b[31m'; | ||
private static readonly YELLOW = '\x1b[33m'; | ||
private static readonly RESET = '\x1b[0m'; | ||
private static readonly BOLD = '\x1b[1m'; | ||
|
||
private constructor() {} | ||
|
||
public static getInstance(): Logger { | ||
return Logger.instance; | ||
const RED = '\x1b[91m'; | ||
const YELLOW = '\x1b[93m'; | ||
const RESET = '\x1b[0m'; | ||
const BOLD = '\x1b[1m'; | ||
|
||
export const report = (error: unknown): {message: string; stack?: string} => { | ||
let errorMessage: string; | ||
let errorStack: string | undefined; | ||
|
||
if (error instanceof Error) { | ||
errorMessage = error.message; | ||
errorStack = error.stack; | ||
} else if (typeof error === 'string') { | ||
errorMessage = error; | ||
} else { | ||
errorMessage = 'An unknown error occurred'; | ||
} | ||
|
||
public logError(error: unknown): {message: string; stack?: string} { | ||
let errorMessage: string; | ||
let errorStack: string | undefined; | ||
|
||
if (error instanceof Error) { | ||
errorMessage = error.message; | ||
errorStack = error.stack; | ||
} else if (typeof error === 'string') { | ||
errorMessage = error; | ||
} else { | ||
errorMessage = 'An unknown error occurred'; | ||
} | ||
|
||
const formattedError = `${Logger.RED}${Logger.BOLD}[MONACOPILOT ERROR] ${errorMessage}${Logger.RESET}`; | ||
const formattedError = `${RED}${BOLD}[MONACOPILOT ERROR] ${errorMessage}${RESET}`; | ||
if (errorStack) { | ||
console.error( | ||
`${formattedError}\n${RED}Stack trace:${RESET}\n${errorStack}`, | ||
); | ||
} else { | ||
console.error(formattedError); | ||
|
||
if (errorStack) { | ||
console.error( | ||
`${Logger.RED}[MONACOPILOT ERROR] Stack trace:${Logger.RESET}\n${errorStack}`, | ||
); | ||
} | ||
|
||
return {message: errorMessage, stack: errorStack}; | ||
} | ||
|
||
public warn(message: string): void { | ||
console.warn( | ||
`${Logger.YELLOW}${Logger.BOLD}[MONACOPILOT WARN] ${message}${Logger.RESET}`, | ||
); | ||
} | ||
return {message: errorMessage, stack: errorStack}; | ||
}; | ||
|
||
public log(message: string): void { | ||
console.log(`${Logger.BOLD}[MONACOPILOT] ${message}${Logger.RESET}`); | ||
} | ||
} | ||
export const deprecated = (message: string): void => { | ||
console.warn(`${YELLOW}${BOLD}[MONACOPILOT DEPRECATED] ${message}${RESET}`); | ||
}; | ||
|
||
export const warn = (message: string): void => { | ||
console.warn(`${YELLOW}${BOLD}[MONACOPILOT WARN] ${message}${RESET}`); | ||
}; | ||
|
||
export const logger = Logger.getInstance(); | ||
export const log = (message: string): void => { | ||
console.log(`${BOLD}[MONACOPILOT] ${message}${RESET}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters