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

'logging' example is not provided; no pattern provided #119

Closed
sgharms opened this issue Feb 1, 2022 · 11 comments
Closed

'logging' example is not provided; no pattern provided #119

sgharms opened this issue Feb 1, 2022 · 11 comments
Assignees

Comments

@sgharms
Copy link

sgharms commented Feb 1, 2022

Hello,

The https://nextjs.org/docs/middleware#examples page suggests that there are number of examples of various types in this repo

Authentication
Bot protection
Redirects and rewrites
Handling unsupported browsers
Feature flags and A/B tests
Server-side analytics
Advanced i18n routing requirements
Logging

all point to the same resource:

https://github.com/vercel/examples/tree/main/edge-functions

Keying in on Logging, searching the Readme for the string oggi yields no match. Similarly ack through the repo for /oggi/i and you will only find a mention in feature-flag-configcat/ example. Furthermore, this is logging from a client function not from a server function e.g. the middleware function.

Absent any directions, I ahem, duplicated this error vercel/next.js#13214

Further research at https://nextjs.org/docs/api-reference/edge-runtime#unsupported-apis, a leap whose connection was made my reading URL paths, suggests this is not possible. OK, I can live with that, but then it becomes all the more important that it be documented how to log server-side data in the middlewares.

I suspect most people are implementing a custom server to get 'fs' access, a practice that seems discouraged in the docs.

So is there either:

  1. An example of doing server-side logging in the Next Middleware (if not...)
  2. The expectation that that should be possible and, if so, how will that be possible if fs and other core modules are not available (e.g. process, util, os, buffer, et al.)
@oswaldoacauan
Copy link

oswaldoacauan commented Feb 18, 2022

I had a very frustrating evening yesterday trying to find a simple logging middleware example. The docs pointing nowhere was not very helpful also.

I see this as a basic feature for a framework that is to be production ready and same goes for Telemetry which there is no examples to be found.

@okbel
Copy link
Collaborator

okbel commented Feb 18, 2022

Hey! Thanks for bringing this up. We're already working on it. 👍

@okbel okbel self-assigned this Feb 18, 2022
@oswaldoacauan
Copy link

@okbel thanks for the update 🙏🏻 . Is there any estimated date for the logging, telemetry and instrumentation examples?

@leerob
Copy link
Member

leerob commented Feb 18, 2022

#156

@sgharms
Copy link
Author

sgharms commented Feb 22, 2022

I think @leerob has offered something formidable, and it is definitely an improvement that should be included; however, for simplicity’s sake and to establish a general pattern, I’d like to see something where the log file is written on the same machine as the next instance.

I’m wondering if we can squint and see the Vercel business model: if calls are processed on lambdas, there’s no local persistence and an approach like #156 makes sense :). I’d argue that for developing middleware and dev mode in general that models for local persistence be demonstrated (or, I suppose, clearly rejected in the documentation so that those considering next middleware see this limitation up front).

@leerob
Copy link
Member

leerob commented Feb 22, 2022

I’d like to see something where the log file is written on the same machine as the next instance.

This isn't a pattern we want to advocate for, whether you're running serverfull or serverless.

I’m wondering if we can squint and see the Vercel business model: if calls are processed on lambdas, there’s no local persistence and an approach like #156 makes sense.

Next.js works open-source, on whatever infra you provide, so it's not really about Vercel's serverless infra - more so that when we talk to companies building with Next.js, they aren't persisting logs to a local file, but rather ingesting them into some logging system 👍

@ValentinH
Copy link

Talking about ingesting them, we are facing issues with winston not being able to run in the Edge runtime. Is it a known issue? What logger do you recommend for logging in middlewares?

@sgharms
Copy link
Author

sgharms commented Feb 23, 2022

@ValentinH

We were unable to log (locally) in the middleware as fs is not available.

The PR suggests that fetch() is available as an alternative.

For us, this meant using a custom server (in docs) to get this functionality. It’s discouraged by vercel as one loses some optimizations, but I didn’t see any other way to get local logging.

good news: the server code is fairly easy and the docs are solid.

@ValentinH
Copy link

I've gone through the custom server path in the past but this time we are hosted on Vercel so we want to stay on the default setup. 😞

@sgharms
Copy link
Author

sgharms commented Feb 23, 2022

Seems like there is community desire for access to some/all of the node runtime in the middleware.

Having to fetch() POST just to get basic telemetry data seems unnecessarily complex.

@lfades
Copy link
Member

lfades commented Mar 26, 2022

Closing as per #164 (comment)

To provider more context, Middleware can be executed multiple times for the same path, and we don't have a reliable way to send logs at 1. the right time and 2. while avoiding sending it twice.

The logging and analytics example have been removed until we have a more appropriate solution for it.

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

No branches or pull requests

6 participants