임동욱 임동욱 08-20
chore: 개발 중에는 콘텐츠 요청을 성공해도 찍는다
브라우저 네트워크 탭에는 백엔드 호출이 뜨지 않는다 — 화면은 Server Action을 부르고
호출은 서버에서 일어난다. 개발에서는 성공·실패를 가리지 않고 남기고, 운영에서는
실패만 남긴다.

Co-Authored-By: Claude Opus 5 
@61f004ae6f20f10abcd2e37c76651c926b6884fd
lib/data/repositories/content-repository.ts
--- lib/data/repositories/content-repository.ts
+++ lib/data/repositories/content-repository.ts
@@ -324,6 +324,47 @@
   return params;
 }
 
+/**
+ * 보낸 것을 서버 콘솔에 그대로 남긴다.
+ *
+ * 브라우저 네트워크 탭에는 이 요청이 **뜨지 않는다** — 화면은 Server Action을 부르고 백엔드
+ * 호출은 서버에서 일어난다(BFF). 그래서 개발 중에는 성공·실패를 가리지 않고 찍고, 운영에서는
+ * 실패만 남긴다. JSON은 Swagger의 Request body에 그대로 붙여 넣을 수 있는 모양이다.
+ */
+function logWrite(
+  method: string,
+  path: string,
+  body: unknown,
+  form: Record<string, string | number | undefined> | undefined,
+  failure: { code: number; message: string } | null
+): void {
+  if (failure === null && process.env.NODE_ENV === 'production') {
+    return;
+  }
+
+  const head = failure
+    ? `[contents] ${method} ${path} 실패 (code ${failure.code}: ${failure.message})`
+    : `[contents] ${method} ${path}`;
+
+  const lines = [
+    head,
+    body === undefined ? null : `body(JSON):\n${JSON.stringify(body, null, 2)}`,
+    form === undefined
+      ? null
+      : `form:\n${new URLSearchParams(
+          Object.entries(form).flatMap(([key, value]) =>
+            value === undefined ? [] : [[key, String(value)] as [string, string]]
+          )
+        ).toString()}`,
+  ].filter(Boolean);
+
+  if (failure) {
+    console.error(lines.join('\n'));
+  } else {
+    console.info(lines.join('\n'));
+  }
+}
+
 async function sendWrite(
   path: string,
   method: 'POST' | 'PUT' | 'DELETE',
@@ -342,24 +383,9 @@
     canHaveEmptyBody: true,
   });
 
+  logWrite(method, path, body, form, result.ok ? null : result);
+
   if (!result.ok) {
-    // 백엔드 로그를 볼 수 없어, 무엇을 보냈는지 여기서 그대로 남긴다. JSON 본문은 Swagger의
-    // Request body에 그대로 붙여 넣을 수 있는 모양으로 찍는다.
-    console.error(
-      [
-        `[contents] ${method} ${path} 실패 (code ${result.code}: ${result.message})`,
-        body === undefined ? null : `body(JSON):\n${JSON.stringify(body, null, 2)}`,
-        form === undefined
-          ? null
-          : `form:\n${new URLSearchParams(
-              Object.entries(form).flatMap(([key, value]) =>
-                value === undefined ? [] : [[key, String(value)] as [string, string]]
-              )
-            ).toString()}`,
-      ]
-        .filter(Boolean)
-        .join('\n')
-    );
     throw new BackendRequestError(result);
   }
 
Add a comment
List