Skip to content

[Feature Request] Can pass a module NOT STRING to pino transport target? #2272

@wxqqh

Description

@wxqqh

I like pino just it's light and powerfull !

[Feature Request] pass a module NOT STRING to pino transport target?

import  {dateRotateTransport} from "@/lib/utils/transport/date-rotate-transport"

pino({
   transport: {
        target: dateRotateTransport,
        options: { 
             ... // opts
        } satisfies Parameters<typeof dateRotateTransport>[0]
   }
})

There are two benefits to doing this

  1. Avoid requiring within Pino. For example, during the construction and startup of Nextjs, its internal packaging tool (SWC) may not be able to correctly parse and handle the relative path of Pino Transport. When loading Transport, Pino will attempt to require or import the specified target file in a new worker thread, which often conflicts with the packaging and module parsing mechanism of Nextjs.
  2. Type derivation can be improved within Pino. Make options more easily compatible with typescript

Current

Just I meet a problem:

 ⨯ Error: unable to determine transport target for "./transport/date-rotate-transport"
    at getLogger (src/lib/utils/logger.ts:58:14)
    at __TURBOPACK__module__evaluation__ (src/lib/utils/logger.ts:67:23)
    at __TURBOPACK__module__evaluation__ (src/app/[locale]/layout.tsx:15:1)
    at __TURBOPACK__module__evaluation__ (.next/server/chunks/ssr/[root-of-the-server]__daec2034._.js:7:47)
    at Object.<anonymous> (.next/server/app/[locale]/page.js:18:3)
  56 | /** Get a new logger */
  57 | export function getLogger(name = "api", level: Level = "info"): Logger {
> 58 |   return pino({
     |              ^
  59 |     transport: loggerConfig,
  60 |     name: `${PREFIX}:${name}`,
  61 |     mixin: contextMixin, {
  page: '/en/about'
}
 GET /en 500 in 3872ms
 GET /about 500 in 1715ms
 GET /_next/internal/helpers.ts 404 in 657ms

ENV:

Nextjs 15.5.2
Nodejs 24.7.0
Typescript 5.8.3

and i add this code

export const loggerConfig: LoggerOptions["transport"] = {
  targets: [
      target: "./transport/date-rotate-transport",
      options: {
        filename: `${ENV.LOG_DIR}/${PREFIX}-${ENV.LOGGER_LEVEL}-%DATE%`,
        frequency: "daily",
        date_format: "YYYY-MM-DD",
        size: "100M",
        max_logs: "10",
        extension: ".log",
        audit_file: `${ENV.LOG_DIR}/audit.json`,
      },
    }]
}

export function getLogger(name = "api", level: Level = "info"): Logger {
  return pino({
    transport: loggerConfig,
    name: `${PREFIX}:${name}`,
    mixin: contextMixin,
    level,
  })
}

files tree

➜  utils git:(main) tree .
.
├── cls-session.ts
├── database.ts
├── env.ts
├── fluentd.ts
├── logger.ts
├── partial.ts
└── transport
    ├── date-rotate-transport.ts
    ├── fluentd-transport.ts
    └── pino-log-data.ts

2 directories, 9 files

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions