Skip to content

Commit

Permalink
logger: provide new flag to enable Vercel log drain for API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dasfmi committed Jul 30, 2024
1 parent 59decf1 commit c287acf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ export default function ErrorPage({
}
```
## Improve API routes logging performance (Vercel)
If you have Vercel's integration enabled and don't mind the log drain cost, you
can make use of the log drain feature to send logs from API routes to Axiom.
This would tell next-axiom to use `console.log()` to send logs to Vercel's log drain instead of sending the logs directly to Axiom and waiting for the logger's http flush request to complete.
To enable this feature, set the `AXIOM_ENABLE_VERCEL_LOGDRAIN` environment variable to `true`.
## Upgrade to the App Router
next-axiom switched to support the App Router starting with version 1.0. If you are upgrading a Pages Router app with next-axiom v0.x to the App Router, you will need to make the following changes:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "next-axiom",
"description": "Send WebVitals from your Next.js project to Axiom.",
"version": "1.3.0",
"version": "1.4.0",
"author": "Axiom, Inc.",
"license": "MIT",
"contributors": [
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare global {
export const Version = require('../package.json').version;
// detect if Vercel integration & logdrain is enabled
export const isVercelIntegration = process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT || process.env.AXIOM_INGEST_ENDPOINT;
export const enableMakeUseOfVercelLogdrain =
process.env.NEXT_PUBLIC_AXIOM_ENABLE_VERCEL_LOGDRAIN || process.env.AXIOM_ENABLE_VERCEL_LOGDRAIN;
// detect if app is running on the Vercel platform
export const isVercel = process.env.NEXT_PUBLIC_VERCEL || process.env.VERCEL;
export const isNetlify = process.env.NETLIFY == 'true';
Expand Down
10 changes: 7 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest } from 'next/server';
import { config, isBrowser, isVercelIntegration, Version } from './config';
import { config, isBrowser, isVercelIntegration, enableMakeUseOfVercelLogdrain, Version } from './config';
import { NetlifyInfo } from './platform/netlify';
import { isNoPrettyPrint, throttle } from './shared';

Expand Down Expand Up @@ -211,8 +211,12 @@ export class Logger {
// if vercel integration is enabled, we can utilize the log drain
// to send logs to Axiom without HTTP.
// This saves resources and time on lambda and edge functions
if (isVercelIntegration && (this.config.source === 'edge-log' || this.config.source === 'lambda-log')) {
this.logEvents.forEach((ev) => console.log(JSON.stringify(ev)));
if (
isVercelIntegration &&
enableMakeUseOfVercelLogdrain &&
(this.config.source === 'edge-log' || this.config.source === 'lambda-log')
) {
this.logEvents.forEach((ev) => console.log(ev));
this.logEvents = [];
return;
}
Expand Down

0 comments on commit c287acf

Please sign in to comment.