I have created a Next.js project with typescript using create-next-app. For logging of this particular project I have decided to use Pino logging library as it is recommended by Next.js itself.

When I am using Pino without its transport functionality it runs perfectly fine. Below is pino definition code which run perfectly:-

import pino from 'pino';

const logger = pino({
    level: process.env.NEXT_PUBLIC_LOG_LEVEL,
    formatters: {
        level: (label) => {
            return { level: label.toUpperCase() };
        },
        bindings: (bindings) => {
            return { host: bindings.hostname };
        },
    },
});

export default logger;

But when I am using Pino transport, the code is mentioned below (I have installed pino-pretty as a dev dependency):-

import pino from 'pino';

const logger1 = pino({
    transport: {
        target: 'pino-pretty',
    },
});

logger1.info('hi');
export default logger1;

I get the below mentioned error

- error uncaughtException: Error: Cannot find module 'C:\starttwo-productionlot-frontend\.next\server\app\home\lib\worker.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:164:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  digest: undefined
}
- error node_modules\thread-stream\index.js (195:31) @ Worker.onWorkerExit
- error uncaughtException: Error: the worker thread exited
    at Worker.onWorkerExit (webpack-internal:///(sc_server)/./node_modules/thread-stream/index.js:163:34)
    at Worker.emit (node:events:513:28)
    at [kOnExit] (node:internal/worker:289:10)
    at Worker.<computed>.onexit (node:internal/worker:209:20)
    at Worker.callbackTrampoline (node:internal/async_hooks:130:17) {
  digest: undefined
}

I get the same error with this code as well (Here i have a question, do we need to install pino/file using npm?):-

import pino from 'pino';
const transport = pino.transport({
  target: 'pino/file',
  options: { destination: '/path/to/file' }
})
pino(transport)

I am using the loggers on server side, that is only in next.js page.tsx file who does not have "use client" directive Can you please me to solve this error

I tried implementing the sample code provided in pino official docs, but i get the same above mentioned error, the code was as follows:-

In my-transport.tsx:-

import { createWriteStream } from 'fs'

export default () => {
  return createWriteStream('.../app/app.log)')// /app/app.log -- location of log file 
}

In logger.tsx:-

const pino = require('pino')
const transport = pino.transport({
  target: '/absolute/path/to/my-transport.mjs'
})
export default pino(transport)

Is there any restriction on using pino.transport() in next.js. Can we use it in client as well as server side? How can i use transport functionality in pino?

1

There are 1 best solutions below

0
On

You have .../path/ instead of ../path/ (one . too many)

But this will not resolve the issue.

Solution

You are on a webserver. You can not navigate further back than the webserver's root directory with Next.js unless you configure a custom server (that's possible), more infos here:

Configuring custom server

GitHub discussion: custom root approach mentioned


⚠️ Advice

You should .gitignore the .next directory, so whatever you try, you should put it to another directory anyway.


If the file is inside the webservers root, then:

Recommendation:

/*
 * __dirname     current directory
 * process.cwd() directory where process/server was started
 */
import path from "path";

// concatenation of paths (e.g. to avoid `/` issues
path.join(__dirname, "where/you/want/to/go");

// or to get the absolute path
path.resolve(__dirname, "where/you/want/to/go");