-
Notifications
You must be signed in to change notification settings - Fork 322
[php-wasm-logger] Filter logs by severity in Logger and assign severity based on verbosity argument in CLIs #2436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mho22
wants to merge
15
commits into
trunk
Choose a base branch
from
add-verbose-option-in-xdebug-bridge
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
071ffc2
Add quiet and verbose options in Xdebug Bridge
mho22 400999f
Refine README file
mho22 805dc23
Add tests
mho22 d5c3140
Lint
mho22 436e46d
Add verbosity in logger and xdebug-bridge
mho22 f48d972
Merge branch 'trunk' into add-verbose-option-in-xdebug-bridge
mho22 a8a6421
Add verbosity tests in Playground CLI
mho22 bcf694c
Merge branch 'trunk' into add-verbose-option-in-xdebug-bridge
mho22 61908a9
Add verbosity level instead of array of filters in Logger
mho22 1cdcec7
Adjust how quiet is promoted to verbosity
adamziel 00d7e5c
Adjust how debug is promoted to verbosity
adamziel 39c5d90
Support .logMessage() calls without an explicit severity
adamziel ace24c4
Merge branch 'trunk' into add-verbose-option-in-xdebug-bridge
mho22 33dcebb
Merge branch 'add-verbose-option-in-xdebug-bridge' of github.com:Word…
mho22 7350c02
Refactored verbosity and severity use in CLIs and Logger
mho22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -12,27 +12,54 @@ export { errorLogPath } from './collectors/collect-php-logs'; | |
|
||
export type Log = { | ||
message: any; | ||
severity?: LogSeverity; | ||
severity: LogSeverity; | ||
prefix?: LogPrefix; | ||
raw?: boolean; | ||
}; | ||
|
||
/** | ||
* Log verbosity levels | ||
*/ | ||
export const LogVerbosity = { | ||
Normal: 'normal', | ||
Quiet: 'quiet', | ||
Debug: 'debug', | ||
} as const; | ||
|
||
export type LogVerbosity = (typeof LogVerbosity)[keyof typeof LogVerbosity]; | ||
|
||
/** | ||
* Log severity levels. | ||
*/ | ||
export type LogSeverity = 'Debug' | 'Info' | 'Warn' | 'Error' | 'Fatal'; | ||
export const LogSeverity = { | ||
Log: { name: 'log', level: 1 }, | ||
Info: { name: 'info', level: 1 }, | ||
Warn: { name: 'warn', level: 1 }, | ||
Error: { name: 'error', level: 1 }, | ||
Fatal: { name: 'fatal', level: 1 }, | ||
Debug: { name: 'debug', level: 2 }, | ||
} as const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's give each severity a unique level, e.g. export const LogSeverity = {
Fatal: { name: 'fatal', level: 0 },
Error: { name: 'error', level: 1 },
Warn: { name: 'warn', level: 2 },
Log: { name: 'log', level: 3 },
Info: { name: 'info', level: 4 },
Debug: { name: 'debug', level: 5 },
} as const; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
|
||
export type LogSeverity = (typeof LogSeverity)[keyof typeof LogSeverity]; | ||
|
||
/** | ||
* Log prefix. | ||
*/ | ||
export type LogPrefix = 'WASM Crash' | 'PHP' | 'JavaScript'; | ||
export const LogPrefix = { | ||
WASM: 'Wasm Crash', | ||
PHP: 'PHP', | ||
JS: 'JavaScript', | ||
} as const; | ||
|
||
export type LogPrefix = (typeof LogPrefix)[keyof typeof LogPrefix]; | ||
|
||
/** | ||
* A logger for Playground. | ||
*/ | ||
export class Logger extends EventTarget { | ||
public readonly fatalErrorEvent = 'playground-fatal-error'; | ||
private readonly handlers: LogHandler[]; | ||
private handlers: LogHandler[]; | ||
private verbosity = 1; | ||
|
||
// constructor | ||
constructor( | ||
|
@@ -62,14 +89,39 @@ export class Logger extends EventTarget { | |
/** | ||
* Log message with severity. | ||
* | ||
* @param message any | ||
* @param severity LogSeverity | ||
* @param raw boolean | ||
* @param log Log | ||
* @param args any | ||
*/ | ||
public logMessage(log: Log, ...args: any[]): void { | ||
public logMessage( | ||
log: Omit<Log, 'severity'> & { severity?: LogSeverity }, | ||
...args: any[] | ||
): void { | ||
const logWithSeverity: Log = { | ||
...log, | ||
severity: log.severity ?? LogSeverity.Log, | ||
}; | ||
for (const handler of this.handlers) { | ||
handler(log, ...args); | ||
if (logWithSeverity.severity.level <= this.verbosity) { | ||
handler(logWithSeverity, ...args); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Filter message based on verbosiy | ||
* @param verbosity LogVerbosity | ||
*/ | ||
public filterByVerbosity(verbosity: LogVerbosity): void { | ||
switch (verbosity) { | ||
case LogVerbosity.Quiet: | ||
this.verbosity = 0; | ||
break; | ||
case LogVerbosity.Normal: | ||
this.verbosity = 1; | ||
break; | ||
case LogVerbosity.Debug: | ||
this.verbosity = 2; | ||
break; | ||
} | ||
} | ||
|
||
|
@@ -83,8 +135,8 @@ export class Logger extends EventTarget { | |
this.logMessage( | ||
{ | ||
message, | ||
severity: undefined, | ||
prefix: 'JavaScript', | ||
severity: LogSeverity.Log, | ||
prefix: LogPrefix.JS, | ||
raw: false, | ||
}, | ||
...args | ||
|
@@ -101,8 +153,8 @@ export class Logger extends EventTarget { | |
this.logMessage( | ||
{ | ||
message, | ||
severity: 'Debug', | ||
prefix: 'JavaScript', | ||
severity: LogSeverity.Debug, | ||
prefix: LogPrefix.JS, | ||
raw: false, | ||
}, | ||
...args | ||
|
@@ -119,8 +171,8 @@ export class Logger extends EventTarget { | |
this.logMessage( | ||
{ | ||
message, | ||
severity: 'Info', | ||
prefix: 'JavaScript', | ||
severity: LogSeverity.Info, | ||
prefix: LogPrefix.JS, | ||
raw: false, | ||
}, | ||
...args | ||
|
@@ -137,8 +189,8 @@ export class Logger extends EventTarget { | |
this.logMessage( | ||
{ | ||
message, | ||
severity: 'Warn', | ||
prefix: 'JavaScript', | ||
severity: LogSeverity.Warn, | ||
prefix: LogPrefix.JS, | ||
raw: false, | ||
}, | ||
...args | ||
|
@@ -155,8 +207,8 @@ export class Logger extends EventTarget { | |
this.logMessage( | ||
{ | ||
message, | ||
severity: 'Error', | ||
prefix: 'JavaScript', | ||
severity: LogSeverity.Error, | ||
prefix: LogPrefix.JS, | ||
raw: false, | ||
}, | ||
...args | ||
|
@@ -209,7 +261,7 @@ export const formatLogEntry = ( | |
}).format(date); | ||
const now = formattedDate + ' ' + formattedTime; | ||
message = prepareLogMessage(message); | ||
return `[${now}] ${prefix} ${severity}: ${message}`; | ||
return `[${now}] ${prefix} ${severity.name}: ${message}`; | ||
}; | ||
|
||
/** | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not distinguish between verbosity and severity at the logger level. One concept is enough. We can decide what each verbosity means in the CLI args parser when we let the user decide. All internal methods could use severities for filtering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.