Skip to content

Commit

Permalink
[BE] 응답 수정 (#157)
Browse files Browse the repository at this point in the history
* 예외 필터 수정

* logger 추가

* 응답 수정
  • Loading branch information
twoo1999 authored Nov 30, 2023
1 parent e52c382 commit b3fcbda
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
1 change: 0 additions & 1 deletion BE/musicspot/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { JourneyModule } from './journey/module/journey.module';
import { SpotModule } from './spot/module/spot.module';
import { UserModule } from './user/module/user.module';


import { ReleaseController } from './releasePage/release.controller';

@Module({
Expand Down
57 changes: 29 additions & 28 deletions BE/musicspot/src/common/logger/winston.util.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { utilities, WinstonModule } from 'nest-winston';
import * as winstonDaily from 'winston-daily-rotate-file';
import * as winston from 'winston';

const logDir = `${__dirname}/../../logs`;
const options = (level: string) => {
return {
level,
datePattern: 'YYYY-MM-DD',
dirname: logDir + `/${level}`,
filename: `%DATE%.${level}.log`,
maxFiles: 30,
zippedArchive: true,
};
};
export const winstonLogger = WinstonModule.createLogger({
transports: [
new winston.transports.Console({
level: 'silly',
format: winston.format.combine(
winston.format.timestamp(),
utilities.format.nestLike('music-spot', { prettyPrint: true }),
),
}),
new winstonDaily(options('info')),
new winstonDaily(options('warn')),
new winstonDaily(options('error')),
],
});
// import { utilities, WinstonModule } from 'nest-winston';
// import * as winstonDaily from 'winston-daily-rotate-file';
// import * as winston from 'winston';

// const logDir = `${__dirname}/../../logs`;
// const options = (level: string) => {
// return {
// level,
// datePattern: 'YYYY-MM-DD',
// dirname: logDir + `/${level}`,
// filename: `%DATE%.${level}.log`,
// maxFiles: 30,
// zippedArchive: true,
// };
// };
// export const winstonLogger = WinstonModule.createLogger({
// transports: [
// new winston.transports.Console({
// level: 'silly',
// format: winston.format.combine(
// winston.format.timestamp(),
// utilities.format.nestLike('music-spot', { prettyPrint: true }),
// ),
// }),
// new winstonDaily(options('info')),
// new winstonDaily(options('warn')),
// new winstonDaily(options('error')),
// ],
// });
26 changes: 26 additions & 0 deletions BE/musicspot/src/common/middleware/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// import { Injectable, NestMiddleware } from '@nestjs/common';
// import { Response, Request, NextFunction } from 'express';
// import { winstonLogger } from '../logger/winston.util';

// @Injectable()
// export class LoggerMiddleware implements NestMiddleware {
// constructor() {}

// use(req: Request, res: Response, next: NextFunction) {
// const { ip, method, originalUrl } = req;

// res.on('finish', () => {
// const { statusCode } = res;
// console.log(statusCode);
// // if (statusCode >= 400 && statusCode < 500) {
// // winstonLogger.warn(`[${method}]${originalUrl}(${statusCode}) ${ip}`);
// // console.log('ASDS');
// // } else if (statusCode >= 500) {
// // winstonLogger.error(`[${method}]${originalUrl}(${statusCode}) ${ip}`);
// // }
// winstonLogger.warn(`[${method}]${originalUrl}(${statusCode}) ${ip}`);
// });

// next();
// }
// }
9 changes: 5 additions & 4 deletions BE/musicspot/src/filters/exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ export class AllExceptionFilter implements ExceptionFilter {
const req = ctx.getRequest();
const res = ctx.getResponse();
const err = exception;

const status = err.status;
if (err instanceof BaseException) {
err.timestamp = new Date().toISOString();
err.path = req.url;
res.status(err.statusCode).json({
res.status(status).json({
errorCode: err.errorCode,
statusCode: err.statusCode,
statusCode: status,
timestamp: err.timestamp,
path: err.path,
message: err.message,
});
} else {
res.send({ ...err, timestamp: new Date().toISOString() });
res.status(status).json(err);
// res.send({ ...err, timestamp: new Date().toISOString() });
}
}
}
6 changes: 4 additions & 2 deletions BE/musicspot/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { AllExceptionFilter } from './filters/exception.filter';
import { winstonLogger } from './common/logger/winston.util';

// import { winstonLogger } from './common/logger/winston.util';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: winstonLogger,
// logger: winstonLogger,

});
const config = new DocumentBuilder()
.setTitle('Music Spot') // 문서의 제목
Expand Down

0 comments on commit b3fcbda

Please sign in to comment.