Skip to content

Commit

Permalink
attach request info along with printing edge report
Browse files Browse the repository at this point in the history
  • Loading branch information
schehata committed Jul 26, 2022
1 parent 624db89 commit 7190ae3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
30 changes: 19 additions & 11 deletions src/logger.ts
Expand Up @@ -12,11 +12,27 @@ interface LogEvent {
request?: any;
}

export interface RequestReport {
startTime: number;
statusCode?: number;
ip?: string;
region?: string;
path: string;
host: string;
method: string;
scheme: string;
userAgent?: string | null;
}

export class Logger {
public logEvents: any[] = [];
throttledSendLogs = throttle(this.sendLogs, 1000);

constructor(private args: any = {}, private req: NextRequest | null = null, private waitForFlush: Boolean = false) {}
constructor(
private args: any = {},
private req: RequestReport | null = null,
private waitForFlush: Boolean = false
) {}

debug(message: string, args: any = {}) {
this._log('debug', message, { ...this.args, ...args });
Expand All @@ -35,7 +51,7 @@ export class Logger {
return new Logger({ ...this.args, ...args }, this.req, this.waitForFlush);
}

withRequest(req: NextRequest) {
withRequest(req: RequestReport) {
return new Logger({ ...this.args }, req, this.waitForFlush);
}

Expand All @@ -46,15 +62,7 @@ export class Logger {
}

if (this.req != null) {
logEvent['request'] = {
ip: this.req.ip,
region: this.req.geo?.region,
host: this.req.nextUrl.host,
method: this.req.method,
path: this.req.nextUrl.pathname,
scheme: this.req.nextUrl.protocol.replace(':', ''),
userAgent: this.req.headers.get('user-agent'),
};
logEvent['request'] = this.req;
}

this.logEvents.push(logEvent);
Expand Down
16 changes: 2 additions & 14 deletions src/withAxiom.ts
Expand Up @@ -3,25 +3,13 @@
import { NextConfig, NextApiHandler, NextApiResponse, NextApiRequest } from 'next';
import { NextFetchEvent, NextMiddleware, NextRequest } from 'next/server';
import { NextMiddlewareResult } from 'next/dist/server/web/types';
import { Logger } from './logger';
import { Logger, RequestReport } from './logger';
import { proxyPath, EndpointType, getIngestURL } from './shared';

declare global {
var EdgeRuntime: string;
}

interface RequestReport {
startTime: number;
statusCode?: number;
ip?: string;
region?: string;
path: string;
host: string;
method: string;
scheme: string;
userAgent?: string | null;
}

function withAxiomNextConfig(nextConfig: NextConfig): NextConfig {
return {
...nextConfig,
Expand Down Expand Up @@ -148,7 +136,7 @@ function withAxiomNextEdgeFunction(handler: NextMiddleware): NextMiddleware {
userAgent: req.headers.get('user-agent'),
};

const logger = new Logger({}, req, true);
const logger = new Logger({}, report, true);
const axiomRequest = req as AxiomRequest;
axiomRequest.log = logger;

Expand Down

0 comments on commit 7190ae3

Please sign in to comment.