Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Support automatic log forwarding in next's app dir #126

Closed
kdembowski opened this issue May 22, 2023 · 7 comments
Closed

Support automatic log forwarding in next's app dir #126

kdembowski opened this issue May 22, 2023 · 7 comments

Comments

@kdembowski
Copy link

Is your feature request related to a problem? Please describe.

Automatic (possibly manual as well) log forwarding does not seem to work in /app dir (considered stable since next 13.4).

Feature Description

Support for automatic log forwarding inside /app directory.

Describe Alternatives

N/A

Additional context

I was struggling to set up logging for a Next application using its new app router. It appears that automatic log forwarding simply does not work in React Server Components. Following newrelic's documentation I expected it to automatically forward logs when using a supported logger - I've tried both Winston and Pino. I've also tried manual logging with enricher plugins but as I understand they are not needed/deprecated(?)
To verify the issue is not in my setup I've created 2 basic Next.js repos - one using old /pages and one using new app router. For the one with /pages logs were forwarded completely fine when logging from getServerSideProps. However for /app when logging from any server component it is not forwarded.

Here are the repos:
/pages: https://github.com/kdembowski/newrelic-pages-repro/blob/main/pages/index.tsx
/app: https://github.com/kdembowski/newrelic-app-router-repro/blob/main/src/app/page.tsx

Priority

Must Have

@workato-integration
Copy link

@VincentCtr
Copy link

Did you find any solution to send your logs ?

@andrewmclagan
Copy link

Not very encouraging:

added this to Triage Needed: Unprioritized Features

Can someone from NewRelic please give more information or a timeline? Thanks.

@kdembowski
Copy link
Author

Did you find any solution to send your logs ?

We switched to OpenTelemetry as it's integrated with Next.js

@andrewmclagan
Copy link

We also did this, although the OTEL data is nowhere near as rich as the full NewRelic integration. Not super useful TBH.

@bizob2828
Copy link
Member

bizob2828 commented Mar 11, 2024

@kdembowski I took a look at this and it's because Next.js is using webpack to bundle dependencies. We have an experimental plugin that can be used to fix issues that webpack causes but it appears to conflict with some of the other packages that Next.js relies on. To get winston working or really any library to instrument. Add this to next.config.js

const nextConfig = {
  experimental: {
    appDir: true,
  },
  webpack: (config, options) => {
    if (config.target.includes('node')) {
      config.externals.push('winston')
    }
    return config
  }
};

By doing this I see your log lines in my app

screenshot 2024-03-11 at 4 44 58 PM

@bizob2828
Copy link
Member

bizob2828 commented Mar 11, 2024

I'm going to close this. Please let me know if this does not work for you and we can reopen. I also introduced a PR to make this easier by automatically externalizing all libraries the Node.js agent instruments

@bizob2828 bizob2828 moved this to Done: Issues recently completed in Node.js Engineering Board Jul 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

4 participants