logger: {
transport: 'console',
console: {
driver: 'console'
},
file: {
driver: 'file',
filename: 'adonis.log'
}
}
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.
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.
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 |
|
1 |
alert |
|
2 |
crit |
|
3 |
error |
|
4 |
warning |
|
5 |
notice |
|
6 |
info |
|
7 |
debug |
|
You can switch transports on the fly using the transport
method.
Logger
.transport('file')
.info('request url is %s', request.url())
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.