메인 컨텐츠로 이동
버전: 3.2.1

로거

의미있는 포맷으로 콘솔 메시지를 지정하기 위해 캡슐화된 로거입니다.

도큐사우루스 생태계 내에서 패키지를 작성한다면 이 패키지를 사용해 통합 로그 형식을 제공하는 것을 권장합니다.

API

It exports a single object as default export: logger. logger has the following properties:

  • 일부 유용한 색상
    • red
    • yellow
    • green
    • bold
    • dim
  • 포맷터. 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 interpolate function. 템플릿 리터럴 태그입니다. 문법은 아래 로깅 함수와 비슷합니다.
  • 로깅 함수 All logging functions can both be used as normal functions (similar to the console.log family, 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 report function. It takes a ReportingSeverity value (ignore, log, warn, throw) and reports a message according to the severity.
A word on the error formatter

Beware 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=: path
  • url=: url
  • name=: name
  • code=: code
  • subdue=: subdue
  • number=: num

If the expression is an array, it's formatted by `\n- ${array.join('\n- ')}\n` (note it automatically gets a leading line end). 각 항목은 자체적으로 포맷이 처리되며 글머리 기호는 포맷이 지정되지 않습니다. 따라서 위의 메시지는 다음과 같이 처리됩니다.

Some text output in the terminal, containing array, code, name, and number formatting