-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): improve logging for contract build and interactions
- Loading branch information
1 parent
9c88678
commit 6946502
Showing
7 changed files
with
80 additions
and
57 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,39 @@ | ||
import { LogLevel, LogMethods, Logger } from '@tact-lang/compiler'; | ||
import EventEmitter from './eventEmitter'; | ||
|
||
const logLevelToMethodName: { [key in LogLevel]: keyof LogMethods | null } = { | ||
[LogLevel.NONE]: null, | ||
[LogLevel.ERROR]: 'error', | ||
[LogLevel.WARN]: 'warn', | ||
[LogLevel.INFO]: 'info', | ||
[LogLevel.DEBUG]: 'debug', | ||
}; | ||
|
||
function getLoggingMethod(level: LogLevel): keyof LogMethods | null { | ||
return logLevelToMethodName[level]; | ||
} | ||
|
||
export default class TactLogger extends Logger { | ||
private levelLevel: LogLevel; | ||
constructor(level: LogLevel = LogLevel.INFO) { | ||
super(level); | ||
this.levelLevel = level; | ||
} | ||
protected log(level: LogLevel, message: string | Error): void { | ||
if (this.levelLevel === LogLevel.NONE) { | ||
return; | ||
} | ||
|
||
message = message instanceof Error ? message.message : message; | ||
|
||
if (level > this.levelLevel) return; | ||
const loggingMethod = getLoggingMethod(level); | ||
if (!loggingMethod) return; | ||
|
||
EventEmitter.emit('LOG', { | ||
text: message, | ||
type: loggingMethod as Exclude<keyof LogMethods, 'debug' | 'warn'>, | ||
timestamp: new Date().toISOString(), | ||
}); | ||
} | ||
} |