winston 을 이용하여 로그를 파일로 관리( 타임존 설정까지 )

import {NestFactory} from '@nestjs/core';
import {AppModule} from './app.module';
import * as winston from 'winston';
import {utilities, WinstonModule} from 'nest-winston';
const moment = require('moment-timezone');install moment
import { DateUtil } from "./common/common.date";

const level : string = process.env.NODE_ENV === 'production' ? 'error' : 'silly';
const format = winston.format.combine(
	// winston.format.timestamp(),
	winston.format((info, opts) => {
		info.timestamp = moment().tz('Asia/Seoul').format('YYYY-MM-DD HH:mm:ss');
		return info;
	})(),
	utilities.format.nestLike( 'log', {prettyPrint : true} ),
);

async function bootstrap()
{
	const logPath : string = './logs/';
	const app = await NestFactory.create( AppModule, {
		logger : WinstonModule.createLogger( {
			transports : [
				new winston.transports.Console( {  //콘솔출력 지정
					level : level,
					format : format,
				} ),
				new winston.transports.File( {  //파일저장 지정
					dirname : logPath + `${DateUtil.moment().format( 'YYYY-MM-DD' )}`,
					filename : 'history.log',
					level : level,
					format : format,
				} ),
			],
		} ),
	} );
	app.enableCors();
	await app.listen( 3000 );
}

bootstrap();
main.ts
.
.
.
constructor( private readonly logger: Logger ){}
.
.
.
this.logger.log( 'test', 'log test' );
사용예시

위처럼 설정하면 해당일 폴더에 로그가 파일로 기록된다.
( 파일은 날짜 지나거나 서버 내릴때 생성 )

덤으로 날짜는 서울로 맞춰짐

Subscribe to X세대 신입사원

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe
774-86-01972 cinnabar.3d@gmail.com