Skip to content

Commit

Permalink
log: winston log
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungGwan123 committed Nov 26, 2024
1 parent 42686f9 commit bc97a8f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import { ScheduleModule as NestScheduleModule } from '@nestjs/schedule';
import { ScheduleModule } from './schedule/schedule.module';
import { TradehistoryModule } from './trade-history/trade-history.module';
import { FavoriteModule } from './favorite/favorite.module';
import { winstonConfig } from './configs/winston.config';
import { WinstonModule } from 'nest-winston';

@Module({
imports: [
AuthModule,
imports: [
AuthModule,
HealthModule,
AccountModule,
TradeModule,
Expand All @@ -31,6 +33,7 @@ import { FavoriteModule } from './favorite/favorite.module';
ScheduleModule,
TradehistoryModule,
FavoriteModule,
WinstonModule.forRoot(winstonConfig),
],
controllers: [AppController],
providers: [AppService],
Expand Down
92 changes: 92 additions & 0 deletions packages/server/src/configs/winston.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { utilities, WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import * as winstonDaily from 'winston-daily-rotate-file';
import { join } from 'path';

const { combine, timestamp, printf, colorize } = winston.format;

const levels = {
error: 0,
warn: 1,
info: 2,
http: 3,
debug: 4,
}

const colors = {
error: 'red',
warn: 'yellow',
info: 'green',
http: 'magenta',
debug: 'blue',
}

winston.addColors(colors);

// 로그 저장 경로
const logDir = join(__dirname, '../../logs');

// 로그 포맷 정의
const logFormat = printf(({ level, message, timestamp, stack }) => {
return `${timestamp} ${level}: ${message} ${stack || ''}`;
});

export const winstonConfig = {
levels,
transports: [
// 콘솔 출력
new winston.transports.Console({
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
format: combine(
colorize({ all: true }),
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
utilities.format.nestLike('MyApp', {
prettyPrint: true,
colors: true,
}),
),
}),

// info 레벨 로그 파일
new winstonDaily({
level: 'info',
datePattern: 'YYYY-MM-DD',
dirname: join(logDir, 'info'),
filename: `%DATE%.log`,
maxFiles: 30,
zippedArchive: true,
format: combine(
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
logFormat,
),
}),

// warn 레벨 로그 파일
new winstonDaily({
level: 'warn',
datePattern: 'YYYY-MM-DD',
dirname: join(logDir, 'warn'),
filename: `%DATE%.warn.log`,
maxFiles: 30,
zippedArchive: true,
format: combine(
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
logFormat,
),
}),

// error 레벨 로그 파일
new winstonDaily({
level: 'error',
datePattern: 'YYYY-MM-DD',
dirname: join(logDir, 'error'),
filename: `%DATE%.error.log`,
maxFiles: 30,
zippedArchive: true,
format: combine(
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
logFormat,
),
}),
],
};
6 changes: 5 additions & 1 deletion packages/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {
import { config } from 'dotenv';
import { setupSshTunnel } from './configs/ssh-tunnel';
import { AllExceptionsFilter } from 'common/all-exceptions.filter';
import { WinstonModule } from 'nest-winston';
import { winstonConfig } from './configs/winston.config';

config();

async function bootstrap() {
await setupSshTunnel();
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, {
logger: WinstonModule.createLogger(winstonConfig),
});
app.enableCors({
origin: true,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
Expand Down

0 comments on commit bc97a8f

Please sign in to comment.