설정만 보기

진짜 클로드 없이 어떻게 개발하지?

먼저 의존성 추가

npm install winston nest-winston winston-daily-rotate-file cross-env

package.json 변경

{
	...
  "scripts": {
    "build": "nest build",
    "start:prod": "cross-env NODE_ENV=production nest start",
    "start:dev": "cross-env NODE_ENV=development nest start --watch",
  },
	...
}

AppMoudle에서 Nest-Winston 설정

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

import { WinstonModule } from 'nest-winston';
import { logDir, logFormat } from '../loggerconfig';
import * as DailyRotateFile from 'winston-daily-rotate-file';
import * as winston from 'winston';
import * as process from 'node:process';

const { combine, timestamp, label } = winston.format;

// production 환경과 development 환경을 구분하기 위한 flag
const productionFlag = process.env.NODE_ENV === 'production';

@Module({
  imports: [
    WinstonModule.forRoot({
	    // 여기에 들어가는 내용은 기존의 winston 설정과 같음.
      // 로그 출력 형식에 대한 정의
      format: combine(
        timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
        label({ label: 'Winston 연습 애플리케이션' }),
        logFormat,
      ),

      // 로그를 어떻게 기록할 것인가?
      transports: [
        // 조건부 배열 확장 문법
        ...(productionFlag) ? [
          //info 레벨 로그 파일 설정
          new DailyRotateFile({
            level: 'info', // info level 설정
            datePattern: 'YYYY-MM-DD', // 파일 날짜 형식
            dirname: logDir, // 저장할 파일 경로
            filename: `%DATE%.log`, // 파일 이름
            maxFiles: 30, // 최근 30일치 로그를 남긴다
            zippedArchive: true, // 아카이브된 로그 파일을 gzip으로 압축할 것인가?
          }),
          //error 레벨 로그 파일 설정
          new DailyRotateFile({
            level: 'error',
            datePattern: 'YYYY-MM-DD',
            dirname: `${logDir}/error`,
            filename: `%DATE%.error.log`,
            maxFiles: 30,
            zippedArchive: true,
          }),
        ] : [
          // 콘솔에 로그 출력
          new winston.transports.Console({
            format: winston.format.combine(
              winston.format.colorize(),
              timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
              label({ label: 'Winston 연습 애플리케이션' }),
              logFormat,
            ),
          }),
        ],
      ],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
}

winston 설정을 위한 값 생성

import * as winston from 'winston';
import * as process from 'node:process';

const { printf } = winston.format;

// 로그 저장 경로
export const logDir = `${process.cwd()}/logs`;

// 로그 출력 포맷 정의 함수
export const logFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} [${label}] ${level}: ${message}`;
});

로깅

로깅이 뭘까?

그러면 로깅을 왜 해야 할까?