Logging

Octoris has built in functionality that allows you to take advantage of logging within the framework.

Using Logging

You can use the logger by first initializing it within the composeRoutes function you can pass it true as a value to just use the default setup like so

const { router } = require('octoris')

router.composeRoutes({ logger: true }, [routes])

The logger uses the Pino package to do the actual logging check it's documentation for the different functions it has available.

You can also pass the logger a options object that follows the pino options object

routes.js
const { router } = require('octoris')

router.composeRoutes({
  logger: {
    level: 'info',
    file: '/path/to/file' // Will use pino.destination()
  }
})

If you provide a file option octoris will automatically use the pino.destination() functionality to make sure your logs are written to that file

Logging From Context

If you setup and decide to use the logger built into octoris, you then have it available to you from the context object passed to your routes and middleware.

function routeHandler (ctx) {
  ctx.logger.info('Handling route!')

  return send(200, 'route handled')
}

You can checkout the different logger methods available within the pino documentation

Last updated