ロガー
意味を持った形式でコンソールメッセージを整形する、カプセル化されたロガー。
Docusaurus エコシステム内のパッケージ作者には、このパッケージを使って統一されたログフォーマットを提供することが推奨されています。
API
It exports a single object as default export: logger. logger has the following properties:
- 便利なカラーパレット。
redyellowgreenbolddim
- フォーマッター。 These functions all have the signature
(msg: unknown) => string. なお、これらの関数の実装は保証されていません。 重要なのは実装の詳細ではなく、それらが持つ意味(セマンティクス)です。path: formats a file path.url: formats a URL.name: formats an identifier.code: formats a code snippet.subdue: subdues the text.num: formats a number.
- The
interpolatefunction. これはテンプレートリテラルタグです。 構文は以下の通りです。 - ログ出力用の関数。 All logging functions can both be used as normal functions (similar to the
console.logfamily, but only accepts one parameter) or template literal tags.info: prints information.warn: prints a warning that should be paid attention to.error: prints an error (not necessarily halting the program) that signals significant problems.success: prints a success message.
- The
reportfunction. It takes aReportingSeverityvalue (ignore,log,warn,throw) and reports a message according to the severity.
error formatterBeware that an error message, even when it doesn't hang the program, is likely going to cause confusion. When users inspect logs and find an [ERROR], even when the build succeeds, they will assume something is going wrong. 使用は控えめにしてください。
Docusaurus only uses logger.error when printing messages immediately before throwing an error, or when user has set the reporting severity of onBrokenLink, etc. to "error".
In addition, warn and error will color the entire message for better attention. If you are printing large blocks of help text about an error, better use logger.info.
テンプレートリテラルタグの使用方法
テンプレートリテラルタグは、埋め込まれたテンプレート文字列や式を評価します。 interpolate returns a new string, while other logging functions prints it. 以下は典型的な使い方の例です:
import logger from '@docusaurus/logger';
logger.info`Hello name=${name}! You have number=${money} dollars. Here are the ${
items.length > 1 ? 'items' : 'item'
} on the shelf: ${items}
To buy anything, enter code=${'buy x'} where code=${'x'} is the item's name; to quit, press code=${'Ctrl + C'}.`;
An embedded expression is optionally preceded by a flag in the form [a-z]+= (a few lowercase letters, followed by an equals sign, directly preceding the embedded expression). 埋め込み式の前にフラグが付いていない場合は、その式の値がそのまま出力されます。 フラグが付いている場合は、指定されたフォーマッターのいずれかを使って整形されます:
path=:pathurl=:urlname=:namecode=:codesubdue=:subduenumber=:num
If the expression is an array, it's formatted by `\n- ${array.join('\n- ')}\n` (note it automatically gets a leading line end). 各要素は個別にフォーマットされ、箇条書きの「・」自体はフォーマットされません。 したがって、上記のメッセージは以下のように表示されます:
