sainsburys-tech / next-logger

JSON logging patcher for Next.js
MIT License
144 stars 14 forks source link

When using a custom logger, next logs aren't being logged using my logger (winston) #24

Closed eloisetaylor5693 closed 6 months ago

eloisetaylor5693 commented 7 months ago

It seems the console logs look like they did before I made the change. Do you have advice on how to make this work?

    "next": "^14.0.4",
    "next-logger": "^3.0.2",
    "winston": "^3.10.0"

     node:  20.10.0

I did the following:

// next-logger.config.js

const { createLogger, format, transports } = require('winston');

// eslint-disable-next-line no-underscore-dangle
const _logger = createLogger({
  level: 'debug',
  defaultMeta: { type: 'nextjs' },
  transports: [
    new transports.Console({
      handleExceptions: true,
      format: format.combine(format.colorize(), format.simple()),
    }),
  ],
});

const logger = () => ({
  error: _logger.error,
  warn: _logger.warn,
  trace: _logger.trace,
  info: _logger.info,
});

module.exports = {
  logger,
};

Actual Log output:

[0] Warning: data for page "/degrees" is 284 kB which exceeds the threshold of 128 kB, this amount of data can reduce performance.
[0] See more info here: https://nextjs.org/docs/messages/large-page-data
[0]  GET /degrees 200 in 2021ms
[0]  GET /_next/static/webpack/78ea0d5b1a7e3004.webpack.hot-update.json 404 in 406ms
[0] Warning: data for page "/" is 280 kB which exceeds the threshold of 128 kB, this amount of data can reduce performance.
[0] See more info here: https://nextjs.org/docs/messages/large-page-data
atkinchris commented 6 months ago

It looks like you're using next@14, which we're currently trying to add support for (#22).

Please can you try using the latest beta version of next-logger, and let me know if it works for you?

npm i next-logger@4.0.0-beta.1
eloisetaylor5693 commented 6 months ago

Using the beta version I get this error:

 % yarn start
yarn run v1.22.19
$ NODE_OPTIONS='-r next-logger' NODE_ENV=production yarn server
error: uncaughtException: logger.child is not a function
TypeError: logger.child is not a function
    at getPinoMethod (..../node_modules/next-logger/lib/patches/next.js:6:30)

Is my next-logger.config.js setup correctly?

atkinchris commented 6 months ago

The exported logger is expected to have a .child method - which both pino and winston do. In your config, you're exporting a plain object with method properties - rather than a winston instance.

If you export the winston instance itself, it should work.

// next-logger.config.js

const { createLogger, format, transports } = require('winston');

const logger = createLogger({
  level: 'debug',
  defaultMeta: { type: 'nextjs' },
  transports: [
    new transports.Console({
      handleExceptions: true,
      format: format.combine(format.colorize(), format.simple()),
    }),
  ],
});

module.exports = {
  logger,
};
eloisetaylor5693 commented 6 months ago

After fixing the config, this works. Thank you

I suggest adding this to the docs, along with perhaps some examples for other popular loggers

When is the version that supports next 14 likely to be released?

atkinchris commented 6 months ago

Glad it works. I'll try to get it released today, along with a README update as suggested.

eloisetaylor5693 commented 6 months ago

Awesome! Thanks again

atkinchris commented 6 months ago

Version 4.0.0 has been published, bringing Next 14 support!