feat: api logger 추가

This commit is contained in:
Daseul Kim 2025-06-12 18:03:24 +09:00
parent 420c481b84
commit a573d7ffb1
4 changed files with 63 additions and 2 deletions

4
.gitignore vendored
View File

@ -46,3 +46,7 @@ next-env.d.ts
bun.lockb bun.lockb
pnpm-lock.yaml pnpm-lock.yaml
pnpm-workspace.yaml pnpm-workspace.yaml
# logs
logs/
*.log

View File

@ -6,7 +6,12 @@ const nextConfig: NextConfig = {
sassOptions: { sassOptions: {
includePaths: [path.join(__dirname, './src/styles')], includePaths: [path.join(__dirname, './src/styles')],
}, },
serverExternalPackages: ['@react-pdf/renderer'], serverExternalPackages: ['@react-pdf/renderer', 'pino'],
logging: {
fetches: {
fullUrl: true,
},
},
async rewrites() { async rewrites() {
return [ return [
{ {

View File

@ -29,6 +29,7 @@
"next": "15.2.4", "next": "15.2.4",
"nodemailer": "^7.0.3", "nodemailer": "^7.0.3",
"pdf-lib": "^1.17.1", "pdf-lib": "^1.17.1",
"pino": "^9.7.0",
"react": "^19.0.0", "react": "^19.0.0",
"react-dom": "^19.0.0", "react-dom": "^19.0.0",
"react-to-pdf": "^2.0.0", "react-to-pdf": "^2.0.0",

51
src/libs/logger.ts Normal file
View File

@ -0,0 +1,51 @@
import { NextRequest } from 'next/server'
import { join } from 'path'
import pino from 'pino'
/* 로그 파일 경로 */
const logFilePath = join('.', 'logs', 'onsite-survey.log')
/* 로거 생성 함수 */
const createLogger = (): pino.Logger => {
try {
return pino({
level: 'info',
timestamp: pino.stdTimeFunctions.isoTime,
transport: {
targets: [
{
target: 'pino/file',
options: {
destination: logFilePath,
mkdir: true,
sync: false,
},
},
],
},
})
} catch (error) {
console.error('Pino transport 생성 실패, 기본 로거 사용:', error)
return pino({
level: 'info',
timestamp: pino.stdTimeFunctions.isoTime,
})
}
}
/* 로거 인스턴스 */
export const logger = createLogger()
/* 로그 기록 함수 */
export const writeLog = async (request: NextRequest, responseStatus: number): Promise<void> => {
const logData = {
timestamp: new Date().toISOString(),
status: responseStatus,
method: request.method,
url: request.url,
// headers: Object.fromEntries(request.headers),
query: Object.fromEntries(new URL(request.url).searchParams),
body: request.body ? await request.clone().text() : undefined,
}
logger.info(logData, 'API Request')
}