Fastify is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Fastify.
Custom Log Level
We can change the log level of our Fastify app.
For instance, we can write:
index.js
const fastify = require('fastify')({
logger: true
})
fastify.register(require('./routes/v1/users'), { prefix: '/v1' })
fastify.register(require('./routes/v2/users'), { prefix: '/v2' })
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
routes/v1/users.js
module.exports = function (fastify, opts, done) {
fastify.get('/user', async () => 'v1')
done()
}
routes/v2/user.js
module.exports = function (fastify, opts, done) {
fastify.get('/user', async () => 'v2')
done()
}
We set logger
to true
to enable logging.
We can also set the logging levels for individual routes.
For instance, we can write:
const fastify = require('fastify')()
fastify.get('/', { logLevel: 'warn' }, (request, reply) => {
reply.send({ hello: 'world' })
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We set the logLevel
to 'warn'
on the /
route.
Custom Log Serializer
We can customize the log serializer.
For instance, we can write:
const fastify = require('fastify')({
logger: {
level: 'info',
serializers: {
user (req) {
return {
method: req.method,
url: req.url,
headers: req.headers,
hostname: req.hostname,
remoteAddress: req.ip,
remotePort: req.socket.remotePort
}
}
}
}
})
fastify.register(context1, {
logSerializers: {
user: value => `My serializer father - ${value}`
}
})
async function context1 (fastify, opts) {
fastify.get('/', (req, reply) => {
req.log.info({ user: 'call father serializer', key: 'another key' })
reply.send({})
})
}
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to add the logSerializers
property.
We configure the logger when we call the Fastify factory function on the first line.
We set the level
to set the log level.
serializers
has an object with the user
function that returns the info from the request.
We return the request method, URL, headers, hostname, IP, and remote port respectively.
Then when we make the request, we see the output in the console.
Route Config
We can pass in a config object when we define our route.
For instance, we can write:
const fastify = require('fastify')()
function handler (req, reply) {
reply.send(reply.context.config.output)
}
fastify.get('/en', { config: { output: 'hello world!' } }, handler)
fastify.get('/fr', { config: { output: 'bonjour' } }, handler)
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We pass in an object with the config
property in the 2nd argument.
Then we can get that value from the reply.context
property.
Conclusion
We can set the custom logging and set route config with Fastify.