You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.

Logger

AdonisJs comes with a fully featured logger built on top of winston, using RFC5424 logging levels.

Logger ships with the following drivers:

  1. Console (console)

  2. File (file)

You are free to add your own drivers built on top of winston transports.

Configuration

The configuration for Logger is saved inside the config/app.js file under the logger object:

config/app.js
logger: {
  transport: 'console',
  console: {
    driver: 'console'
  },
  file: {
    driver: 'file',
    filename: 'adonis.log'
  }
}

The file driver saves your log file inside the application root tmp directory.

You can define an absolute filename path to a different log file location if you wish.

Basic example

Let’s start with a basic example of logging data within your app:

const Logger = use('Logger')

Logger.info('request url is %s', request.url())

Logger.info('request details %j', {
  url: request.url(),
  user: auth.user.username()
})
All logging methods support sprintf syntax.

The logger uses RFC5424 log levels, exposing simple methods for each level:

Level Method Usage

0

emerg

Logger.emerg(msg, …​data)

1

alert

Logger.alert(msg, …​data)

2

crit

Logger.crit(msg, …​data)

3

error

Logger.error(msg, …​data)

4

warning

Logger.warning(msg, …​data)

5

notice

Logger.notice(msg, …​data)

6

info

Logger.info(msg, …​data)

7

debug

Logger.debug(msg, …​data)

Switching transports

You can switch transports on the fly using the transport method:

Logger
  .transport('file')
  .info('request url is %s', request.url())

Logging level

Logger has a default config logging level which can be updated at runtime.

Any messages above the defined logging level are not logged. For example:

const Logger = use('Logger')
Logger.level = 'info'

// not logged
Logger.debug('Some debugging info')

Logger.level = 'debug'

// now logged
Logger.debug('Some debugging info')

This approach could make it easier to turn off debugging messages when your server is under high load.