-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
565ded4
commit 954d037
Showing
5 changed files
with
130 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type Manager from './manager'; | ||
|
||
export interface ILoggerOpts { | ||
/** | ||
* 0 - disabled | ||
* 1 - error | ||
* 2 - warning | ||
* 3 - info | ||
* 4 - debug | ||
*/ | ||
level: number; | ||
manager: Manager; | ||
} | ||
|
||
export interface ILoggerLogOpts { | ||
level: ILoggerOpts['level']; | ||
err?: Error; | ||
payload?: Record<string, any>; | ||
} | ||
|
||
class Logger { | ||
/** | ||
* Logger options | ||
*/ | ||
protected options: ILoggerOpts; | ||
|
||
/** | ||
* @constructor | ||
*/ | ||
constructor(opts: ILoggerOpts) { | ||
this.options = opts; | ||
} | ||
|
||
/** | ||
* Log message | ||
*/ | ||
public log(msg: string, { level, err, payload }: ILoggerLogOpts): void { | ||
if (this.options.level < level) { | ||
return; | ||
} | ||
|
||
let type = 'log'; | ||
|
||
switch (level) { | ||
case 1: | ||
type = 'error'; | ||
break; | ||
|
||
case 2: | ||
type = 'warn'; | ||
break; | ||
|
||
case 3: | ||
type = 'info'; | ||
break; | ||
} | ||
|
||
console[type](...[msg, err, payload].filter(Boolean)); | ||
} | ||
|
||
/** | ||
* Log error message | ||
*/ | ||
public err(msg: string, err?: unknown, payload?: Record<string, any>): void { | ||
this.log(msg, { err: err as Error, level: 1, payload }); | ||
} | ||
|
||
/** | ||
* Log warning message | ||
*/ | ||
public warn(msg: string, payload?: Record<string, any>): void { | ||
this.log(msg, { level: 2, payload }); | ||
} | ||
|
||
/** | ||
* Log info message | ||
*/ | ||
public info(msg: string, payload?: Record<string, any>): void { | ||
this.log(msg, { level: 3, payload }); | ||
} | ||
|
||
/** | ||
* Log debug message | ||
*/ | ||
public debug(msg: string, payload: Record<string, any> = {}, hasSnapshot = false): void { | ||
if (hasSnapshot) { | ||
payload.additional = { | ||
relations: Object.fromEntries(this.options.manager.getStoresRelations().entries()), | ||
}; | ||
} | ||
|
||
this.log(`DEBUG: ${msg}`, { level: 4, payload: { ...payload } }); | ||
} | ||
} | ||
|
||
export default Logger; |
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