Examples
Raw OleqLog recipes for NestJS, Next.js, and Express
These examples use OleqLog directly. For official middleware, modules, and hooks, see Adapters.
| Stack | Where to init | When to flush |
|---|---|---|
| NestJS | Injectable provider or module bootstrap | onModuleDestroy / shutdown hook |
| Next.js | Route Handler or server action | End of request / after() |
| Express | App bootstrap | SIGTERM handler |
Never expose OLEQLOG_API_KEY in browser code. Log from your server (API routes, workers, Nest providers).
NestJS
Module + provider
import { Global, Inject, Module, OnModuleDestroy } from "@nestjs/common";
import OleqLog from "@oleq-ai/logs";
export const OLEQ_LOG = Symbol("OLEQ_LOG");
@Global()
@Module({
providers: [
{
provide: OLEQ_LOG,
useFactory: () =>
new OleqLog({
appName: "api",
environment: process.env.NODE_ENV ?? "development",
}),
},
],
exports: [OLEQ_LOG],
})
export class OleqLogModule implements OnModuleDestroy {
constructor(@Inject(OLEQ_LOG) private readonly log: OleqLog) {}
async onModuleDestroy() {
await this.log.flush();
await this.log.close();
}
}Prefer the built-in module: NestJS adapter.
Next.js
App Router Route Handler — init once per cold start:
import OleqLog from "@oleq-ai/logs";
const log = new OleqLog({ appName: "web" });
export async function GET() {
await log.info({ message: "health check" });
await log.flush();
return Response.json({ ok: true });
}For lazy init and client logging, see Next.js adapters.
Express
import express from "express";
import OleqLog from "@oleq-ai/logs";
const app = express();
const log = new OleqLog({ appName: "api" });
app.use((req, res, next) => {
const start = Date.now();
res.on("finish", () => {
void log.info({
message: `${req.method} ${req.originalUrl}`,
kind: "res",
body: { status: res.statusCode, ms: Date.now() - start },
});
});
next();
});
process.on("SIGTERM", async () => {
await log.flush();
await log.close();
process.exit(0);
});Or use Express adapter middleware instead.
See also: Pino transport