UNPKG

4.52 kBMarkdownView Raw
1# Web Frameworks
2
3Since HTTP logging is a primary use case, Pino has first class support for the Node.js
4web 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
17The 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
19to each individual request:
20
21```js
22const fastify = require('fastify')({
23 logger: true
24})
25fastify.get('/', async (request, reply) => {
26 request.log.info('something')
27 return { hello: 'world' }
28})
29```
30
31The `logger` option can also be set to an object, which will be passed through directly
32as the [`pino` options object](/docs/api.md#options-object).
33
34See 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
40npm install express-pino-logger
41```
42
43```js
44const app = require('express')()
45const pino = require('express-pino-logger')()
46
47app.use(pino)
48
49app.get('/', function (req, res) {
50 req.log.info('something')
51 res.send('hello world')
52})
53
54app.listen(3000)
55```
56
57See 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
63npm install hapi-pino
64```
65
66```js
67'use strict'
68
69require('make-promises-safe')
70
71const Hapi = require('hapi')
72
73async 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
113start().catch((err) => {
114 console.log(err)
115 process.exit(1)
116})
117```
118
119See 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
125npm install restify-pino-logger
126```
127
128```js
129const server = require('restify').createServer({name: 'server'})
130const pino = require('restify-pino-logger')()
131
132server.use(pino)
133
134server.get('/', function (req, res) {
135 req.log.info('something')
136 res.send('hello world')
137})
138
139server.listen(3000)
140```
141
142See 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
150npm install koa-pino-logger
151```
152
153```js
154const Koa = require('koa')
155const app = new Koa()
156const pino = require('koa-pino-logger')()
157
158app.use(pino)
159
160app.use((ctx) => {
161 ctx.log.info('something else')
162 ctx.body = 'hello world'
163})
164
165app.listen(3000)
166```
167
168See 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
174npm install pino-http
175```
176
177```js
178const http = require('http')
179const server = http.createServer(handle)
180const logger = require('pino-http')()
181
182function handle (req, res) {
183 logger(req, res)
184 req.log.info('something else')
185 res.end('hello world')
186}
187
188server.listen(3000)
189```
190
191See 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
198npm install nestjs-pino
199```
200
201```ts
202import { NestFactory } from '@nestjs/core'
203import { Controller, Get, Module } from '@nestjs/common'
204import { LoggerModule, Logger } from 'nestjs-pino'
205
206@Controller()
207export 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})
221class MyModule {}
222
223async function bootstrap() {
224 const app = await NestFactory.create(MyModule)
225 await app.listen(3000)
226}
227bootstrap()
228```
229
230See the [nestjs-pino readme](http://npm.im/nestjs-pino) for more info.