1 | # Web Frameworks
|
2 |
|
3 | Since HTTP logging is a primary use case, Pino has first class support for the Node.js
|
4 | web framework ecosystem.
|
5 |
|
6 | + [Pino with Fastify](#fastify)
|
7 | + [Pino with Express](#express)
|
8 | + [Pino with Hapi](#hapi)
|
9 | + [Pino with Restify](#restify)
|
10 | + [Pino with Koa](#koa)
|
11 | + [Pino with Node core `http`](#http)
|
12 | + [Pino with Nest](#nest)
|
13 |
|
14 | <a id="fastify"></a>
|
15 | ## Pino with Fastify
|
16 |
|
17 | The Fastify web framework comes bundled with Pino by default, simply set Fastify's
|
18 | `logger` option to `true` and use `request.log` or `reply.log` for log messages that correspond
|
19 | to each individual request:
|
20 |
|
21 | ```js
|
22 | const fastify = require('fastify')({
|
23 | logger: true
|
24 | })
|
25 | fastify.get('/', async (request, reply) => {
|
26 | request.log.info('something')
|
27 | return { hello: 'world' }
|
28 | })
|
29 | ```
|
30 |
|
31 | The `logger` option can also be set to an object, which will be passed through directly
|
32 | as the [`pino` options object](/docs/api.md#options-object).
|
33 |
|
34 | See the [fastify documentation](https://www.fastify.io/docs/latest/Logging/) for more information.
|
35 |
|
36 | <a id="express"></a>
|
37 | ## Pino with Express
|
38 |
|
39 | ```sh
|
40 | npm install express-pino-logger
|
41 | ```
|
42 |
|
43 | ```js
|
44 | const app = require('express')()
|
45 | const pino = require('express-pino-logger')()
|
46 |
|
47 | app.use(pino)
|
48 |
|
49 | app.get('/', function (req, res) {
|
50 | req.log.info('something')
|
51 | res.send('hello world')
|
52 | })
|
53 |
|
54 | app.listen(3000)
|
55 | ```
|
56 |
|
57 | See the [express-pino-logger readme](http://npm.im/express-pino-logger) for more info.
|
58 |
|
59 | <a id="hapi"></a>
|
60 | ## Pino with Hapi
|
61 |
|
62 | ```sh
|
63 | npm install hapi-pino
|
64 | ```
|
65 |
|
66 | ```js
|
67 | 'use strict'
|
68 |
|
69 | require('make-promises-safe')
|
70 |
|
71 | const Hapi = require('hapi')
|
72 |
|
73 | async function start () {
|
74 | // Create a server with a host and port
|
75 | const server = Hapi.server({
|
76 | host: 'localhost',
|
77 | port: 3000
|
78 | })
|
79 |
|
80 | // Add the route
|
81 | server.route({
|
82 | method: 'GET',
|
83 | path: '/',
|
84 | handler: async function (request, h) {
|
85 | // request.log is HAPI standard way of logging
|
86 | request.log(['a', 'b'], 'Request into hello world')
|
87 |
|
88 | // a pino instance can also be used, which will be faster
|
89 | request.logger.info('In handler %s', request.path)
|
90 |
|
91 | return 'hello world'
|
92 | }
|
93 | })
|
94 |
|
95 | await server.register({
|
96 | plugin: require('.'),
|
97 | options: {
|
98 | prettyPrint: process.env.NODE_ENV !== 'production'
|
99 | }
|
100 | })
|
101 |
|
102 | // also as a decorated API
|
103 | server.logger().info('another way for accessing it')
|
104 |
|
105 | // and through Hapi standard logging system
|
106 | server.log(['subsystem'], 'third way for accessing it')
|
107 |
|
108 | await server.start()
|
109 |
|
110 | return server
|
111 | }
|
112 |
|
113 | start().catch((err) => {
|
114 | console.log(err)
|
115 | process.exit(1)
|
116 | })
|
117 | ```
|
118 |
|
119 | See the [hapi-pino readme](http://npm.im/hapi-pino) for more info.
|
120 |
|
121 | <a id="restify"></a>
|
122 | ## Pino with Restify
|
123 |
|
124 | ```sh
|
125 | npm install restify-pino-logger
|
126 | ```
|
127 |
|
128 | ```js
|
129 | const server = require('restify').createServer({name: 'server'})
|
130 | const pino = require('restify-pino-logger')()
|
131 |
|
132 | server.use(pino)
|
133 |
|
134 | server.get('/', function (req, res) {
|
135 | req.log.info('something')
|
136 | res.send('hello world')
|
137 | })
|
138 |
|
139 | server.listen(3000)
|
140 | ```
|
141 |
|
142 | See the [restify-pino-logger readme](http://npm.im/restify-pino-logger) for more info.
|
143 |
|
144 | <a id="koa"></a>
|
145 | ## Pino with Koa
|
146 |
|
147 | ### Koa
|
148 |
|
149 | ```sh
|
150 | npm install koa-pino-logger
|
151 | ```
|
152 |
|
153 | ```js
|
154 | const Koa = require('koa')
|
155 | const app = new Koa()
|
156 | const pino = require('koa-pino-logger')()
|
157 |
|
158 | app.use(pino)
|
159 |
|
160 | app.use((ctx) => {
|
161 | ctx.log.info('something else')
|
162 | ctx.body = 'hello world'
|
163 | })
|
164 |
|
165 | app.listen(3000)
|
166 | ```
|
167 |
|
168 | See the [koa-pino-logger readme](https://github.com/pinojs/koa-pino-logger) for more info.
|
169 |
|
170 | <a id="http"></a>
|
171 | ## Pino with Node core `http`
|
172 |
|
173 | ```sh
|
174 | npm install pino-http
|
175 | ```
|
176 |
|
177 | ```js
|
178 | const http = require('http')
|
179 | const server = http.createServer(handle)
|
180 | const logger = require('pino-http')()
|
181 |
|
182 | function handle (req, res) {
|
183 | logger(req, res)
|
184 | req.log.info('something else')
|
185 | res.end('hello world')
|
186 | }
|
187 |
|
188 | server.listen(3000)
|
189 | ```
|
190 |
|
191 | See the [pino-http readme](http://npm.im/pino-http) for more info.
|
192 |
|
193 |
|
194 | <a id="nest"></a>
|
195 | ## Pino with Nest
|
196 |
|
197 | ```sh
|
198 | npm install nestjs-pino
|
199 | ```
|
200 |
|
201 | ```ts
|
202 | import { NestFactory } from '@nestjs/core'
|
203 | import { Controller, Get, Module } from '@nestjs/common'
|
204 | import { LoggerModule, Logger } from 'nestjs-pino'
|
205 |
|
206 | @Controller()
|
207 | export class AppController {
|
208 | constructor(private readonly logger: Logger) {}
|
209 |
|
210 | @Get()
|
211 | getHello() {
|
212 | this.logger.log('something')
|
213 | return `Hello world`
|
214 | }
|
215 | }
|
216 |
|
217 | @Module({
|
218 | controllers: [AppController],
|
219 | imports: [LoggerModule.forRoot()]
|
220 | })
|
221 | class MyModule {}
|
222 |
|
223 | async function bootstrap() {
|
224 | const app = await NestFactory.create(MyModule)
|
225 | await app.listen(3000)
|
226 | }
|
227 | bootstrap()
|
228 | ```
|
229 |
|
230 | See the [nestjs-pino readme](http://npm.im/nestjs-pino) for more info.
|