-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
refactor: move PerfLogger from core to @docusaurus/logger #10480
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import chalk from 'chalk'; | ||
| import type {ReportingSeverity} from '@docusaurus/types'; | ||
|
|
||
| type InterpolatableValue = string | number | (string | number)[]; | ||
|
|
||
| const path = (msg: unknown): string => chalk.cyan.underline(`"${String(msg)}"`); | ||
| const url = (msg: unknown): string => chalk.cyan.underline(msg); | ||
| const name = (msg: unknown): string => chalk.blue.bold(msg); | ||
| const code = (msg: unknown): string => chalk.cyan(`\`${String(msg)}\``); | ||
| const subdue = (msg: unknown): string => chalk.gray(msg); | ||
| const num = (msg: unknown): string => chalk.yellow(msg); | ||
|
|
||
| function interpolate( | ||
| msgs: TemplateStringsArray, | ||
| ...values: InterpolatableValue[] | ||
| ): string { | ||
| let res = ''; | ||
| values.forEach((value, idx) => { | ||
| const flag = msgs[idx]!.match(/[a-z]+=$/); | ||
| res += msgs[idx]!.replace(/[a-z]+=$/, ''); | ||
Check failureCode scanning / CodeQL Polynomial regular expression used on uncontrolled data
This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](3) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](4) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](5) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](6) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](7) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](8) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](9) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](10) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](11) may run slow on strings with many repetitions of 'a'.
This [regular expression](1) that depends on [library input](12) may run slow on strings with many repetitions of 'a'.
|
||
| const format = (() => { | ||
| if (!flag) { | ||
| return (a: string | number) => a; | ||
| } | ||
| switch (flag[0]) { | ||
| case 'path=': | ||
| return path; | ||
| case 'url=': | ||
| return url; | ||
| case 'number=': | ||
| return num; | ||
| case 'name=': | ||
| return name; | ||
| case 'subdue=': | ||
| return subdue; | ||
| case 'code=': | ||
| return code; | ||
| default: | ||
| throw new Error( | ||
| 'Bad Docusaurus logging message. This is likely an internal bug, please report it.', | ||
| ); | ||
| } | ||
| })(); | ||
| res += Array.isArray(value) | ||
| ? `\n- ${value.map((v) => format(v)).join('\n- ')}` | ||
| : format(value); | ||
| }); | ||
| res += msgs.slice(-1)[0]; | ||
| return res; | ||
| } | ||
|
|
||
| function stringify(msg: unknown): string { | ||
| if (String(msg) === '[object Object]') { | ||
| return JSON.stringify(msg); | ||
| } | ||
| if (msg instanceof Date) { | ||
| return msg.toUTCString(); | ||
| } | ||
| return String(msg); | ||
| } | ||
|
|
||
| function info(msg: unknown): void; | ||
| function info( | ||
| msg: TemplateStringsArray, | ||
| ...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
| ): void; | ||
| function info(msg: unknown, ...values: InterpolatableValue[]): void { | ||
| console.info( | ||
| `${chalk.cyan.bold('[INFO]')} ${ | ||
| values.length === 0 | ||
| ? stringify(msg) | ||
| : interpolate(msg as TemplateStringsArray, ...values) | ||
| }`, | ||
| ); | ||
| } | ||
| function warn(msg: unknown): void; | ||
| function warn( | ||
| msg: TemplateStringsArray, | ||
| ...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
| ): void; | ||
| function warn(msg: unknown, ...values: InterpolatableValue[]): void { | ||
| console.warn( | ||
| chalk.yellow( | ||
| `${chalk.bold('[WARNING]')} ${ | ||
| values.length === 0 | ||
| ? stringify(msg) | ||
| : interpolate(msg as TemplateStringsArray, ...values) | ||
| }`, | ||
| ), | ||
| ); | ||
| } | ||
| function error(msg: unknown): void; | ||
| function error( | ||
| msg: TemplateStringsArray, | ||
| ...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
| ): void; | ||
| function error(msg: unknown, ...values: InterpolatableValue[]): void { | ||
| console.error( | ||
| chalk.red( | ||
| `${chalk.bold('[ERROR]')} ${ | ||
| values.length === 0 | ||
| ? stringify(msg) | ||
| : interpolate(msg as TemplateStringsArray, ...values) | ||
| }`, | ||
| ), | ||
| ); | ||
| } | ||
| function success(msg: unknown): void; | ||
| function success( | ||
| msg: TemplateStringsArray, | ||
| ...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
| ): void; | ||
| function success(msg: unknown, ...values: InterpolatableValue[]): void { | ||
| console.log( | ||
| `${chalk.green.bold('[SUCCESS]')} ${ | ||
| values.length === 0 | ||
| ? stringify(msg) | ||
| : interpolate(msg as TemplateStringsArray, ...values) | ||
| }`, | ||
| ); | ||
| } | ||
| function throwError(msg: unknown): void; | ||
| function throwError( | ||
| msg: TemplateStringsArray, | ||
| ...values: [InterpolatableValue, ...InterpolatableValue[]] | ||
| ): void; | ||
| function throwError(msg: unknown, ...values: InterpolatableValue[]): void { | ||
| throw new Error( | ||
| values.length === 0 | ||
| ? stringify(msg) | ||
| : interpolate(msg as TemplateStringsArray, ...values), | ||
| ); | ||
| } | ||
|
|
||
| function newLine(): void { | ||
| console.log(); | ||
| } | ||
|
|
||
| /** | ||
| * Takes a message and reports it according to the severity that the user wants. | ||
| * | ||
| * - `ignore`: completely no-op | ||
| * - `log`: uses the `INFO` log level | ||
| * - `warn`: uses the `WARN` log level | ||
| * - `throw`: aborts the process, throws the error. | ||
| * | ||
| * Since the logger doesn't have logging level filters yet, these severities | ||
| * mostly just differ by their colors. | ||
| * | ||
| * @throws In addition to throwing when `reportingSeverity === "throw"`, this | ||
| * function also throws if `reportingSeverity` is not one of the above. | ||
| */ | ||
| function report(reportingSeverity: ReportingSeverity): typeof success { | ||
| const reportingMethods = { | ||
| ignore: () => {}, | ||
| log: info, | ||
| warn, | ||
| throw: throwError, | ||
| }; | ||
| if ( | ||
| !Object.prototype.hasOwnProperty.call(reportingMethods, reportingSeverity) | ||
| ) { | ||
| throw new Error( | ||
| `Unexpected "reportingSeverity" value: ${reportingSeverity}.`, | ||
| ); | ||
| } | ||
| return reportingMethods[reportingSeverity]; | ||
| } | ||
|
|
||
| const logger = { | ||
| red: (msg: string | number): string => chalk.red(msg), | ||
| yellow: (msg: string | number): string => chalk.yellow(msg), | ||
| green: (msg: string | number): string => chalk.green(msg), | ||
| bold: (msg: string | number): string => chalk.bold(msg), | ||
| dim: (msg: string | number): string => chalk.dim(msg), | ||
| path, | ||
| url, | ||
| name, | ||
| code, | ||
| subdue, | ||
| num, | ||
| interpolate, | ||
| info, | ||
| warn, | ||
| error, | ||
| success, | ||
| report, | ||
| newLine, | ||
| }; | ||
|
|
||
| export default logger; | ||
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.
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data