CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
web.md270 linesDownload Raw Back to docs
1# Web Frameworks2 3Since HTTP logging is a primary use case, Pino has first-class support for the Node.js4web framework ecosystem.5 6- [Web Frameworks](#web-frameworks)7  - [Pino with Fastify](#pino-with-fastify)8  - [Pino with Express](#pino-with-express)9  - [Pino with Hapi](#pino-with-hapi)10  - [Pino with Restify](#pino-with-restify)11  - [Pino with Koa](#pino-with-koa)12  - [Pino with Node core `http`](#pino-with-node-core-http)13  - [Pino with Nest](#pino-with-nest)14  - [Pino with H3](#pino-with-h3)15 16<a id="fastify"></a>17## Pino with Fastify18 19The Fastify web framework comes bundled with Pino by default, simply set Fastify's20`logger` option to `true` and use `request.log` or `reply.log` for log messages that correspond21to each request:22 23```js24const fastify = require('fastify')({25  logger: true26})27 28fastify.get('/', async (request, reply) => {29  request.log.info('something')30  return { hello: 'world' }31})32 33fastify.listen({ port: 3000 }, (err) => {34  if (err) {35    fastify.log.error(err)36    process.exit(1)37  }38})39```40 41The `logger` option can also be set to an object, which will be passed through directly42as the [`pino` options object](/docs/api.md#options-object).43 44See the [fastify documentation](https://www.fastify.io/docs/latest/Reference/Logging/) for more information.45 46<a id="express"></a>47## Pino with Express48 49```sh50npm install pino-http51```52 53```js54const app = require('express')()55const pino = require('pino-http')()56 57app.use(pino)58 59app.get('/', function (req, res) {60  req.log.info('something')61  res.send('hello world')62})63 64app.listen(3000)65```66 67See the [pino-http README](https://npm.im/pino-http) for more info.68 69<a id="hapi"></a>70## Pino with Hapi71 72```sh73npm install hapi-pino74```75 76```js77'use strict'78 79const Hapi = require('@hapi/hapi')80const Pino = require('hapi-pino');81 82async function start () {83  // Create a server with a host and port84  const server = Hapi.server({85    host: 'localhost',86    port: 300087  })88 89  // Add the route90  server.route({91    method: 'GET',92    path: '/',93    handler: async function (request, h) {94      // request.log is HAPI's standard way of logging95      request.log(['a', 'b'], 'Request into hello world')96 97      // a pino instance can also be used, which will be faster98      request.logger.info('In handler %s', request.path)99 100      return 'hello world'101    }102  })103 104  await server.register(Pino)105 106  // also as a decorated API107  server.logger.info('another way for accessing it')108 109  // and through Hapi standard logging system110  server.log(['subsystem'], 'third way for accessing it')111 112  await server.start()113 114  return server115}116 117start().catch((err) => {118  console.log(err)119  process.exit(1)120})121```122 123See the [hapi-pino README](https://npm.im/hapi-pino) for more info.124 125<a id="restify"></a>126## Pino with Restify127 128```sh129npm install restify-pino-logger130```131 132```js133const server = require('restify').createServer({name: 'server'})134const pino = require('restify-pino-logger')()135 136server.use(pino)137 138server.get('/', function (req, res) {139  req.log.info('something')140  res.send('hello world')141})142 143server.listen(3000)144```145 146See the [restify-pino-logger README](https://npm.im/restify-pino-logger) for more info.147 148<a id="koa"></a>149## Pino with Koa150 151```sh152npm install koa-pino-logger153```154 155```js156const Koa = require('koa')157const app = new Koa()158const pino = require('koa-pino-logger')()159 160app.use(pino)161 162app.use((ctx) => {163  ctx.log.info('something else')164  ctx.body = 'hello world'165})166 167app.listen(3000)168```169 170See the [koa-pino-logger README](https://github.com/pinojs/koa-pino-logger) for more info.171 172<a id="http"></a>173## Pino with Node core `http`174 175```sh176npm install pino-http177```178 179```js180const http = require('http')181const server = http.createServer(handle)182const logger = require('pino-http')()183 184function handle (req, res) {185  logger(req, res)186  req.log.info('something else')187  res.end('hello world')188}189 190server.listen(3000)191```192 193See the [pino-http README](https://npm.im/pino-http) for more info.194 195 196<a id="nest"></a>197## Pino with Nest198 199```sh200npm install nestjs-pino201```202 203```ts204import { NestFactory } from '@nestjs/core'205import { Controller, Get, Module } from '@nestjs/common'206import { LoggerModule, Logger } from 'nestjs-pino'207 208@Controller()209export class AppController {210  constructor(private readonly logger: Logger) {}211 212  @Get()213  getHello() {214    this.logger.log('something')215    return `Hello world`216  }217}218 219@Module({220  controllers: [AppController],221  imports: [LoggerModule.forRoot()]222})223class MyModule {}224 225async function bootstrap() {226  const app = await NestFactory.create(MyModule)227  await app.listen(3000)228}229bootstrap()230```231 232See the [nestjs-pino README](https://npm.im/nestjs-pino) for more info.233 234 235<a id="h3"></a>236## Pino with H3237 238```sh239npm install pino-http h3240```241 242Save as `server.mjs`:243 244```js245import { createApp, createRouter, eventHandler, fromNodeMiddleware } from "h3";246import pino from 'pino-http'247 248export const app = createApp();249 250const router = createRouter();251app.use(router);252app.use(fromNodeMiddleware(pino()))253 254app.use(eventHandler((event) => {255  event.node.req.log.info('something')256  return 'hello world'257}))258 259router.get(260  "/",261  eventHandler((event) => {262    return { path: event.path, message: "Hello World!" };263  }),264);265```266 267Execute `npx --yes listhen -w --open ./server.mjs`.268 269See the [pino-http README](https://npm.im/pino-http) for more info.270