refactor(logging): centralize console output

This commit is contained in:
2026-06-18 13:34:19 +08:00
parent f600e11d55
commit 812a3e41b9
24 changed files with 261 additions and 166 deletions
@@ -19,6 +19,9 @@ import { SUBSCRIPTION_PLANS } from "@/data/constants/subscription-plans";
import type { SubscriptionPlanId } from "@/data/constants/subscription-plans";
import { getPlanPriceId } from "@/lib/stripe/stripe-products";
import { getStripeClient } from "@/lib/stripe/stripe-client";
import { Logger } from "@/utils";
const log = new Logger("AppApiPaymentCreateCheckoutSessionRoute");
interface CreateCheckoutRequestBody {
planId: SubscriptionPlanId;
@@ -86,7 +89,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ url: session.url });
} catch (e) {
const err = e as { message?: string; type?: string; code?: string };
console.error("[api/create-checkout-session] Stripe error", {
log.error("[api/create-checkout-session] Stripe error", {
message: err.message,
type: err.type,
code: err.code,
+4 -1
View File
@@ -14,6 +14,9 @@
import { NextRequest, NextResponse } from "next/server";
import { getStripeClient } from "@/lib/stripe/stripe-client";
import { Logger } from "@/utils";
const log = new Logger("AppApiPaymentCustomerPortalRoute");
interface CreatePortalRequestBody {
customerId: string | null;
@@ -56,7 +59,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ url: session.url });
} catch (e) {
const err = e as { message?: string; type?: string; code?: string };
console.error("[api/customer-portal] Stripe error", {
log.error("[api/customer-portal] Stripe error", {
customerId: body.customerId,
message: err.message,
type: err.type,
+9 -6
View File
@@ -27,11 +27,14 @@ import type Stripe from "stripe";
import { dispatchStripeEvent } from "@/lib/stripe/stripe-events";
import { getStripeClient } from "@/lib/stripe/stripe-client";
import { Logger } from "@/utils";
const log = new Logger("AppApiPaymentWebhookRoute");
export async function POST(req: NextRequest) {
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
if (!webhookSecret) {
console.error("[api/webhook] STRIPE_WEBHOOK_SECRET is not set");
log.error("[api/webhook] STRIPE_WEBHOOK_SECRET is not set");
return NextResponse.json(
{ error: "Webhook secret not configured" },
{ status: 500 },
@@ -42,7 +45,7 @@ export async function POST(req: NextRequest) {
const rawBody = await req.text();
const sigHeader = req.headers.get("stripe-signature");
if (!sigHeader) {
console.error("[api/webhook] Missing stripe-signature header");
log.error("[api/webhook] Missing stripe-signature header");
return NextResponse.json(
{ error: "Missing stripe-signature header" },
{ status: 400 },
@@ -56,7 +59,7 @@ export async function POST(req: NextRequest) {
event = stripe.webhooks.constructEvent(rawBody, sigHeader, webhookSecret);
} catch (e) {
const err = e as Error;
console.error("[api/webhook] Signature verification failed", {
log.error("[api/webhook] Signature verification failed", {
message: err.message,
});
return NextResponse.json(
@@ -66,7 +69,7 @@ export async function POST(req: NextRequest) {
}
// 分发到 handler
console.log("[api/webhook] Received event", {
log.debug("[api/webhook] Received event", {
eventId: event.id,
type: event.type,
created: new Date(event.created * 1000).toISOString(),
@@ -75,11 +78,11 @@ export async function POST(req: NextRequest) {
try {
const handled = await dispatchStripeEvent(event);
if (!handled) {
console.log(`[api/webhook] Ignored event type: ${event.type}`);
log.debug(`[api/webhook] Ignored event type: ${event.type}`);
}
} catch (e) {
const err = e as Error;
console.error("[api/webhook] Handler error", {
log.error("[api/webhook] Handler error", {
eventId: event.id,
type: event.type,
message: err.message,