Use node-bunyan on server side and browser-bunyan on client side in Next.js project
npm install --save bunyan browser-bunyan next-bunyan
or
yarn add bunyan browser-bunyan next-bunyan
Create a next.config.js
in your project
// next.config.js
const withBunyan = require("next-bunyan");
module.exports = withBunyan();
Create a logger file utils/logger/JsonLogger.js
import bunyan from "bunyan";
export default bunyan.createLogger({
name: "foo"
// other bunyan config
});
Then you could use it on another file, for example
import JsonLogger from "utils/logger/JsonLogger.js";
JsonLogger.info("foo bar");
Optionally you can add your custom Next.js configuration as parameter
// next.config.js
const withBunyan = require("next-bunyan");
module.exports = withBunyan({
webpack(config, options) {
return config;
}
});