Logger

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

Logger ships with following drivers. However, you free to add your drivers built on top of Winston transports.

  1. Console ( console )

  2. File ( file )

Configuration

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

logger: {
  transport: 'console',
  console: {
    driver: 'console'
  },
  file: {
    driver: 'file',
    filename: 'adonis.log'
  }
}

The file driver saves the file inside tmp directory. However, you can define an absolute path to a different location.

Basic example

Let’s start with a basic example of logging data within your app. Also, all logging methods support sprintf syntax.

const Logger = use('Logger')

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

Logger.info('request details %j', {
  url: request.url(),
  user: auth.user.username()
})

The logger follows RFC5424 levels to log messages. Also, it exposes readable 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 always has a certain logging level set via config file, which can be updated at runtime too. 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 makes it easier to turn off debugging messages when your server is under high load.