1.1.0-beta1 • Published 7 months ago
@lactea-blue/lululala v1.1.0-beta1
lululala
서버에서 발생하는 로그들을 같은 패턴으로 파일에 저장하는 라이브러리
local 환경에서는 console 창에도 로그가 같이 기록된다.
로그는 2가지 타입으(api-log, console-log, exception-log)로 나눌 수 있다.
api-log는 API의 request, response 값을 저장하며,
exception-log는 exception이 발생하였을 때, exception 메세지를 저장하는 로그이며,
console-log는 프로그래머 지정 로그를 저장한다.
Installation
$ npm install --save @lactea-blue/lululala
How to use
- configuration 라이브러리를 다운받는다.
기록되는 로그 파일의 이름을 환경변수 지정해주기 위해 해당 라이브러리를 사용한다. - configuration이 참조할 환경 변수 파일(.env 파일)에 해당 변수들을 지정한다.
API_LOG_FILE_NAME_PREFIX=api-test-server (api-log가 저장되는 파일 이름) CONSOLE_FILE_NAME_PREFIX=cosole-test-server (console-log가 저장되는 파일 이름) EXCEPTION_LOG_FILE_NAME_PREFIX=exception-test-server (exception-log가 저장되는 파일 이름)
- main 함수에서 global interceptor로 지정한다.
import { NestFactory } from '@nestjs/core'; import {ConfigService} from "@nestjs/config"; import {LoggingInterceptor} from "@lactea-blue/lululala"; async function bootstrap() { const app = await NestFactory.create(AppModule) let configService = app.get(ConfigService); app.useGlobalInterceptors(new LoggingInterceptor(configService)) await app.listen(3000) } bootstrap();
- 프로그램 build
nest build
- 프로그램 실행시 아래를 참조하여 각 환경에 맞는 시스템 환경변수(NODE_ENV)를 지정해준다.
#로컬 환경: NODE_ENV=local #개발 환경: NODE_ENV=dev | NODE_ENV=development #운영 환경: NODE_ENV=prod | NODE_ENV=production NODE_ENV=local nest start
Custom Log
- 방법 1
main.ts에 log를 기록할 수 있는 logger를 global 변수로 등록한다.
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import {CustomLogger, LoggingInterceptor} from "@lactea-blue/lululala"; import {ConfigService} from "@nestjs/config"; async function bootstrap() { const app = await NestFactory.create(AppModule); let configService = app.get(ConfigService); app.useGlobalInterceptors(new LoggingInterceptor(configService)) //global 변수로 설정 global.customLogger = new CustomLogger(configService).createLogger(); await app.listen(3000); } bootstrap();
실제 서비스에서 사용
class TestServcie { async testFunction() { global.customLogger.info("test용 custion log"); } }
방법 2
import {ConfigService} from "@nestjs/config"; import {CustomLogger} from "@lactea-blue/lululala"; class TestServcie { constructor( private readonly configService: ConfigService ) {} private logger = new CustomLogger(this.configService).createLogger(); async testFunction() { this.logger.info("test용 custion log"); } }
File output
- api-log
위의 형태로 ./logs/api/{api-log 파일 이름}_yyyy-dd-mm.log 파일에 적힌다.{ "@timestamp": "2023-01-04T09:01:41.065Z", "apiLog": { "requestLog": { "uri": "/test-api", "httpMethod": "GET", "controllerMethodArgs": "{\"body\":{},\"query\":{}}", "requestHeader": "{header에 있는 정보들....}" }, "responseLog": { "responseBody": "{}", "httpStatusCode": 200 }, "apiStartTime": "2023-01-04T09:01:41.058Z", "apiEndTime": "2023-01-04T09:01:41.062Z", "duration": { "time": 4, "unit": "MILLIS", "readableString": "0시간 0분 0.004초" } } }
- console-log
위의 형태로 ./logs/normal/{console-log 파일 이름}_yyyy-dd-mm.log 파일에 적힌다.{ "@timestamp": "2023-01-05T01:31:27.830Z", "level": "info", "message": "test message" }
- exception-log
위의 형태로 ./logs/exception/{exception-log 파일 이름}_yyyy-dd-mm.log 파일에 적힌다.```JSON { "@timestamp": "2023-01-04T09:01:41.067Z", "level": "error", "message": { "requestLog": { "uri": "/test-exception", "httpMethod": "GET", "controllerMethodArgs": "{\"body\":{},\"query\":{}}", "requestHeader": "{header에 있는 정보들....}" }, "responseLog": { "httpStatusCode": 500 }, "exceptionMessage": "error message", "exceptionStack": "Error stack" } } ```
1.1.0-beta1
7 months ago
1.0.4
1 year ago
1.0.3
1 year ago
1.0.2
1 year ago
1.0.1-beta3
1 year ago
1.0.1-beta1
1 year ago
1.0.1
1 year ago
1.0.0-beta2
1 year ago
1.0.0-beta1
1 year ago
1.0.0
1 year ago