# Logger

Logger

Nest에는 인스턴스화 프로세스 중에 사용되며 **발생한 예외** 등과 같은 여러 가지 상황에서 사용되는 내부 `Logger`의 기본 구현이 제공됩니다. 그러나 때로는 로깅을 완전히 비활성화하거나 사용자 지정 구현을 제공하고 메시지를 직접 처리 할 수도 있습니다. 로거를 끄려면 Nest의 옵션 객체를 사용합니다.

```typescript
const app = await NestFactory.create(ApplicationModule, {
  logger: false,
});
await app.listen(3000);
```

그럼에도 불구하고 전체 로깅 메커니즘을 비활성화 하는 대신 다른 로거를 사용하는 것이 좋습니다. 이를 위해서는 `LoggerService` 인터페이스를 만족시키는 객체를 전달해야 합니다. 예를 들어 내장 `콘솔`이 될 수 있습니다.

```typescript
const app = await NestFactory.create(ApplicationModule, {
  logger: console,
});
await app.listen(3000);
```

그러나 적절한 아이디어는 아닙니다. 그러나 자체 로거를 쉽게 만들 수 있습니다.

```typescript
import { LoggerService } from '@nestjs/common';

export class MyLogger implements LoggerService {
  log(message: string) {}
  error(message: string, trace: string) {}
  warn(message: string) {}
  debug(message: string) {}
  verbose(message: string) {}
}
```

그런 다음 MyLogger 인스턴스를 직접 적용 할 수 있습니다.

```typescript
const app = await NestFactory.create(ApplicationModule, {
  logger: new MyLogger(),
});
await app.listen(3000);
```

## Extend built-in logger

많은 유스 케이스는 자신의 로거를 작성해야 합니다. 바퀴를 완전히 재발명할 필요는 없습니다. 내장`Logger` 클래스를 확장하여 기본 구현을 부분적으로 무시하고 `super`를 사용하여 호출을 부모 클래스에 위임하십시오.

```typescript
import { Logger } from '@nestjs/common';

export class MyLogger extends Logger {
  error(message: string, trace: string) {
    // add your tailored logic here
    super.error(message, trace);
  }
}
```

## Dependency injection

로거에서 의존성 주입을 활성화하려면 MyLogger 클래스를 실제 애플리케이션의 일부로 만들어야합니다. 예를 들어 `LoggerModule`을 만들 수 있습니다.

```typescript
import { Module } from '@nestjs/common';
import { MyLogger } from './my-logger.service.ts';

@Module({
  providers: [MyLogger],
  exports: [MyLogger],
})
export class LoggerModule {}
```

LoggerModule을 어디서나 가져 오면 프레임 워크는 로거 인스턴스 생성을 담당합니다. 이제 부트 스트랩 및 오류 처리를 포함하여 전체 앱에서 동일한 로거 인스턴스를 사용하려면 다음 구성을 사용하십시오.

```typescript
const app = await NestFactory.create(ApplicationModule, {
  logger: false,
});
app.useLogger(app.get(MyLogger));
await app.listen(3000);
```

이 솔루션의 유일한 단점은 첫번째 초기화 메시지가 로거 인스턴스에서 처리되지 않지만 실제로는 중요하지 않다는 것입니다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jakekwak.gitbook.io/nestjs/techniques/logger.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
