Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support structured logging for Vercel 'edge' functions #51

Open
tchief opened this issue Jan 26, 2022 · 3 comments
Open

Support structured logging for Vercel 'edge' functions #51

tchief opened this issue Jan 26, 2022 · 3 comments

Comments

@tchief
Copy link

tchief commented Jan 26, 2022

Hi @chasers, thanks for the product!

Setup

Next.js project with middleware deployed to Vercel with pino-logflare.

Problem

Attempt to use logger either directly or via new child inside middleware

logger.info({ name: 'abc' }, 'In middleware');
...
const currentLogger = logger.child({ source: 'middleware' });
currentLogger.info({ name: 'abc' }, 'In middleware');

lead to edge function crash with message

"message": "[EvalError: Code generation from strings disallowed for this context]",
"source": "edge",
"type": "fatal"

Using console.log does not parse message into parsedLambdaMessage or similar property inside middleware.

Questions

  1. Can I setup logger in a way it supports structured logging inside edge function?
  2. Do you plan to include property similar to 'parsedLambdaMessage' for Vercel's edge functions?
@chasers
Copy link
Contributor

chasers commented Jan 26, 2022 via email

@chasers
Copy link
Contributor

chasers commented Jan 27, 2022

And yeah if you're looking for data specifically from the middleware this is the way to do it. I'm happy to help assist you in modifying the Cloudflare worker accordingly.

@kjetilhartveit
Copy link

kjetilhartveit commented Nov 19, 2023

If you're using the API then i reverse-engineered createWriteStream and wrote a somewhat dirty way to support it in edge runtimes. I also had to install the npm package logflare-transport-core to get LogflareHttpClient.

Hopefully in the future edge will be supported out-of-the-box but this seems to work for me for now and I haven't found a subsitute for pino.

Update:) it seems pino will log as in inside the browser when in the edge runtimes (because native node is not supported).
This is a bit annoying but it seems to work somewhat. I see the logged keys are different from the node version, e.g. keys defined in the base property in the pino configuration are missing (a bug?). Not quite sure what to do about that, the node and browser implementations seems completely different.
My implementation should prevent a compile error on the edge and support both runtimes.

import {
	addLogflareTransformDirectives,
	handlePreparePayload,
} from "pino-logflare/dist/utils";
import { type Options } from "pino-logflare/dist/httpStream";
import { LogflareHttpClient } from "logflare-transport-core";

const stream = (() => {
	const options = {
		apiKey: /* YOUR API KEY */,
		sourceToken:  /* YOUR SOURCE */,
	} satisfies Options;
	return {
		/**
		 * createWriteStream in pino-logflare re-written to work in Edge and ESM.
		 *
		 * The logic is simplified and copied from pino-logflare's repository.
		 * Note that the implementation below can become stale from the original repository.
		 * 
		 * Sources:
		 * - https://github.com/Logflare/pino-logflare/blob/master/src/httpStream.ts
		 * - https://github.com/Logflare/pino-logflare/blob/master/src/streams.ts
		 */
		write(msg: string, _?: string, callback?: (...args: any[]) => any) {
			const obj = JSON.parse(msg);
			const entry = handlePreparePayload(obj, options);
			const maybeWithTransforms = addLogflareTransformDirectives(
				entry,
				options,
			);
			const httpClient = new LogflareHttpClient(options);
			httpClient
				.addLogEvent(maybeWithTransforms)
				.then(() => {
					callback?.(null);
				})
				.catch(callback);
		},
	};
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants