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.
Logger ships with the following drivers:
Console (console
)
File (file
)
You are free to add your own drivers built on top of winston transports.
The configuration for Logger is saved inside the config/app.js
file under the logger
object:
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.
|
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 |
|
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 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.