fastify / one-line-logger

Helps you format fastify's log into a nice one line message
MIT License
32 stars 7 forks source link

Cannot use @fastify/one-line-logger on vercel #41

Closed tianheg closed 4 months ago

tianheg commented 4 months ago

Prerequisites

Fastify version

4.26.2

Plugin version

1.3.0

Node.js version

20.x

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Vercel Serverless

Description

I use @fastify/one-line-logger format logs, when deploying fastify as Vercel's serverless function, get below error:

Error: unable to determine transport target for "@fastify/one-line-logger"
at fixTarget (/var/task/node_modules/.pnpm/pino@8.20.0/node_modules/pino/lib/transport.js:144:13)
at transport (/var/task/node_modules/.pnpm/pino@8.20.0/node_modules/pino/lib/transport.js:114:22)
at normalizeArgs (/var/task/node_modules/.pnpm/pino@8.20.0/node_modules/pino/lib/tools.js:316:16)
at pino (/var/task/node_modules/.pnpm/pino@8.20.0/node_modules/pino/pino.js:90:28)
at createPinoLogger (/var/task/node_modules/.pnpm/fastify@4.26.2/node_modules/fastify/lib/logger.js:42:14)
at createLogger (/var/task/node_modules/.pnpm/fastify@4.26.2/node_modules/fastify/lib/logger.js:101:18)
at fastify (/var/task/node_modules/.pnpm/fastify@4.26.2/node_modules/fastify/fastify.js:135:33)
at file:///var/task/server.js:13:13
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.
INIT_REPORT Init Duration: 602.99 ms    Phase: invoke   Status: error   Error Type: Runtime.ExitError
Unknown application error occurred

Steps to Reproduce

  1. Create a simple fastify api example
  2. Change the logger to @fastify/one-line-logger

Expected Behavior

The fastify api will deploy successfully on vercel

mcollina commented 4 months ago

I don't think the pino transport system would work on vercel. You should use it as a stream: https://github.com/pinojs/pino-pretty?tab=readme-ov-file#usage-as-a-stream.

tianheg commented 4 months ago

Use pino-pretty as a stream worked, here is the code:

import Fastify from 'fastify';
import pino from 'pino';
import pretty from 'pino-pretty';
const stream = pretty({
  translateTime: 'SYS:HH:MM:ss Z',
  messageFormat: '{msg} {req.method} {req.url}',
  include: 'time,pid,level',
  hideObject: true,
  colorize: false,
});
const logger = pino({ level: 'info' }, stream);

const app = Fastify({ logger });

Thanks for your reply