link-stack/apps/leafcutter/app/api/upload/index.ts
Darren Clarke c1feaa4cb1 feat: Add centralized logging system with @link-stack/logger package
- Create new @link-stack/logger package wrapping Pino for structured logging
- Replace all console.log/error/warn statements across the monorepo
- Configure environment-aware logging (pretty-print in dev, JSON in prod)
- Add automatic redaction of sensitive fields (passwords, tokens, etc.)
- Remove dead commented-out logger file from bridge-worker
- Follow Pino's standard argument order (context object first, message second)
- Support log levels via LOG_LEVEL environment variable
- Export TypeScript types for better IDE support

This provides consistent, structured logging across all applications
and packages, making debugging easier and production logs more parseable.
2025-08-20 11:37:39 +02:00

60 lines
1.7 KiB
TypeScript

/* eslint-disable no-restricted-syntax */
import { NextRequest, NextResponse } from "next/server";
import { Client } from "@opensearch-project/opensearch";
import { v4 as uuid } from "uuid";
import taxonomy from "app/_config/taxonomy.json";
import unRegions from "app/_config/unRegions.json";
import { createLogger } from "@link-stack/logger";
const logger = createLogger('leafcutter-index');
export const POST = async (req: NextRequest) => {
const { tickets } = await req.json();
const authorization = req.headers.get("authorization");
const baseURL = `https://${process.env.OPENSEARCH_URL}`;
const client = new Client({
node: baseURL,
ssl: {
rejectUnauthorized: false,
},
headers: {
authorization,
},
});
const succeeded = [];
const failed = [];
for await (const ticket of tickets) {
const { id } = ticket;
try {
const country = ticket.country[0] ?? "none";
// @ts-expect-error
const translatedCountry = taxonomy.country[country]?.display ?? "none";
const countryDetails: any = unRegions.find(
(c) => c.name === translatedCountry,
);
const augmentedTicket = {
...ticket,
region:
countryDetails["sub-region"]?.toLowerCase().replace(" ", "-") ?? null,
continent:
countryDetails.region?.toLowerCase().replace(" ", "-") ?? null,
};
const out = await client.create({
id: uuid(),
index: "sample_tagged_tickets",
refresh: true,
body: augmentedTicket,
});
succeeded.push(id);
} catch (e) {
logger.error(e);
failed.push(id);
}
}
const results = { succeeded, failed };
return NextResponse.json(results);
};