Compare commits
No commits in common. "7597700e7b96c856b84a5946eb39014e6476bd44" and "298b563977fded2cc732ebf7a465ea4e47b1fa6c" have entirely different histories.
7597700e7b
...
298b563977
@ -1,8 +1,8 @@
|
|||||||
import getConfigs from '@/config/config.common'
|
import getConfigs from '@/config/config.common'
|
||||||
|
|
||||||
// 환경마다 달라져야 할 변수, 값들을 정의합니다. (여기는 production 환경에 맞는 값을 지정합니다.)
|
// 환경마다 달라져야 할 변수, 값들을 정의합니다. (여기는 production 환경에 맞는 값을 지정합니다.)
|
||||||
const baseUrl = 'https://hanasysfield.jp'
|
// const baseUrl = 'https://hanasysfield.jp'
|
||||||
// const baseUrl = 'http://172.16.56.55:3000'
|
const baseUrl = 'http://172.16.56.55:3000'
|
||||||
const mode = 'production'
|
const mode = 'production'
|
||||||
|
|
||||||
// 환경마다 달라져야 할 값들을 getConfig 함수에 전달합니다.
|
// 환경마다 달라져야 할 값들을 getConfig 함수에 전달합니다.
|
||||||
|
|||||||
@ -2,86 +2,43 @@ import { NextRequest } from 'next/server'
|
|||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import pino from 'pino'
|
import pino from 'pino'
|
||||||
|
|
||||||
/* 로그 데이터 인터페이스 */
|
/* 로그 파일 경로 */
|
||||||
interface LogData {
|
const logFilePath = join('.', 'logs', 'onsite-survey.log')
|
||||||
timestamp: string
|
|
||||||
status: number
|
|
||||||
method: string
|
|
||||||
url: string
|
|
||||||
// headers: { [k: string]: string }
|
|
||||||
query: { [k: string]: string }
|
|
||||||
body: string | undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 날짜별 로그 파일 경로 생성 함수 */
|
/* 로거 생성 함수 */
|
||||||
const getLogFilePath = (): string => {
|
const createLogger = (): pino.Logger => {
|
||||||
const today = new Date().toISOString().split('T')[0] // YYYY-MM-DD 형식
|
try {
|
||||||
return join(process.cwd(), 'logs', `onsite-survey-${today}.log`)
|
return pino({
|
||||||
}
|
level: 'info',
|
||||||
|
timestamp: pino.stdTimeFunctions.isoTime,
|
||||||
/* 날짜별 로거 생성 클래스 */
|
transport: {
|
||||||
class DailyLogger {
|
targets: [
|
||||||
private currentDate: string
|
{
|
||||||
private logger: pino.Logger
|
target: 'pino/file',
|
||||||
private destination: ReturnType<typeof pino.destination>
|
options: {
|
||||||
|
destination: logFilePath,
|
||||||
constructor() {
|
mkdir: true,
|
||||||
this.currentDate = new Date().toISOString().split('T')[0]
|
sync: false,
|
||||||
this.destination = pino.destination({
|
},
|
||||||
dest: getLogFilePath(),
|
},
|
||||||
mkdir: true,
|
],
|
||||||
sync: false,
|
|
||||||
})
|
|
||||||
this.logger = this.createLogger()
|
|
||||||
|
|
||||||
/* kill signal 핸들러 등록 */
|
|
||||||
process.on('SIGTERM', this.handleShutdown.bind(this))
|
|
||||||
process.on('SIGINT', this.handleShutdown.bind(this))
|
|
||||||
}
|
|
||||||
|
|
||||||
private async handleShutdown(): Promise<void> {
|
|
||||||
this.destination.flushSync()
|
|
||||||
this.destination.end()
|
|
||||||
}
|
|
||||||
|
|
||||||
private createLogger(): pino.Logger {
|
|
||||||
return pino(
|
|
||||||
{
|
|
||||||
level: 'info',
|
|
||||||
timestamp: pino.stdTimeFunctions.isoTime,
|
|
||||||
},
|
},
|
||||||
this.destination,
|
})
|
||||||
)
|
} catch (error) {
|
||||||
}
|
console.error('Pino transport 생성 실패, 기본 로거 사용:', error)
|
||||||
|
return pino({
|
||||||
public info(obj: any, msg?: string): void {
|
level: 'info',
|
||||||
const today = new Date().toISOString().split('T')[0]
|
timestamp: pino.stdTimeFunctions.isoTime,
|
||||||
|
})
|
||||||
if (today !== this.currentDate) {
|
|
||||||
/* 기존 destination 종료 */
|
|
||||||
this.destination.flushSync()
|
|
||||||
this.destination.end()
|
|
||||||
|
|
||||||
/* 새로운 destination 생성 */
|
|
||||||
this.destination = pino.destination({
|
|
||||||
dest: getLogFilePath(),
|
|
||||||
mkdir: true,
|
|
||||||
sync: false,
|
|
||||||
})
|
|
||||||
this.currentDate = today
|
|
||||||
this.logger = this.createLogger()
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.info(obj, msg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 로거 인스턴스 */
|
/* 로거 인스턴스 */
|
||||||
const dailyLogger = new DailyLogger()
|
export const logger = createLogger()
|
||||||
|
|
||||||
/* 로그 기록 함수 */
|
/* 로그 기록 함수 */
|
||||||
export const writeLog = async (request: NextRequest, responseStatus: number): Promise<void> => {
|
export const writeLog = async (request: NextRequest, responseStatus: number): Promise<void> => {
|
||||||
const logData: LogData = {
|
const logData = {
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
status: responseStatus,
|
status: responseStatus,
|
||||||
method: request.method,
|
method: request.method,
|
||||||
@ -90,5 +47,5 @@ export const writeLog = async (request: NextRequest, responseStatus: number): Pr
|
|||||||
query: Object.fromEntries(new URL(request.url).searchParams),
|
query: Object.fromEntries(new URL(request.url).searchParams),
|
||||||
body: request.body ? await request.clone().text() : undefined,
|
body: request.body ? await request.clone().text() : undefined,
|
||||||
}
|
}
|
||||||
dailyLogger.info(logData, 'API Request')
|
logger.info(logData, 'API Request')
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user