Skip to content

Commit

Permalink
Merge pull request #7 from axiomhq/arne/fix-example
Browse files Browse the repository at this point in the history
Use waitUntil, log.flush() in middleware, function
  • Loading branch information
bahlo authored Jun 9, 2022
2 parents bca8dfb + ed2534b commit 1162737
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/logger/pages/_middleware.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextResponse } from 'next/server'
import { log } from 'next-axiom'

export async function middleware(_req, _ev) {
export async function middleware(_req, ev) {
log.info("Hello from middleware", { 'bar': 'baz' });
ev.waitUntil(log.flush())
return NextResponse.next()
}
1 change: 1 addition & 0 deletions examples/logger/pages/api/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { log } from 'next-axiom'

export default async function handler(req, res) {
log.info('Hello from function', { url: req.url });
await log.flush()
res.status(200).json({ name: 'John Doe' })
}
4 changes: 1 addition & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export const log = {
info: (message: string, args: any = {}) => _log('info', message, args),
warn: (message: string, args: any = {}) => _log('warn', message, args),
error: (message: string, args: any = {}) => _log('error', message, args),
flush: async () => {
await sendLogs();
},
flush: sendLogs,
};

async function sendLogs() {
Expand Down

1 comment on commit 1162737

@vercel
Copy link

@vercel vercel bot commented on 1162737 Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.